Ultrashock Forums > Flash > ActionScript > UltraMath
fun with maths/programming

You are currently viewing our website as a guest which gives you limited access to forums, files and other resources.

Click here to join now for free, and start interacting with our members, download files and much more!

Click here if you are looking for our Flash files and other professional assets.
 
Post Reply | View first unread | Rate Thread Search this Thread | Thread Tools | Display Modes

#1
Bookmark and Share!
fun with maths/programming
Old 2002-10-08

wana have fun?
try to be creative by solving this problem in as many ways as you can:

you are writing a program (in a C-like language, or whatever), and you have a variable k which is either equal to 1 or 2.
find as many ways as possible so that if k is 1 then it becomes 2, and if k is 2 then it becomes 1.

this problem was given to us in a "basic" computer course at the begining of this year...
quite a simple question, but with so many answers...
some relate to math, geometry and programming tricks...
I found it fun, and thought you might too

I'll post all answers I know sometime next week, until then, post what you find...


(I have a feeling that robert will get real creative here...)
I really do find it all.
postbit arrow 27 comments | 1030 views postbit arrow Reply: with Quote   
Registered User
Basta is offline
seperator
Posts: 3,713
2001-04-07
Age: 26
Basta lives in Canada
seperator

Ultrashock Member Comments:
pedrodinis pedrodinis is offline pedrodinis lives in Portugal 2002-10-08 #2 Old  
you could always do
k = 3-k

it's pretty simple and does the job.
and it work for every language too
Reply With Quote  
bit-101 bit-101 is offline 2002-10-08 #3 Old  
as MANY POSSIBLE ways?

not the best, shortest, most elegant way?

how about

Code:
if(k==1){
  k=2;
} else {
  k=1;
}
Code:
k=(k==1)?2:1;
Code:
if(!(k--)) k=2;
Reply With Quote  
Syzla Syzla is offline 2002-10-08 #4 Old  
k=(k%=2)+1;

or

k=k%2+1;
Reply With Quote  
pedrodinis pedrodinis is offline pedrodinis lives in Portugal 2002-10-09 #5 Old  
k = !(k-1) + 1;
Reply With Quote  
Basta Basta is offline Basta lives in Canada 2002-10-09 #6 Old  
looking good, but there are still some missing
it's a real brain twister once you get into it...
Reply With Quote  
eighty eighty is offline 2002-10-09 #7 Old  
Last edited by eighty : 2002-10-09 at 11:48.
Code:
// This is a boring one
switch (k) {
  case 1:
    k = 2;
  case 2:
    k = 1;
}

// This requires another variable bool b
k = ++b + 1;

// Geometry!
k = 1 + sin(asin(k-1) + PI/2);

// Another..
k = pow(2,2-k);

// Some bitshift
(k-1)? (k >>= 1) : (k <<= 1);
Reply With Quote  
robpenner robpenner is offline 2002-10-09 #8 Old  
Code:
function flip (k) {
	return (Math.log(k)) ? --k : ++k;
};
Reply With Quote  
bit-101 bit-101 is offline 2002-10-09 #9 Old  
let's go bit-wise!

Code:
k=(((k&1)<<2)|k)>>1;
might be able to remove some of those parentheses. not sure.
Reply With Quote  
robpenner robpenner is offline 2002-10-09 #10 Old  
Code:
function flip (k) {
	return (Math.sqrt(--k)) + 2*(!k);
};
Reply With Quote  
robpenner robpenner is offline 2002-10-09 #11 Old  
Code:
function flip (k) {
	return 1-(k--*--k);
};
Reply With Quote  
robpenner robpenner is offline 2002-10-09 #12 Old  
Code:
k=1+!(--k/k);
Reply With Quote  
robpenner robpenner is offline 2002-10-09 #13 Old  
Code:
k=!(++k%--k)+1;
Reply With Quote  
bit-101 bit-101 is offline 2002-10-09 #14 Old  
i like the bitwise ones.


