collatz algorithm
collatz algorithm as function
This commit is contained in:
11
snippets/collatz.md
Normal file
11
snippets/collatz.md
Normal 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
|
||||||
|
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user