diff --git a/practice/040/answer/impl.ml b/practice/040/answer/impl.ml new file mode 100644 index 0000000000..5bcd6adeef --- /dev/null +++ b/practice/040/answer/impl.ml @@ -0,0 +1,11 @@ +let is_prime n = + let n = abs n in + let rec is_not_divisor d = + d * d > n || (n mod d <> 0 && is_not_divisor (d + 1)) in + n <> 1 && is_not_divisor 2 + +let goldbach n = + let rec aux d = + if is_prime d && is_prime (n - d) then (d, n - d) + else aux (d + 1) + in aux 2 diff --git a/practice/040/answer/test/dune b/practice/040/answer/test/dune new file mode 100644 index 0000000000..efa4aec676 --- /dev/null +++ b/practice/040/answer/test/dune @@ -0,0 +1,2 @@ +(include_subdirs no) +(test (name run) (libraries ounit2 ex)) diff --git a/practice/040/answer/test/run.ml b/practice/040/answer/test/run.ml new file mode 100644 index 0000000000..f415eab075 --- /dev/null +++ b/practice/040/answer/test/run.ml @@ -0,0 +1,3 @@ +module Test = Ex.Make(Ex.Answer) + +let () = OUnit2.run_test_tt_main Test.v diff --git a/practice/040/dune b/practice/040/dune new file mode 100644 index 0000000000..36077c968d --- /dev/null +++ b/practice/040/dune @@ -0,0 +1,2 @@ +(include_subdirs qualified) +(library (name ex) (libraries ounit2)) diff --git a/practice/040/dune-project b/practice/040/dune-project new file mode 100644 index 0000000000..3c48133ad5 --- /dev/null +++ b/practice/040/dune-project @@ -0,0 +1 @@ +(lang dune 3.7) diff --git a/practice/040/ex.ml b/practice/040/ex.ml new file mode 100644 index 0000000000..eccfaf230a --- /dev/null +++ b/practice/040/ex.ml @@ -0,0 +1,21 @@ +open OUnit2 + +module type Testable = sig + val goldbach : int -> int * int +end + +module Make(Tested: Testable) : sig val v : test end = struct + let tests = "Goldbach's conjecture" >::: [ + "goldbach of 4" >:: (fun _ -> + assert_equal (2, 2) (Tested.goldbach 4)); + "goldbach of 28" >:: (fun _ -> + assert_equal (5, 23) (Tested.goldbach 28)); + ] + + let v = "Tests for Goldbach's Conjecture" >::: [ + tests + ] +end + +module Work : Testable = Work.Impl +module Answer : Testable = Answer.Impl diff --git a/practice/040/work/impl.ml b/practice/040/work/impl.ml new file mode 100644 index 0000000000..b1a9590611 --- /dev/null +++ b/practice/040/work/impl.ml @@ -0,0 +1 @@ +let goldbach _ = failwith "goldbach not implemented" diff --git a/practice/040/work/test/dune b/practice/040/work/test/dune new file mode 100644 index 0000000000..efa4aec676 --- /dev/null +++ b/practice/040/work/test/dune @@ -0,0 +1,2 @@ +(include_subdirs no) +(test (name run) (libraries ounit2 ex)) diff --git a/practice/040/work/test/run.ml b/practice/040/work/test/run.ml new file mode 100644 index 0000000000..7f63254fc5 --- /dev/null +++ b/practice/040/work/test/run.ml @@ -0,0 +1,3 @@ +module Test = Ex.Make(Ex.Work) + +let () = OUnit2.run_test_tt_main Test.v