Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I'm trying to write fast exponentiation method using russian peasant algorithm in tail recursion using the Ocaml. But I don't know which part of the
I'm trying to write fast exponentiation method using russian peasant algorithm in tail recursion using the Ocaml. But I don't know which part of the code is wrong...
let fastExp base power- let rec helper base power acc if base -0 then else if power = 0 then 1 (*/Note that this is the base case/*) else if (odd power) then helper base (power-1) (base * acc) (/Here, I have to try to make power gets close to the base case/*) (/Also, update the accumulator as how we want it/*) else let tmphelper base (power/2) (basexacc) in tmp*tmp (*/also trying to make power gets close to the base case b/c we ddn't w he key in the ine of code above is that casi /I think the key in the line of code above is that we are calling the helper function twice so that the accumulator can also be updated twice as mudh/*) in helper base power 1;; let fastExp base power- let rec helper base power acc if base -0 then else if power = 0 then 1 (*/Note that this is the base case/*) else if (odd power) then helper base (power-1) (base * acc) (/Here, I have to try to make power gets close to the base case/*) (/Also, update the accumulator as how we want it/*) else let tmphelper base (power/2) (basexacc) in tmp*tmp (*/also trying to make power gets close to the base case b/c we ddn't w he key in the ine of code above is that casi /I think the key in the line of code above is that we are calling the helper function twice so that the accumulator can also be updated twice as mudh/*) in helper base power 1Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started