Fibonacci number generator

From Omath

Jump to: navigation, search

The Fibonacci number generator below tests a few things. There's somewhat complicated pattern matching, making sure ? is working. More difficult is getting the pattern specificity right; the kernel has to make sure rules for specific values always apply before more general ones. In version <math>10^{-17}</math>, this is a bit of a hack!

We also have a good opportunity to test a common coding pattern. Any reasonable implementation should cache results, in order to run in linear (rather than fibonacci!) time.

fibonacci[0]=fibonacci[1]=1
fibonacci[n_Integer?Positive]:=fibonacci[n]=fibonacci[n-1] + fibonacci[n-2]
Personal tools