-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathEuler.fs
70 lines (48 loc) · 1.54 KB
/
Euler.fs
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#light
open System.Numerics
(*
Problem #1
Answer: 234168
If we list all the natural numbers below 10 that are multiples of 3 or
5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
*)
let euler1 =
[ for x in 1..999 do
if x % 3 = 0 || x % 5 = 0
then yield x ] |> List.fold (+) 0
(*
Problem #2
Answer:
Each new term in the Fibonacci sequence is generated by adding the
previous two terms. By starting with 1 and 2, the first 10 terms will
be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
Find the sum of all the even-valued terms in the sequence which do not
exceed four million.
*)
// let fibs =
// Seq.unfold
// (fun (current, next) -> Some(current, (next, current + next)))
// (BigInteger 0, BigInteger 1)
// let fibs = Seq.initInfinite (fun n ->
// match n with
// | 0 -> 1
// | 1 -> 2
// | _ -> (fibs (n - 1)) + (fibs (n - 2)))
// let euler2 =
// [ for x in Seq.take 39999999 fibs do
// if x % (BigInteger 2) = (BigInteger 0)
// then yield x ] |> List.fold (+) (BigInteger 0)
//let eulers = [ euler1; euler2 ]
let eulers = [ euler1; ]
let rec do_eulers xs =
match xs with
| [||] -> do_eulers [| 1 .. eulers.Length |]
| _ -> for x in xs do
let n = (string >> int) x
printfn "%d. %d" n eulers.[n - 1]
[<EntryPoint>]
let main args =
do_eulers (Array.map (string >> int) args)
0