# IronPythonFibonacciNumbers

Enter a topic name to show or a new topic name to create; then press Enter

Mini-programs below are from Literate Programming

## Recursive Fibonacci

This is a recursive formula. Numbers up to 30 are okay. Numbers in excess of 100 using this formula would require more function calls than can be completed in the expected lifetime of the universe!

```    def fib(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return fib(n-1) + fib(n-2)
```

2019-11-12 19:50:33

fib(21) is: 10946

## Recursion with Memorization

This will easily calculate Fibonacci numbers in excess of 100 using a memorization formula

```    memo = {0:0, 1:1}
def fib(n):
if not n in memo:
memo[n] = fib(n-1) + fib(n-2)
return memo[n]
so = UtilityProperties.Now + UtilityProperties.Newline
so = so + 'fib(221) is: ' + str(fib(221))
ScriptOutput = so
```

2019-11-12 19:50:33

fib(221) is: 6867260041627791953052057353082063289320596021

## Fibonacci by Iteration

Using iteration to generate the Fibonacci number

```    def fib(n):
a, b = 0, 1
for i in range(n) :
a, b = b, a + b
return a
so = UtilityProperties.Now + UtilityProperties.Newline
so = so + 'fib(221) is: ' + str(fib(221))
ScriptOutput = so
```

2019-11-12 19:50:33

fib(221) is: 6867260041627791953052057353082063289320596021

Also see IronPythonFibonacciLargeNumbers

Version: 17   Revised: 2013-08-26 10:28:56 Last Updated by: 208.114.148.38 Rename Show Links to Topic