Menu

Non Tail Recursive Version Shown Class Question Needs Done Using Ocaml Format Q43881867

4. In lecture 11 quickly explained the Russian peasant algorithm for fast exponentiation. In this exercise you have to implem

The non-tail recursive version shown in class

# let even n = (n mod 2) = 0; ; # let odd n = (n mod 2) = 1;; # let rec rpe base power = if base = 0 then 0 else if power = 0

The question needs to be done using OCaml in this format.

(* 04 TODO: Implement tail recursive helper fast_exp_aux. *) let rec fast_exp_aux (base, power, acc) = raise NotImplemented (

4. In lecture 11 quickly explained the Russian peasant algorithm for fast exponentiation. In this exercise you have to implement a tail-recursive version of the algorithm. Everything is with integers in this question so please do not use floating point operations. Our tester will detect whether you used built-in functions, so please don’t try to use them. # let even n = (n mod 2) = 0; ; # let odd n = (n mod 2) = 1;; # let rec rpe base power = if base = 0 then 0 else if power = 0 then i else if (odd power) then base * (rpe base (power – 1)) else let tmp = (rpe base (power/2)) in tmp * tmp; ; val rpe : int -> int -> int = <fun> (* 04 TODO: Implement tail recursive helper fast_exp_aux. *) let rec fast_exp_aux (base, power, acc) = raise NotImplemented (* 04 TODO: Implement fast_exp using fast_exp_aux. *). let fast_exp (base, power) = raise NotImplemented Show transcribed image text 4. In lecture 11 quickly explained the Russian peasant algorithm for fast exponentiation. In this exercise you have to implement a tail-recursive version of the algorithm. Everything is with integers in this question so please do not use floating point operations. Our tester will detect whether you used built-in functions, so please don’t try to use them.
# let even n = (n mod 2) = 0; ; # let odd n = (n mod 2) = 1;; # let rec rpe base power = if base = 0 then 0 else if power = 0 then i else if (odd power) then base * (rpe base (power – 1)) else let tmp = (rpe base (power/2)) in tmp * tmp; ; val rpe : int -> int -> int =
(* 04 TODO: Implement tail recursive helper fast_exp_aux. *) let rec fast_exp_aux (base, power, acc) = raise NotImplemented (* 04 TODO: Implement fast_exp using fast_exp_aux. *). let fast_exp (base, power) = raise NotImplemented

Expert Answer


Answer to The non-tail recursive version shown in class The question needs to be done using OCaml in this format. …

OR