Code:
k=(k>>1|k<<1)&3;
Reply With Quote  
robpenner robpenner is offline 2002-10-09 #15 Old  
Code:
k=2/k;
Reply With Quote  
robpenner robpenner is offline 2002-10-09 #16 Old  
Code:
k=Math.abs(k*3-5);
Reply With Quote  
robpenner robpenner is offline 2002-10-09 #17 Old  
Code:
k=Math.ceil(Math.cos(k))+1;
Reply With Quote  
robpenner robpenner is offline 2002-10-09 #18 Old  
Code:
k=Math.round(2.4-k/2);
Reply With Quote  
robpenner robpenner is offline 2002-10-09 #19 Old  
Code:
k=2-Math.floor(Math.tan(--k));
Reply With Quote  
bit-101 bit-101 is offline 2002-10-09 #20 Old  
i think rob likes this.
Reply With Quote  
robpenner robpenner is offline 2002-10-09 #21 Old  
Code:
k=Math.abs((k^2)-1);
Reply With Quote  
robpenner robpenner is offline 2002-10-09 #22 Old  
Originally posted by bit-101
i think rob likes this.
I'm just trying to boost my post count.
Reply With Quote  
Basta Basta is offline Basta lives in Canada 2002-10-12 #23 Old  
(I have a feeling that robert will get real creative here...)
I knew this would happen

there are some great solutions here... some I didn't know

but there are still some (one or two) that I know that haven't been posted

I'll post them monday or something (I'm giving more time to robert, so that he can have more fun...)


useless maths are so fun
Reply With Quote  
Dickee Dickee is offline 2002-10-12 #24 Old  
// fun with arrays

// init array for options 1,2,3
aNum = [1];

// option 1:
if (aNum.length==1) aNum.push(2);
else aNum.pop();

// option 2:
if (aNum.length==2) aNum.splice(1,1);
else aNum.push(2);

// options 1 and 2 return options:
k = aNum.length;
// or ...
k = aNum[aNum.length-1];

////////////////////////////////

// option 3:
if (aNum.length==1) aNum.push(2), k = aNum[1];

// option 3 return options:
else k = aNum.shift(), aNum = [k];
// or ...
else aNum.pop(), k = aNum[0];

////////////////////////////////////////////////////////

// init array for options 4,5
aNum = [1,2];

// option 4:
aNum.reverse();
k = aNum[0];

// option 5:
if (k==1) aTemp = aNum.slice(1);
else aTemp = aNum.slice(0,-1);
k = aTemp[0];

Richard
Reply With Quote  
Dickee Dickee is offline 2002-10-13 #25 Old  
Last edited by Dickee : 2003-05-12 at 11:27.
Code:
// init array
aNum = [1,2];

// option 6 - uses all 13 methods of the Array object
// caution: some code snippets may include redundancy :D
function dohseedohAllMethods() {
    var a = aNum.splice(0,1).slice(-1); 
    aNum.unshift(a);
    var b = aNum.splice(1).slice(0);
    aNum.push(b);
}
var aTemp = aNum.concat();
aNum.sort(dohseedohAllMethods);
(aTemp[0]==aNum[0]) ? (aNum.reverse(),k=aNum[0]) : 
	              (aNum.toString().join(),k=aNum[1]);
c = aTemp.pop();
d = aTemp.shift();
aSorter = [{e:c,f:d},{e:d,f:c}];
aSorter.sortOn(f);
(!aTemp.length) ? (c=null,delete d,aSorter=undefined) : 
                  (delete c,d=undefined,aSorter=null);
Richard
Reply With Quote  
Basta Basta is offline Basta lives in Canada 2002-10-14 #26 Old  
So here are the ones I knew about:

Code:
// the 3 boring ones

if (k==1) {
k=2;
} else {
k=1;
}

//

switch (k) {
case 1:
k=2;
case 2:
k=1;
}

//

k=(k==1) ? 2 : 1;

// two nice and efficient ones

k=3-k;

//

k=2/k;

// array

j=[0,2,1];
k=j[k];

// mod

k=k%2+1;

// pow

k=k-Math.pow(-1,k);

// boolean

k=!(k-1)+1;
so, that's it...

did you kids have fun?
Reply With Quote  
Basta Basta is offline Basta lives in Canada 2002-10-16 #27 Old  
more that I found...

Code:
// trigo
k=sin(PI*k/2)+1;
//
k=cos(PI/k)+1;

// complex: this won't work in C, because
// C doesn't get complex numbers, but it
// would work in FORTRAN if it were rewritten
// properly (I"m not sure of the function names...)
// btw, i is equal to the square root of -1
// (yes, that's minus one...)
// oh, and ** simply means pow (or ^)

k=real(i**k+1)+img(i**k+1)
Reply With Quote  
Basta Basta is offline Basta lives in Canada 2003-05-11 #28 Old  
http://www.holger-kohnen.de/ linked to this thread, so I thought I'd revive it (it's a great blog, by the way)

when you think about it, there is an infinite number of ways to solve this problem
Reply With Quote  
Thread Tools
Display Modes Rate This Thread
Rate This Thread: