Friday, July 15, 2011

Triangle

Consider a triangle with vertices labelled A,B and C.
We'd like to count the number of paths of a given length say n, that start and end at A.
The interesting part is that there is a closed form solution to this problem,
and whats more this solution can be implemented in two lines of Haskell code.


g = 0:1:zipWith (\x y -> 2*x + y) g (tail g)

f = [2*x | x <- tail(g)] 

eg, f !!6 = 86 (that is , there are 86 distinct paths of length 8 )

No comments:

Post a Comment