Skip to content

Instantly share code, notes, and snippets.

@kunishi
Created July 2, 2012 03:52
Show Gist options
  • Save kunishi/3030947 to your computer and use it in GitHub Desktop.
Save kunishi/3030947 to your computer and use it in GitHub Desktop.
ACM International Collegiate Programming Contest, Asia Regional (Tokyo), 1998, Problem A
open List;;
let generate_isprime x =
let rec generate_isprime x =
match x with
1 -> []
| n -> n :: generate_isprime (n-1)
in
rev (generate_isprime x);;
let filter_isprime l =
let rec filter_isprime l =
match l with
[] -> []
| x :: xs ->
let
filtered = filter (fun y -> y mod x != 0) xs
in
x :: filter_isprime(filtered)
in
1 :: filter_isprime(l);;
let rec find_pair n l =
match l with
[] -> []
| x :: xs ->
let
ys = filter (fun z -> z == (n - x)) xs
in
if length ys != 0
then (x, hd(ys)) :: find_pair n xs
else find_pair n xs;;
let count_goldbach n =
length (find_pair n (filter_isprime (generate_isprime n)));;
let get_text filename =
let input = open_in filename in
try
while true do
let
n = int_of_string (input_line input)
in
if n != 0 then print_endline (string_of_int (count_goldbach n))
else ()
done
with
End_of_file -> ();;
Arg.parse [] (fun s -> get_text (s)) ("");;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment