Skip to content

Instantly share code, notes, and snippets.

@roniemicro
Last active December 16, 2015 18:58
Show Gist options
  • Save roniemicro/5481265 to your computer and use it in GitHub Desktop.
Save roniemicro/5481265 to your computer and use it in GitHub Desktop.
3n+1 assignment sollution
function isOdd(n){
return (n & 1);
}
function getStepCount(n, c){
var count = c | 0;
count++;
if(n < 1){
return 0;
}else if(n == 1){
return count;
}else if(isOdd(n)){
return getStepCount(3*n+1, count);
}else{
return getStepCount(n/2, count) ;
}
}
function getMaximumStep(i,j){
if(i>j){
return "Wrong Input";
}
var data = {
max_step:0,
max_step_n:false
};
for(n=i; n<=j; n++){
var current_steps = getStepCount(n);
if( current_steps > data.max_step ){
data.max_step = current_steps;
data.max_step_n = n;
}
}
return "Maximum Step (" + data.max_step + ") Needed for number : " + data.max_step_n;
}
var n= prompt('Please enter value of i,j','');
n = n.split(',');
document.write(getMaximumStep(n[0],n[1]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment