Update and rename collatz.md to collatz-algorithm.md
This commit is contained in:
9
snippets/collatz-algorithm.md
Normal file
9
snippets/collatz-algorithm.md
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
### Collatz algorithm
|
||||||
|
|
||||||
|
If `n` is even, return `n/2`. Otherwise return `3n+1`.
|
||||||
|
|
||||||
|
```js
|
||||||
|
const collatz = n => (n % 2 == 0) ? (n/2) : (3*n+1);
|
||||||
|
// collatz(8) --> 4
|
||||||
|
// collatz(5) --> 16
|
||||||
|
```
|
||||||
@ -1,11 +0,0 @@
|
|||||||
### 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
|
|
||||||
|
|
||||||
```
|
|
||||||
Reference in New Issue
Block a user