import calculator
class Calculator(calculator.Calculator):
def add(self, a: int, b: int) -> int:
return a + b
def subtract(self, a: int, b: int) -> int:
return a - b
def multiply(self, a: int, b: int) -> int:
return a * b
def divide(self, a: int, b: int):
if b == 0:
return calculator.Err("Division by zero")
return calculator.Ok(a // b)
def fibonacci(self, n: int) -> int:
if n <= 1:
return n
a, b = 0, 1
for _ in range(2, n + 1):
a, b = b, a + b
return b
class AdvancedMath(calculator.AdvancedMath):
def factorial(self, n: int):
if n < 0:
return calculator.Err("Factorial undefined for negative numbers")
if n > 20:
return calculator.Err("Factorial too large (would overflow)")
result = 1
for i in range(2, n + 1):
result *= i
return calculator.Ok(result)
def is_prime(self, n: int) -> bool:
if n < 2:
return False
if n == 2:
return True
if n % 2 == 0:
return False
for i in range(3, int(n**0.5) + 1, 2):
if n % i == 0:
return False
return True
def gcd(self, a: int, b: int) -> int:
while b:
a, b = b, a % b
return a