Ideas
Judge whether the parameter is legal :
- At least two parameters are required ( Judge whether the length is greater than 2)
- Accept only positive integers ( Traverse all parameters 》 Judge whether the type is integer Is it less than or equal to zero )
- Do not conform to the : Return to the prompt message < character string >
- accord with : return False
# Parameter input determines whether it is legal
def _notStd(num, length=2, positive=True):
if len(num) < length:
return " At least two parameters are required "
for x in num:
if type(x) != int:
return " Only integers... Are supported "
if positive and x <= 0:
return " Only positive integers are supported "
else:
return False
greatest common divisor :
- Whether the smallest parameter can directly become the result ( Sort from small to large Result set to minimum )
- Iterate over each parameter and determine whether the result can be divisible
- Judge whether the result of parameter modulus is not zero Assign the modulus to the result , To iterate
- The result can divide all the parameters ( If the modulus is zero , Then run else Return results )
# greatest common divisor
def gcd(*num, result=0):
# Judge whether the value is legal
judge = _notStd(num)
if judge:
return judge
if result == 0: # Call for the first time
num = sorted(list(num)) # Sort from small to large
result = num[0] # The default result is the minimum value
for i in num:
if i % result != 0: # i Modulo result is not zero
result = i % result # The result is set to the modulus of these two values
return gcd(*num, result=result) # recursive
else:
return result # Return minimum
Minimum common multiple :
- Whether the largest parameter can directly become the result ( Sort from small to large The result is set to the maximum )
- Iterate over each parameter and determine whether the result can be divisible
- Judge whether the result of parameter modulus is not zero Calculate the least common multiple of this result and this parameter ( Assign the product of the result and the least common multiple of this parameter to the result ), To iterate
- The result can divide all the parameters ( If the modulus is zero , Then run else Return results )
# Minimum common multiple
def lcm(*num, result=0):
# Judge whether the value is legal
judge = _notStd(num)
if judge:
return judge
if result == 0: # Call for the first time
num = sorted(list(num)) # Sort from small to large
result = num[-1] # The default result is the maximum value
for i in num:
if result % i != 0: # Maximum modulus i Not zero
result = i * result // gcd(i, result) # The maximum value is set as the greatest common multiple of the two numbers
lcm(*num, result=result) # recursive
else:
return result # Return maximum
Prime or not :
- Define built-in functions Prime or not
- from 2 Traverse to the parameter -1
- Not divisible returns False
- Can be divided function else return True
- The length is 1 when : Output a string containing information by subscript (False Subscript to be 0,True Subscript to be 1)
- The length is not 1 when : Traverse and run each
# Sum up prime numbers
def isSu(*num):
# Judge whether the value is legal
judge=_notStd(num, length=1)
if judge:
return judge
# Judge whether it is a prime number
def su(x):
for i in range(2, x):
if x % i == 0: return False
else:
return True
# A parameter directly outputs the judgment result
if len(num) == 1:
return [" Sum ", " prime number "][su(num[0])] # False=0 True=1
# Multiple parameters induce the parameters
else:
result = {" prime number ": [], " Sum ": []}
for x in num:
if su(x):
result[" prime number "].append(x)
else:
result[" Sum "].append(x)
return result
Source code
# Parameter input determines whether it is legal
def _notStd(num, length=2, positive=True):
if len(num) < length:
return " At least two parameters are required "
for x in num:
if type(x) != int:
return " Only integers... Are supported "
if positive and x <= 0:
return " Only positive integers are supported "
else:
return False
# greatest common divisor
def gcd(*num, result=0):
# Judge whether the value is legal
judge = _notStd(num)
if judge:
return judge
if result == 0: # Call for the first time
num = sorted(list(num)) # Sort from small to large
result = num[0] # The default result is the minimum value
for i in num:
if i % result != 0: # i Modulo result is not zero
result = i % result # The result is set to the modulus of these two values
return gcd(*num, result=result) # recursive
else:
return result # Return minimum
# Minimum common multiple
def lcm(*num, result=0):
# Judge whether the value is legal
judge = _notStd(num)
if judge:
return judge
if result == 0: # Call for the first time
num = sorted(list(num)) # Sort from small to large
result = num[-1] # The default result is the maximum value
for i in num:
if result % i != 0: # Maximum modulus i Not zero
result = i * result // gcd(i, result) # The maximum value is set as the greatest common multiple of the two numbers
lcm(*num, result=result) # recursive
else:
return result # Return maximum
# Sum up prime numbers
def isSu(*num):
# Judge whether the value is legal
judge=_notStd(num, length=1)
if judge:
return judge
# Judge whether it is a prime number
def su(x):
for i in range(2, x):
if x % i == 0: return False
else:
return True
# A parameter directly outputs the judgment result
if len(num) == 1:
return [" Sum ", " prime number "][su(num[0])] # False=0 True=1
# Multiple parameters induce the parameters
else:
result = {" prime number ": [], " Sum ": []}
for x in num:
if su(x):
result[" prime number "].append(x)
else:
result[" Sum "].append(x)
return result
if __name__ == "__main__":
print(gcd(6, 8))
print(gcd(63, 18))
print(lcm(15, 6, 3))
print(lcm(24, 28))
print(isSu(3, 8, 6, 4, 9))
print(isSu(3))