Question: Exercise 5 . For cryptographic purposes, we need a function to compute powers: given integers a , n > = 1 , return a n
Exercise For cryptographic purposes, we need a function to compute powers: given integers
a n return a
n a a a where the number of a is n Multiplication is expensive so we
want to use as few of those as possible. Note that the naive approach to compute a
n uses n
multiplications.
Justify that the function pow computes a
n
How many multiplications does it perform, asymptotically, as a function of n
static long pow long a long n
if n return ;
long res pow a n ;
return n res res a : res res ;
Can you rewrite the function pow without using recursion?
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
