Skip to content

Commit f8310b7

Browse files
committed
Add examples/zarith
1 parent d8baf27 commit f8310b7

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

examples/zarith/solution.ml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
let fib_zarith n =
2+
let rec fib n a b =
3+
if n = 0 then a else fib (n - 1) b (Z.add a b) in
4+
Z.rem (fib n Z.zero Z.one) (Z.of_int 1000007)
5+
6+
let fib_num n =
7+
let open Big_int in
8+
let rec fib n a b =
9+
if n = 0 then a else fib (n - 1) b (add_big_int a b) in
10+
mod_big_int (fib n zero_big_int unit_big_int) (big_int_of_int 1000007)

examples/zarith/tests.ml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
open OUnit
2+
3+
let suite = [
4+
"fib_zarith tests" >:::
5+
[
6+
"Testing 42" >:: (fun _ -> assert_equal (Z.of_int 912427) (Solution.fib_zarith 42) ~cmp:Z.equal ~printer:Z.to_string);
7+
"Testing 100000" >:: (fun _ -> assert_equal (Z.of_int 584523) (Solution.fib_zarith 100000) ~cmp:Z.equal ~printer:Z.to_string);
8+
"Testing 1000000" >:: (fun _ -> assert_equal (Z.of_int 930254) (Solution.fib_zarith 1000000) ~cmp:Z.equal ~printer:Z.to_string);
9+
];
10+
"fib_num tests" >:::
11+
[
12+
"Testing 42" >:: (fun _ -> assert_equal (Big_int.big_int_of_int 912427) (Solution.fib_num 42) ~cmp:Big_int.eq_big_int ~printer:Big_int.string_of_big_int);
13+
"Testing 100000" >:: (fun _ -> assert_equal (Big_int.big_int_of_int 584523) (Solution.fib_num 100000) ~cmp:Big_int.eq_big_int ~printer:Big_int.string_of_big_int);
14+
"Testing 1000000" >:: (fun _ -> assert_equal (Big_int.big_int_of_int 930254) (Solution.fib_num 1000000) ~cmp:Big_int.eq_big_int ~printer:Big_int.string_of_big_int);
15+
];
16+
]

0 commit comments

Comments
 (0)