Problem overview
The function printPowers receives two non-negative integers, A and B. It prints every power of A that is less than or equal to B, one per line, in increasing order. The powers of A are A⁰ = 1, A¹ = A, A² = A * A, and so on. The function prints its result and returns nothing.
For A = 3 and B = 100 the powers are 1, 3, 9, 27, 81 and then 243, which is above 100. So the output is:
1
3
9
27
81
Approach: multiply and test
You don't need an exponent or a power function. Start with the smallest power, 1, and get each next power by multiplying the current one by A. As long as the current power is at most B, print it and move on. The first time it exceeds B, every later power is larger still, so you can stop.
This is a loop whose number of iterations isn't known in advance. It depends on the values, not on a count. In many languages that calls for while. Go has no while, and uses for with only a condition instead.
Step by step
- Start at 1. Declare
powerwith:=and give it the value 1, which isA⁰. Every power list starts there, even whenAis large. - Loop on a condition. Write a
forthat has only a condition after the keyword, herepower <= B. There is no initialiser and no post statement. This is Go's version of awhileloop. - Print the current power with
fmt.Println. The check has just passed, so this value belongs in the output. - Advance to the next power. Multiply
powerbyAwith*=. Without this step the condition never changes and the loop runs forever.
The solution in Go
func printPowers(A int, B int) {
power := 1
for power <= B {
fmt.Println(power)
power *= A
}
}
Some Go details behind these six lines:
- One loop keyword. Go's
forhas three forms.for i := 0; i < n; i++is the classic one,for cond { }is thewhilereplacement used here, andfor { }with nothing loops until abreakorreturn. - No parentheses, required braces. The condition isn't wrapped in parentheses, and the braces are mandatory, even for a one-line body.
:=declares and infers.power := 1creates a variable of typeint. Inside the loop,power *= Auses plain assignment, because the variable already exists. Writing:=there would be an error, or would create a new variable that shadows the old one if it appeared inside a nested block.- Printing needs
fmt.fmt.Printlncomes from thefmtpackage, which a complete Go file must import.Printlnends each value with a newline, which gives one number per line. - Integer size. An
intis 64 bits here, and overflow wraps around silently instead of raising an error. Inputs of normal size stay far from that limit.
Complexity
Time: O(log B) for A ≥ 2. Each iteration multiplies power by at least 2, so the loop can run only about log₂(B) times before power passes B. With B = 100 and A = 3 it runs 5 times.
Space: O(1). The function keeps one variable, power, whatever the size of B.
Edge cases and common mistakes
Bsmaller than 1. WithB = 0the condition1 <= 0fails at once and nothing is printed. That is right, since the smallest power, 1, is already too large.Bequal to a power. ForA = 2andB = 8the output ends with 8. The comparison is<=, so a power equal toBis included. Using<would drop it.A = 1andA = 0. WithA = 1,powerstays 1 forever, so for anyBof at least 1 the loop never ends and prints 1 endlessly. WithA = 0,powerbecomes 0 after the first pass and stays there, with the same result. The solution is correct for bases of 2 and up. If your inputs can include 0 or 1, handle them before the loop.- Printing after the multiplication. Swapping the two lines inside the loop prints 3, 9, 27, 81, 243 for the example. The value 1 is lost, and 243, which is above
B, appears. - Forgetting to update. Leaving out
power *= Aprints 1 forever.
Testing
A = 3,B = 100prints 1, 3, 9, 27, 81.A = 2,B = 0prints nothing.A = 5,B = 4prints just 1.