Given two non-negative integers A and B, print to the console all numbers less than or equal to B that are powers of A
Powers of a number are: A0, A1, A2, etc.
An = A multiplied by itself n times
A0 = 1
A1 = A
A2 = A * A
A3 = A * A * A
A4 = A * A * A * A
etc.
Example:
Input: A = 3, B = 100 Output: 1 3 9 27 81 Explanation: 30 = 1 31 = 3 32 = 9 33 = 27 34 = 81Hints:
We start with the integer
1. We should repeatedly do some instructions (print it then multiply it by A) as long as one condition is satisfied (current power <= B). Sounds familiar?
We should use a variable
power, initially equal to 1, in order to store the current power.
And we should use a while loop to repeatedly check if power <= B and if so, print and update power.
How will the code look like?
power = 1
while power <= B:
print(power)
power *= A
Walkthroughs of the solution. Try the steps first, then watch to compare approaches.
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check