forked from rmetzler/litcoffee-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fib.coffee.md
executable file
·62 lines (38 loc) · 1.34 KB
/
fib.coffee.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#! /usr/bin/env coffee
# fib.litcoffee
This program prints all Fibonacci numbers. It is written in Literate CoffeeScript.
Fibonacci is a common example for teaching recursive functions and complexity theory.
## What are the Fibonacci numbers?
Every Fibonacci Number (`fib(n)`) is built by the following rules.
- fib(1) = 1
- fib(2) = 1
- fib(n) = fib(n-1) + fib(n-2)
The last rule is the recursion step.
As a recursive CoffeeScriptIn a recursive style this would be written like this:
# simple fibonacci function
fib = (n) ->
# first rules
if n in [ 1 , 2 ]
return 1
# recursion step
fib( n-1 ) + fib( n-2 )
# call the function like this
fib(1) # => fib(2) + fib(1) # => 1 + 1 # => 1
fib(2) # => fib(2) + fib(1) # => 1 + 1 # => 1
fib(3) # => fib(2) + fib(1) # => 1 + 1 # => 2
fib(4) # => fib(3) + fib(2) # => 2 + 1 # => 3
fib(5) # => fib(4) + fib(3) # => 3 + 2 # => 5
# Complexity Theory
For bigger n the The recursion step is going to spend more and more time for bigger
We use a memory structure to cache and speed up the computation.
cache =
1: 1
2: 1
Fibonacci is a recursive function.
fib = (n) ->
unless cache[n]
cache[n] = fib(n-1) + fib(n-2)
cache[n]
for n in [1..100]
console.log "fib(#{n}) => #{fib(n)}"
#console.log mem