Quotes help make search much faster. Example: "Practice Makes Perfect"

Sunday, January 27, 2013

Math Problems: Collatz Function


Problem:

The Collatz function is defined as f(n) = 3n+1 for odd values of n and f(n) = n/2 for even values of n. What is the smallest possible value of n for which f^7(n) is equal to 5.

Answer: 17

Solution: Solved using the script below:


console.log("*********************************");
function collatz(x){
if(x%2===0){
return x/2;
}
else{
return (3*x)+1;
}
}
var max=1000;
for(var i=0; i<max; i++){
var temp = collatz(collatz(collatz(collatz(collatz(collatz(collatz(i)))))));
if(temp===5){
console.log("i: "+i+" temp: "+temp);
}
}

No comments:

Post a Comment


This is an example of scrolling text using Javascript.

Popular Posts