Files
30-seconds-of-code/snippets/collatz.md
Angelos Chalaris 97c250bcfb Updated examples
2017-12-27 16:35:25 +02:00

215 B

collatz

Applies the Collatz algorithm.

If n is even, return n/2. Otherwise, return 3n+1.

const collatz = n => (n % 2 == 0) ? (n / 2) : (3 * n + 1);
collatz(8) // 4
collatz(5) // 16