Last active
August 5, 2018 14:11
-
-
Save JaHIY/f533dc79cbdc5c3d2e8324eea661ca72 to your computer and use it in GitHub Desktop.
Yang Hui's triangle, Pascal's triangle in F#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let rec getNext' (line : int list) result i = | |
if i < (List.length line) - 1 then | |
getNext' line ((line.[i] + line.[i + 1]) :: result) (i + 1) | |
else | |
1 :: result | |
let getNext line = | |
getNext' line [1] 0 | |
let rec get' n result i = | |
if i = 1 then | |
get' n ([1] :: result) (i + 1) | |
elif i = 2 then | |
get' n ([1; 1] :: result) (i + 1) | |
elif i <= n then | |
get' n ((getNext (List.head result)) :: result) (i + 1) | |
else | |
List.rev result | |
let get n = | |
get' n [] 1 | |
// test | |
printfn "%A" (get 10) | |
(* | |
output: | |
[[1]; [1; 1]; [1; 2; 1]; [1; 3; 3; 1]; [1; 4; 6; 4; 1]; [1; 5; 10; 10; 5; 1]; | |
[1; 6; 15; 20; 15; 6; 1]; [1; 7; 21; 35; 35; 21; 7; 1]; | |
[1; 8; 28; 56; 70; 56; 28; 8; 1]; [1; 9; 36; 84; 126; 126; 84; 36; 9; 1]] | |
*) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment