Created
May 18, 2012 12:57
-
-
Save mrklein/2725118 to your computer and use it in GitHub Desktop.
Project Euler #65
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
#load "nums.cma";; | |
open Big_int;; | |
open List | |
let p a n = | |
let rec p_inner acc m = | |
let pn k = | |
let an = big_int_of_int (nth a (k - 2)) | |
in | |
let pn_1 = nth acc (k - 1) | |
in | |
let pn_2 = nth acc (k - 2) | |
in | |
add_big_int (mult_big_int an pn_1) pn_2 | |
in | |
if m = (n + 2) then | |
pn m | |
else | |
p_inner (acc @ [pn m]) (m + 1) | |
in | |
p_inner [zero_big_int; unit_big_int] 2;; | |
let rec gen_e_fraction n = | |
if n = 0 then | |
[2] | |
else | |
(gen_e_fraction (n - 1)) @ [1; 2*n; 1] | |
let ea = gen_e_fraction 100;; | |
let sum_digits n = | |
let rec i_sum_digits acc m = | |
if eq_big_int m zero_big_int then | |
acc | |
else | |
let (q, r) = quomod_big_int m (big_int_of_int 10) | |
in | |
i_sum_digits (add_big_int acc r) q | |
in | |
i_sum_digits zero_big_int n;; | |
let p_100 = p ea 99;; | |
Printf.printf "%s -> %s\n" (string_of_big_int p_100) | |
(string_of_big_int (sum_digits p_100));; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment