Question
Fast exponentiation The starter code includes a recursive function, powerSlow, to calculate exponents with a non-negative integer power. For example, powerSlow 2 5 produces 32.
Fast exponentiation The starter code includes a recursive function, powerSlow, to calculate exponents with a non-negative integer power. For example, powerSlow 2 5 produces 32. (Trace it yourself to see how it works!) Your task is to create a function powerFast. It looks almost exactly the same as powerSlow, but includes one extra clause (before the otherwise) that will take a shortcut when the exponent is an even number. The shortcut works through an application of this formula: powfast-formula.png So the 2y on the left side of the formula indicates that the exponent is an even number (a multiple of two). You can then take the power using half that exponent, and square the result. This results in substantially fewer multiplications when the exponent is large. You should define your own square function to use in powerFast; one test case requires this. (Note: The answer needs to be in Haskell programming language)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored 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