Added powmod in utility and PowerMod in the sintax and semantics of MiniImp
This commit is contained in:
@ -10,7 +10,7 @@ let program =
|
||||
)
|
||||
;;
|
||||
|
||||
Printf.printf "%d\n" (reduce 100 program)
|
||||
Printf.printf "%d\n" (reduce 1 program)
|
||||
|
||||
(* -------------------------------------------------------------------------- *)
|
||||
(* y not defined program *)
|
||||
@ -57,7 +57,7 @@ Printf.printf "%d\n" (reduce 10 program)
|
||||
;;
|
||||
|
||||
(* -------------------------------------------------------------------------- *)
|
||||
(* hailstone sequence lenght program *)
|
||||
(* hailstone sequence's lenght program *)
|
||||
let program =
|
||||
Main
|
||||
("a",
|
||||
|
||||
@ -28,6 +28,7 @@ and a_exp =
|
||||
| Division of a_exp * a_exp (* a / a *)
|
||||
| Modulo of a_exp * a_exp (* a % a *)
|
||||
| Power of a_exp * a_exp (* a ^ a *)
|
||||
| PowerMod of a_exp * a_exp * a_exp (* a ^ a % a *)
|
||||
| Rand of a_exp (* rand(0, a) *)
|
||||
|
||||
|
||||
@ -118,6 +119,12 @@ and evaluate_a (mem: memory) (exp_a: a_exp) =
|
||||
let exp_a2val = evaluate_a mem exp_a2 in
|
||||
Utility.pow exp_a1val exp_a2val
|
||||
)
|
||||
| PowerMod (exp_a1, exp_a2, exp_a3) -> (
|
||||
let exp_a1val = evaluate_a mem exp_a1 in
|
||||
let exp_a2val = evaluate_a mem exp_a2 in
|
||||
let exp_a3val = evaluate_a mem exp_a3 in
|
||||
Utility.powmod exp_a1val exp_a3val exp_a2val
|
||||
)
|
||||
| Rand (exp_a) -> (
|
||||
Random.int (evaluate_a mem exp_a)
|
||||
)
|
||||
|
||||
@ -28,6 +28,7 @@ and a_exp =
|
||||
| Division of a_exp * a_exp (* a / a *)
|
||||
| Modulo of a_exp * a_exp (* a % a *)
|
||||
| Power of a_exp * a_exp (* a ^ a *)
|
||||
| PowerMod of a_exp * a_exp * a_exp (* a ^ a % a *)
|
||||
| Rand of a_exp (* rand(0, a) *)
|
||||
|
||||
|
||||
|
||||
@ -4,3 +4,10 @@ let rec pow a = function
|
||||
| n ->
|
||||
let b = pow a (n / 2) in
|
||||
b * b * (if n mod 2 = 0 then 1 else a)
|
||||
|
||||
let rec powmod a d = function
|
||||
| 0 -> 1
|
||||
| 1 -> a mod d
|
||||
| n ->
|
||||
let b = (powmod a d (n / 2)) mod d in
|
||||
(((b * b) mod d) * (if n mod 2 = 0 then 1 else a)) mod d
|
||||
|
||||
@ -1 +1,3 @@
|
||||
val pow : int -> int -> int
|
||||
|
||||
val powmod : int -> int -> int -> int
|
||||
|
||||
Reference in New Issue
Block a user