Guest User

Untitled

a guest
Jan 23rd, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. (*
  2. Problem 2
  3. 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:
  4. 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
  5. Find the sum of all the even-valued terms in the sequence which do not exceed four million.
  6. *)
  7. let problem_2 bound =
  8. let rec fsum prev preprev sum =
  9. let nfib = prev + preprev
  10.  
  11. if nfib >= bound then sum
  12. else
  13. if nfib % 2 <> 0 then
  14. printfn "nfib %i, sum %i" nfib sum
  15. fsum nfib prev sum
  16. else
  17. printfn "nfib %i, sum %i" nfib (sum + nfib)
  18. fsum nfib prev (sum + nfib)
  19.  
  20. //simlify the task a little, start from 31
  21. fsum 2 1 2
  22.  
  23. //problem_2 1000000
  24. //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