Files
30-seconds-of-code/snippets/collatz.md
Christian Bender e44fef2a2a changed
I changed 'javascript' in 'js'
2017-12-14 13:48:57 +01:00

234 B

Collatz algorithm

If n even then returns n/2 otherwise (n is odd) 3n+1. It uses the ternary operator.

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