Created
January 14, 2010 14:33
-
-
Save pyetras/277205 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
let rev = List.fold_left (fun a x -> x::a) [];; | |
let is_prime x = | |
let rec pom a = | |
if a*a > x then true | |
else | |
if x mod a = 0 then false | |
else pom (a+2) | |
in if x mod 2 = 0 then false | |
else pom 3;; | |
let next_ltps base ten = | |
List.fold_left | |
(fun a x -> | |
(List.fold_left (fun acc base -> | |
let ltp = ten*x + base in if is_prime ltp then ltp::acc else acc) | |
[] (rev base))@a | |
) | |
[] | |
[1;2;3;4;5;6;7;8;9];; | |
let make_tens n = | |
let rec pom a prev n = | |
if n = 0 then a | |
else pom (prev::a) (prev*10) (n-1) | |
in rev (pom [] 10 n);; | |
let ltp = | |
List.fold_left | |
(fun (acc, prev) ten -> let a = next_ltps prev ten in | |
(a@acc, a) | |
) | |
([7;5;3;2],[7;5;3]) | |
(make_tens 8);; | |
let ltp n = | |
let rec pom l n = | |
match l with | |
| (h::tl) -> if n = 0 then h | |
else pom tl (n-1) | |
| [] -> assert false | |
and fst (a, _) = a | |
in pom (rev (fst ltp)) (n - 1);; | |
let n = read_int () in | |
print_int (ltp n);; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment