Connexion
Applications Google
Menu principal
Post a Comment On:
Scattered Thoughts
Untitled
No comments yet. -
1 – 0 of 0
Project Euler Problem 5:
What is the smallest number divisible by each of the numbers 1 to 20?
There are several ways to solve this problem. I chose to compute the
Least Common Multiple
of two numbers using their
Greatest Common Divisor
:
[Image]
Using Euclid's algorithm, the F# code for gcd is:
let rec gcd a b =
match a with
| a when a = 0L -> b
| a when a < b -> gcd (b-a) a
| a -> gcd (a-b) a
And lcm is just as easy:
let lcm a b = (a*b)/gcd a b
Then we can write the code that solves the problem:
seq {1L..20L} |> Seq.fold lcm 1L |> printfn "lcm = %A"
The fold operation calculates a running lcm. At each step the result of the previous step is passed to the lcm function with the current element in the sequence of integers.
posted by Bernie at
1:04 PM
on Oct 21, 2009
Leave your comment
You can use some HTML tags, such as
<b>, <i>, <a>
Comment moderation has been enabled. All comments must be approved by the blog author.
Choose an identity
Google Account
You will be asked to sign in after submitting your comment.
Name/URL
Comment with your Google account if you’d like to be able to manage your comments in the future. If you comment anonymously, you won’t be able to edit or delete your comment.
Learn more
Name
URL
Anonymous
Comment with your Google account if you’d like to be able to manage your comments in the future. If you comment anonymously, you won’t be able to edit or delete your comment.
Learn more
Please prove you're not a robot
Untitled
No comments yet. -