Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (*
- 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|];;
Add Comment
Please, Sign In to add comment