Title: Exponentiating by squaring
1Exponentiating by squaring
- Raef Aidibi
- CSE 670 Midterm
- Winter 2004
2- - Used for fast computation of large powers
- Compared to the ordinary method of multiplying x
with itself n-1 times, this algorithm uses
only O(lg n) - -Often used to compute powers of Matrices
- - It is equivalent to decomposing the exponent
into a sequence of squares and products
3include ltstdio.hgt int main() int x,n,
result printf("Enter x \n") scanf("d",x)
printf("Enter n \n") scanf("d",n) result
1 while (n ! 0) // if n is odd,
multiply result with x if ((n 2) 1)
result result x x
xx n n/2 printf("result d",
result)
4EXP
5include ltstdio.hgt int main() int x,n,
result printf("Enter x \n")
scanf("d",x) printf("Enter n \n")
scanf("d",n) result 1 while (n ! 0)
// if n is odd, multiply result with x if ((n
2) 1) result result x x
xx n n/2
6After BTN debounce
S2
include ltstdio.hgt int main() int x,n,
result printf("Enter x \n")
scanf("d",x) printf("Enter n \n")
scanf("d",n) result 1 while (n ! 0)
// if n is odd, multiply result with x if ((n
2) 1) result result x x
xx n n/2
Result 1
S8
S3
n0
Check on n
n!0
S7
S4
n is even
N is odd
S5
S6