ocaml: phase 5.1 pow_mod.ml baseline (modular exponentiation, sum = 738639)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 27s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 27s
Fast exponentiation by squaring with modular reduction:
let rec pow_mod base exp m =
if exp = 0 then 1
else if exp mod 2 = 0 then
let half = pow_mod base (exp / 2) m in
(half * half) mod m
else
(base * pow_mod base (exp - 1) m) mod m
Even exponent halves and squares (O(log n)); odd decrements and
multiplies. mod-reduction at each step keeps intermediates bounded.
pow_mod 2 30 1000003 + pow_mod 3 20 13 + pow_mod 5 17 100 = 738639
84 baseline programs total.
This commit is contained in:
@@ -61,6 +61,7 @@
|
||||
"prefix_sum.ml": 66,
|
||||
"pretty_table.ml": 64,
|
||||
"poly_stack.ml": 5,
|
||||
"pow_mod.ml": 738639,
|
||||
"prime_factors.ml": 17,
|
||||
"queens.ml": 2,
|
||||
"quicksort.ml": 44,
|
||||
|
||||
11
lib/ocaml/baseline/pow_mod.ml
Normal file
11
lib/ocaml/baseline/pow_mod.ml
Normal file
@@ -0,0 +1,11 @@
|
||||
let rec pow_mod base exp m =
|
||||
if exp = 0 then 1
|
||||
else if exp mod 2 = 0 then
|
||||
let half = pow_mod base (exp / 2) m in
|
||||
(half * half) mod m
|
||||
else
|
||||
(base * pow_mod base (exp - 1) m) mod m
|
||||
|
||||
;;
|
||||
|
||||
pow_mod 2 30 1000003 + pow_mod 3 20 13 + pow_mod 5 17 100
|
||||
Reference in New Issue
Block a user