Last active
July 17, 2017 19:09
-
-
Save apiv/4399737 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* the function */ | |
fib = (x) -> | |
if x < 2 | |
x | |
else | |
fib(x-1) + fib(x-2) | |
/* test it out */ | |
solutions = [] | |
for number in [0..10] | |
solutions.push ( fib number ) | |
console.log solutions |
Note that comments in CoffeeScript are # comment
not /* comment */
nor // comment
.
Here is a more iterative and concise version:
f = (l) ->
p = 0
n = 1
[0].concat (p for i in [1..l] when [p, n] = [n, p + n])
console.log f 10 # (11) [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's a good function, but there's an issue with it. It will not like doing numbers above 40 since it is recursive.
I would suggest using a Loop instead.