Saturday, September 20, 2008

Code F#: Generate Pascal Triangles

The following is code written in F#. It generates a list of Pascal Triangles.

#light


 

let oddTriangle x = [x, ( x * x - 1) / 2, (x * x + 1) / 2]

let evenTriangle x = [ 2 * x, (x * x) - 1, (x * x ) + 1]


 

let pairTriangle x = [ oddTriangle (2 * x - 1), evenTriangle x ]


 

let numbers = [ 1 .. 100]


 

let perfectTriangles = List.map pairTriangle numbers


 

printf "%A" perfectTriangles


 

open System

Console.ReadKey(true)


 

Output:

[[([(1, 0, 1)], [(2, 0, 2)])]; [([(3, 4, 5)], [(4, 3, 5)])];

[([(5, 12, 13)], [(6, 8, 10)])]; [([(7, 24, 25)], [(8, 15, 17)])];

[([(9, 40, 41)], [(10, 24, 26)])]; [([(11, 60, 61)], [(12, 35, 37)])];

[([(13, 84, 85)], [(14, 48, 50)])]; [([(15, 112, 113)], [(16, 63, 65)])];

[([(17, 144, 145)], [(18, 80, 82)])]; [([(19, 180, 181)], [(20, 99, 101)])]]

No comments: