Project Euler in F# – Problem 53
Some of the problems proposed by Project Euler actually present the solution together with the description of the problem itself.
This is the case with Problem 53:
There are exactly ten ways of selecting three from five, 12345:
123, 124, 125, 134, 135, 145, 234, 235, 245, and 345
In combinatorics, we use the notation, ^(5)C_(3) = 10.
In general,
^(n)C_(r) =
n!r!(n−r)!
,where r ≤ n, n! = n×(n−1)×…×3×2×1, and 0! = 1.It is not until n = 23, that a value exceeds one-million: ^(23)C_(10) = 1144066.
How many, not necessarily distinct, values of ^(n)C_(r), for 1 ≤ n ≤ 100, are greater than one-million?
The only knowledge required in this problem is the binomial coefficient, but its formula is also described in the text above, so we can simply generate all couples (n, r) and check if the value of the binomial coefficient is greater than one million.
In F# this is equivalent to coding a function for the binomial coefficient and testing the items of a sequence:
#light
open Microsoft.FSharp.Math
let binomial_coefficient n k = BigInt.Factorial n /
(BigInt.Factorial k * BigInt.Factorial (n-k))
let answer =
seq { for n in 1I .. 100I do
for k in 1I .. n -> n,k } |> Seq.filter (fun (a,b) -> binomial_coefficient a b > 1000000I ) |> Seq.length
The approach adopted is almost the same that I used to solve Project Euler problem number 9. It is not the smartest nor the quickest since it is essentially a brute-force algorithm, but without any doubt it is the easiest to implement.
Which optimizations would you consider?
claudio on December 7th 2008 in Project Euler
Project Euler in F# - Problem 53 » Rash thoughts about .NET, C#, F# and Dynamics NAV. responded on 08 Dec 2008 at 12:23 pm #
[...] Cherubino (from fsharp.it/) posted his solution to Problem 53 in the Euler Project. As I dealed a lot with Dynamic Programming in the recent time, [...]
Steffen Forkmann responded on 08 Dec 2008 at 12:54 pm #
Hi,
I posted my solutions at http://www.navision-blog.de/2008/12/08/project-euler-in-fsharp-problem-53/.
Best regards,
Steffen
Project Euler in F# - Problem 55 » FSharp.it responded on 17 Dec 2008 at 6:30 pm #
[...] in the last post, in the description of Project Euler problem 55 there is a detailed step-by-step solution of the [...]