Skip to content

Instantly share code, notes, and snippets.

@wertlex
Created January 7, 2020 16:25
Show Gist options
  • Save wertlex/d03a8728b73d7162f0fb60960848d01f to your computer and use it in GitHub Desktop.
Save wertlex/d03a8728b73d7162f0fb60960848d01f to your computer and use it in GitHub Desktop.
// https://en.wikipedia.org/wiki/Collatz_conjecture
function collatzStep(n: number): number {
if (n % 2 === 0) {
return n/2;
} else {
return (n * 3) + 1;
}
}
let i: number = 1;
let n: number = 3; // number to start with
console.log(`${i}: ${n}`);
while (true) {
n = collatzStep(n);
console.log(`${i}: ${n}`);
i++;
if (n === 1) {
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment