-
Notifications
You must be signed in to change notification settings - Fork 0
/
easyline.fsx
35 lines (25 loc) · 1.03 KB
/
easyline.fsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
(*
In the drawing below we have a part of the Pascal's triangle, lines are numbered from zero (top).
We want to calculate the sum of the squares of the binomial coefficients on a given line with a function called easyline (or easyLine or easy-line).
Can you write a program which calculate easyline(n) where n is the line number?
The function will take n (with: n>= 0) as parameter and will return the sum of the squares of the binomial coefficients on line n.
##Examples:
easyline(0) => 1
easyline(1) => 2
easyline(4) => 70
easyline(50) => 100891344545564193334812497256
*)
let easyline (n:int) =
let rec getpascal line no =
match no with
|_ when no = n -> line
|_ -> getpascal ((line
|> List.pairwise
|> List.map(fun (x, y) -> x + y)
|> List.append [line.Head])
@ [line.[line.Length - 1]]) (no + 1)
getpascal [1I] 0 |> List.sumBy(fun x -> pown x 2)
#time
let p = easyline 15000
#time
printfn "%O" p