Download code

Jump to: navigation, search

Back to Fibonacci_numbers_(E)

Download for Windows: zip

Download for UNIX: zip, tar.gz, tar.bz2

fib_recursive.e

 1 #!/usr/bin/env rune
 2 
 3 pragma.syntax("0.9")
 4 
 5 def fib(n :(int >= 0)) :int {
 6   switch (n) {
 7     match == 0 { return 0 }
 8     match == 1 { return 1 }
 9     match _    { return fib(n-1) + fib(n-2) }
10   }
11 }
12 
13 for i in 0..20 {
14   print(" ", fib(i))
15 }
16 


hijacker
hijacker
hijacker
hijacker

fib_memo.e

 1 #!/usr/bin/env rune
 2 
 3 pragma.syntax("0.9")
 4 
 5 def makeFib() {
 6   var fibs := [0 => 0, 1 => 1].diverge()
 7 
 8   def fib { 
 9     to run(n :(int >= 0)) :int {
10       if (!fibs.maps(n)) {
11         fibs[n] := fib(n-1) + fib(n-2)
12       }
13       return fibs[n]
14     }
15   }
16   return fib
17 }
18 
19 
20 def fib := makeFib()
21 for i in 0..20 {
22   print(" ", fib(i))
23 }


hijacker
hijacker
hijacker
hijacker