Problem overview
The method printPowers(A, B) receives two non-negative integers and prints, one per line, every power of A that is less than or equal to B. The powers are A⁰ = 1, A¹ = A, A² = A * A, and so on. The method returns nothing: its result is what it writes to the console.
With A = 3 and B = 100 the output is 1, 3, 9, 27 and 81, since the next power, 243, is larger than 100.
Approach: walk up the powers
You don't need an exponent or a power function. Each power is the previous one multiplied by A, so keep a single variable that holds the current power. Start it at 1, which is A⁰. While it is still at most B, print it and multiply it by A to move to the next one. The first time the variable passes B, every later power is even larger, so the loop can stop.
The number of repetitions isn't known in advance, which is why a while loop fits better than a for loop here: it runs as long as a condition holds.
Step by step
- Declare
poweras anumberand set it to 1. This is the first power and the value printed first. - Write the loop with the condition
power <= B. The comparison is "less than or equal" because a power equal toBmust be printed too. - Print
powerwithconsole.loginside the loop. - Move on with
power *= A, which is short forpower = power * A. Without this line the condition never changes and the loop prints forever.
The order inside the loop matters: print first, then multiply. If you multiplied first, the value 1 would never be printed, and the last value printed could be greater than B.
The solution in TypeScript
class Solution {
printPowers(A: number, B: number): void {
let power: number = 1;
while (power <= B) {
console.log(power);
power *= A;
}
}
}
The : number annotation after power is optional, because the compiler infers it from the 1. The return type void says the method gives back no value. Both are erased when the code is compiled to JavaScript. Each console.log(power) prints one number on its own line, which gives the format the example shows.
Complexity
After k iterations power equals A^k, and the loop stops once that exceeds B, so it runs about log base A of B times, plus one. For A = 2 and B = 1,000,000 that is only 20 iterations. The time is O(log B) for A ≥ 2, and the space is O(1), because the method keeps one variable.
Edge cases and common mistakes
Bsmaller than 1. WithB = 0the first check,1 <= 0, is false, so nothing is printed. That is correct, because evenA⁰ = 1is larger thanB.B = 1. ForAof 2 or more, only 1 is printed, because the next power is already larger thanB.- Forgetting the update line. The loop then runs forever. Whenever you write a
while, ask what changes the condition. - Using
+=by mistake. AddingAwould print multiples ofA, not powers. A = 0orA = 1. The statement allows them, but with this looppowernever grows past 1 (forA = 1) or becomes 0 forever (forA = 0), so withB >= 1it would not terminate. Real code should guard against these two values, for example by returning early.
Testing
Try printPowers(2, 16), which prints 1, 2, 4, 8 and 16 (note that 16 equals B and is included), and printPowers(5, 1), which prints just 1.