Download code

From LiteratePrograms

Jump to: navigation, search

Back to Fibonacci_numbers_(E)

Download for Windows: zip

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

fib_memo.e

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


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 
17 


Views
Personal tools