-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.py
More file actions
44 lines (30 loc) · 802 Bytes
/
calculator.py
File metadata and controls
44 lines (30 loc) · 802 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
def multiply(a, b):
return a * b
def add(a, b):
return a + b
def subtract(a, b):
return a - b
def divide(a, b):
return a / b
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