You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
872 B
37 lines
872 B
from .base import BaseCalculator
|
|
import math
|
|
|
|
class AdvancedCalculator(BaseCalculator):
|
|
@staticmethod
|
|
def power(a, b):
|
|
return a ** b
|
|
|
|
@staticmethod
|
|
def sqrt(a):
|
|
if a < 0:
|
|
raise ValueError("负数不能开平方")
|
|
return math.sqrt(a)
|
|
|
|
@staticmethod
|
|
def factorial(n):
|
|
if not isinstance(n, int) or n < 0:
|
|
raise ValueError("阶乘仅支持非负整数")
|
|
if n == 0:
|
|
return 1
|
|
result = 1
|
|
for i in range(1, n + 1):
|
|
result *= i
|
|
return result
|
|
|
|
@staticmethod
|
|
def is_prime(n):
|
|
if n <= 1:
|
|
return False
|
|
if n == 2:
|
|
return True
|
|
if n % 2 == 0:
|
|
return False
|
|
for i in range(3, int(math.sqrt(n)) + 1, 2):
|
|
if n % i == 0:
|
|
return False
|
|
return True |