I have to use n-1 multiplications but I am confused about proving the correctness of the algorithm and finding the upper bound. How do I do/show that??
I know 2022 = 20*(100+1)+2
2022 = 2000+20+2 ......
2022 = 2000+2+2+..+2
etc.
I have to use n-1 multiplications but I am confused about proving the correctness of the algorithm and finding the upper bound. How do I do/show that??
I know 2022 = 20*(100+1)+2
2022 = 2000+20+2 ......
2022 = 2000+2+2+..+2
etc.
Copyright © 2021 Jogjafile Inc.
I'm not sure why you are breaking 2022 into additions.
m^nmeans to multiplymby itselfntimes, e.g.:Note 2022 appears
ntimes (this is the definition of raising a number to a power), and there aren-1multiplication operations.For example,
2022^2 = 2022 * 2022. There is one multiplication operation.You can accomplish this in code like this (C# here, but the concept applies to every language):
Note that raising 2022 to even a modestly large value for n will overflow 64-bit integer types. You can still get a good approximation for larger n using floating point types.
As mentioned in the comments, a more efficient but less obvious approach is exponentation by squaring.