| Ultrashock Forums
• fun with maths/programming |
|||
![]() |
||||
| Search this Thread | Thread Tools | Display Modes |
|
|
|||||||||||||||||||||||||
![]() |
Ultrashock Member Comments:
|
2002-10-08
#2 |
||
|
|
|
2002-10-08
#3 |
||
|
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; |
|
|
|
2002-10-08
#4 |
||
|
k=(k%=2)+1; or k=k%2+1; |
|
|
2002-10-09
#5 |
||
|
k = !(k-1) + 1;
|
|
|
2002-10-09
#6 |
||
|
looking good, but there are still some missing ![]() it's a real brain twister once you get into it... |
|
|
|
2002-10-09
#7 |
||
|
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);
|
|
|
|
2002-10-09
#8 |
||
|
Code:
function flip (k) {
return (Math.log(k)) ? --k : ++k;
};
|
|
|
|
2002-10-09
#9 |
||
|
let's go bit-wise! Code:
k=(((k&1)<<2)|k)>>1; |
|
|
|
2002-10-09
#10 |
||
|
Code:
function flip (k) {
return (Math.sqrt(--k)) + 2*(!k);
};
|
|
|
|
2002-10-09
#11 |
||
|
Code:
function flip (k) {
return 1-(k--*--k);
};
|
|
|
|
2002-10-09
#12 |
||
|
Code:
k=1+!(--k/k); |
|
|
|
2002-10-09
#13 |
||
|
Code:
k=!(++k%--k)+1; |
|
|
|
2002-10-09
#14 |
||
|
i like the bitwise ones. Code:
k=(k>>1|k<<1)&3; |
|
|
|
2002-10-09
#15 |
||
|
Code:
k=2/k; |
|
|
|
2002-10-09
#16 |
||
|
Code:
k=Math.abs(k*3-5); |
|
|
|
2002-10-09
#17 |
||
|
Code:
k=Math.ceil(Math.cos(k))+1; |
|
|
|
2002-10-09
#18 |
||
|
Code:
k=Math.round(2.4-k/2); |
|
|
|
2002-10-09
#19 |
||
|
Code:
k=2-Math.floor(Math.tan(--k)); |
|
|
|
2002-10-09
#20 |
||
|
i think rob likes this.
|
|
|
|
2002-10-09
#21 |
||
|
Code:
k=Math.abs((k^2)-1); |
|
|
|
2002-10-09
#22 |
||
|
Originally posted by bit-101 I'm just trying to boost my post count. i think rob likes this.
|
|
|
2002-10-12
#23 |
||
|
(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
|
|
|
|
2002-10-12
#24 |
||
|
// 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 |
|
|
|
2002-10-13
#25 |
||
|
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
|
|
|
2002-10-14
#26 |
||
|
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;
did you kids have fun?
|
|
|
2002-10-16
#27 |
||
|
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) |
|
|
2003-05-11
#28 |
||
|
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 |
|
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|



27 comments
| 1030 views




Linear Mode
k = 3-k
it's pretty simple and does the job.
and it work for every language too