serious stuff
www.cubbi.com
personal
programming
fibonacci numbers
algorithms
benchmarks
forth examples
postscript examples
muf examples
joy examples
j examples
scheme examples
hope examples
ocaml examples
haskell examples
prolog examples
c++ examples
java examples
assembly language examples
fortran examples
c examples
sh examples
awk examples
perl examples
tcl examples
asmix
hacker test
resume
science
martial arts
fun stuff
www.cubbi.org
|
Haskell
Date: 1990
Type: Pure functional lazy evaluated language
Usage: growing, I'd love to see more
| ALGORITHM 1A: BINARY RECURSION |
module Main where
-- Function f returns the n'th Fibonacci number
-- It uses ALGORITHM 1A: BINARY RECURSION
f n | n < 2 = 1
| n >= 2 = f (n-1) + f (n-2)
-- Function f_print prints the n'th Fibonacci number
f_print n = print(show n ++ "th Fibonacci number is " ++ show (f n))
-- Function main is the entry point of the program
main = f_print 46
|
| ALGORITHM 2A-1: DATA STRUCTURE - INFINITE LIST |
module Main where
-- Function f returns the n'th Fibonacci number
-- It uses ALGORITHM 2A-1: INFINITE LIST
-- This example is based on the one-liner from The Gentle Introduction to Haskell
f = 1 : 1 : zipWith (+) f (tail f)
-- Function f_print prints the n'th Fibonacci number
f_print n = print(show n ++ "th Fibonacci number is " ++ show (f!!n))
-- Function main is the entry point of the program
main = f_print 160000
|
| ALGORITHM 2B: SIMPLE RECURSION |
module Main where
-- Function f returns the n'th Fibonacci number
-- It uses ALGORITHM 2B: SIMPLE RECURSION
f n = l 1 1 n where
l a b c = if c<2 then a else l (a+b) a (c-1)
-- Function f_print prints the n'th Fibonacci number
f_print n = print(show n ++ "th Fibonacci number is " ++ show (f n))
-- Function main is the entry point of the program
main = f_print 160000
|
| ALGORITHM 2C: NON-RECURSIVE LOOP |
Haskell does not support non-recursive loops.
Rewriting this algorithm in recursion produces ALGORITHM 2B.
|
| ALGORITHM 3A: MATRIX EQUATION |
module Main where
import List(transpose)
-- Function f returns the n'th Fibonacci number
-- It uses ALGORITHM 3A: MATRIX EQUATION
f n = head (head (mp [[1,1],[1,0]] n))
-- Function mm performs matrix multiplication.
-- I don't know the original author of this one-liner.
mm a b = [[sum$zipWith (*) row col | col <- transpose a] | row <-b]
-- Function mp raises a matrix m into nth power (for n>0)
mp m n | n <= 1 = m
| otherwise = if (mod n 2)==1 then mm (mm a a) m else mm a a
where a = mp m (div n 2)
-- Function f_print prints the n'th Fibonacci number
f_print n = print(show n ++ "th Fibonacci number is " ++ show (f n))
-- Function main is the entry point of the program
main = f_print 1000000
|
| ALGORITHM 3B: FAST RECURSION |
module Main where
-- Function f returns the n'th Fibonacci number
-- It uses ALGORITHM 3B: FAST RECURSION
f n = fst (l n) where
l n | n<2 = (1,1)
| n==2 = (2,1)
| n>2 = if (rem n 2)==1 then ( k1*(k1+k2)+k1*k2 , k1*k1+k2*k2 )
else ((l1+l2)*(l1+l2)+l1*l1, (l1+l2)*l1+l1*l2)
where (k1,k2) = l (div (n-1) 2)
(l1,l2) = l ((div n 2)-1)
-- Function f_print prints the n'th Fibonacci number
f_print n = print(show n ++ "th Fibonacci number is " ++ show (f n))
-- Function main is the entry point of the program
main = f_print 500000
|
| ALGORITHM 3C: BINET'S FORMULA |
module Main where
-- Function f returns the n'th Fibonacci number
-- It uses ALGORITHM 3C: BINET'S FORMULA
f n = round((phi**(x+1)-(1-phi)**(x+1))/(sqrt 5))
where phi = (1+sqrt 5)/2
x = (fromInteger n)::Float
-- Function f_print prints the n'th Fibonacci number
f_print n = print(show n ++ "th Fibonacci number is " ++ show (f n))
-- Function main is the entry point of the program
main = f_print 30
|
|