← Back to Recursion
Question 624
Fast Exponent By Squaring
Fast Exponent By Squaring
Write exponent_fast(base: int, exp: int) -> int that returns base raised to exp (exp of 0 or more) by repeated squaring, so the number of recursive calls grows with the number of binary digits in exp rather than with exp itself: an exponent of 0 gives 1; otherwise store exponent_fast(base, exp // 2) in a variable half, return half * half when exp is even (test with the remainder operator %), and return half * half * base when exp is odd. Do NOT write a main block or any top-level test code; the tests construct the calls. Input: two lines, the base then the exponent, no prompts, which the tests read and pass to your function before printing the value it returns.
Expected Output:
1024= newline. The output ends with a newline — print one after the last line.
Hints4 available
Sign in to use hintsTopics:
RecursionOperators
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Ctrl+Space to expand shortcuts (pr, inp, def)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.