Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,32 @@ def subtract(a, b):
def divide(a, b):
return a / b

print("I'm going use the calculator functions to multiply 5 and 6")
x = multiply(5,6)
print(x)

def square(x):
return x * x


def cube(x):
return x * x * x


def square_n_times(number, n):
"""Square `number` repeatedly `n` times and return the sum.

On each iteration, the current value is squared and added to the total.
For example, with number=2 and n=3:
- iteration 1: 2^2 = 4
- iteration 2: 4^2 = 16
- iteration 3: 16^2 = 256
Sum returned: 4 + 16 + 256 = 276

If `n` is 0, returns 0. Expects non-negative integer `n`.
"""
if n < 0:
raise ValueError("n must be a non-negative integer")
result = 0
current = number
for _ in range(n):
current = current * current
result += current
return result