Created
March 15, 2017 06:52
-
-
Save T0071/c826bf85a4f204d477307c3098a09716 to your computer and use it in GitHub Desktop.
collatz conjecture
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isEven(a) { | |
a = Number(a); | |
return a === 0 || !!(a && !(a%2)); | |
} | |
// Write a Function for the conjecture | |
function collatz(n){ | |
const number = n; | |
let counter = 0; | |
while(n != 1){ | |
counter += 1; | |
// Condition | |
if(isEven(n) === true){ | |
n = n * 0.5; | |
}else if(n === 1){ | |
break; | |
} | |
else{ | |
n = n * 3 + 1; | |
} | |
} | |
console.log(number + "=>" + counter); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment