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

11 lines
234 B
Markdown

### Collatz algorithm
If n even then returns **n/2** otherwise (n is odd) **3n+1**.
It uses the ternary operator.
``` js
const collatz = n => (n % 2 == 0) ? (n/2) : (3*n+1);
// collatz(8) --> 4
// collatz(5) --> 16
```