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: rare example of a research language that has so many cool features that it's entering industry, it is now used in some computational and financial software
| ALGORITHM 1A: NAIVE BINARY RECURSION |
module Main( main ) where
import System( getArgs )
f n | n < 0 && rem n 2 == 0 = negate (fib (negate n))
| n < 0 && rem n 2 /= 0 = fib (negate n)
| otherwise = fib n
where fib n | n < 2 = n
| otherwise = fib (n-1) + fib (n-2)
f_print n = print(show n ++ "th Fibonacci number is " ++ show (f n))
main = do
args <- getArgs
if (length args /= 1)
then putStr "Usage: f1a <n>"
else (f_print (read (head args)))
putStr "\n"
|
| ALGORITHM 2A: CACHED LINEAR RECURSION / INFINITE LAZY EVALUATED LIST |
module Main( main ) where
import System( getArgs )
fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
f n | n < 0 && rem n 2 == 0 = negate (f (negate n))
| n < 0 && rem n 2 /= 0 = f (negate n)
| otherwise = fibs!!n
f_print n = print(show n ++ "th Fibonacci number is " ++ show (f n))
main = do
args <- getArgs
if (length args /= 1)
then putStr "Usage: f2a <n>"
else (f_print (read (head args)))
putStr "\n"
|
| ALGORITHM 2B: LINEAR RECURSION WITH ACCUMULATOR |
module Main( main ) where
import System( getArgs )
f n | n < 0 && rem n 2 == 0 = negate (f (abs n))
| otherwise = fib 0 1 (abs n) where
fib a b c = if c<1 then a else fib (a+b) a (c-1)
f_print n = print(show n ++ "th Fibonacci number is " ++ show (f n))
main = do
args <- getArgs
if (length args /= 1)
then putStr "Usage: f2b <n>"
else (f_print (read (head args)))
putStr "\n"
|
| 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
|
|