Created
August 27, 2011 14:42
-
-
Save 55v/1175467 to your computer and use it in GitHub Desktop.
Problem #2 from http://projecteuler.net/
This file contains 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
(* | |
Problem 2 | |
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 problem_2 bound = | |
let rec fsum prev preprev sum = | |
let nfib = prev + preprev | |
if nfib >= bound then sum | |
else | |
if nfib % 2 <> 0 then | |
printfn "nfib %i, sum %i" nfib sum | |
fsum nfib prev sum | |
else | |
printfn "nfib %i, sum %i" nfib (sum + nfib) | |
fsum nfib prev (sum + nfib) | |
//simlify the task a little, start from 31 | |
fsum 2 1 2 | |
//problem_2 1000000 | |
//Array.fold(fun acc e -> acc + e) 0 [|1; 2; 3; 5; 8; 13; 21; 34; 55; 89|];; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment