Start learning free

LESSON 161 OF 348 · LOOPS - CHALLENGES

Print Powers in TypeScript: A while Loop with Multiplication

Practice Read the full explanation

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 = 81
Hints:

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


STEP 1 OF 4
    Ctrl + Enter to check

    TYPESCRIPT · LOOPS - CHALLENGES

    Print Powers in TypeScript, explained

    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

    1. Declare power as a number and set it to 1. This is the first power and the value printed first.
    2. Write the loop with the condition power <= B. The comparison is "less than or equal" because a power equal to B must be printed too.
    3. Print power with console.log inside the loop.
    4. Move on with power *= A, which is short for power = 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

    • B smaller than 1. With B = 0 the first check, 1 <= 0, is false, so nothing is printed. That is correct, because even A⁰ = 1 is larger than B.
    • B = 1. For A of 2 or more, only 1 is printed, because the next power is already larger than B.
    • Forgetting the update line. The loop then runs forever. Whenever you write a while, ask what changes the condition.
    • Using += by mistake. Adding A would print multiples of A, not powers.
    • A = 0 or A = 1. The statement allows them, but with this loop power never grows past 1 (for A = 1) or becomes 0 forever (for A = 0), so with B >= 1 it 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.

    Keep going with TypeScriptAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free