collatz algorithm

collatz algorithm as function
This commit is contained in:
Christian Bender
2017-12-13 23:02:42 +01:00
committed by GitHub
parent 2674273efc
commit f92c6d4079

11
snippets/collatz.md Normal file
View File

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