程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Finding the maximum common divisor and minimum common multiple prime of any number of parameters in Python

編輯:Python

Ideas

Judge whether the parameter is legal :

  1. At least two parameters are required ( Judge whether the length is greater than 2)
  2. Accept only positive integers ( Traverse all parameters 》 Judge whether the type is integer Is it less than or equal to zero )
  3. Do not conform to the : Return to the prompt message < character string >
  4. 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 :

  1. Whether the smallest parameter can directly become the result ( Sort from small to large Result set to minimum )
  2. Iterate over each parameter and determine whether the result can be divisible
    1. Judge whether the result of parameter modulus is not zero Assign the modulus to the result , To iterate
  3. 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 : 

  1. Whether the largest parameter can directly become the result ( Sort from small to large The result is set to the maximum )
  2. Iterate over each parameter and determine whether the result can be divisible
    1. 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
  3. 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 : 

  1. Define built-in functions Prime or not
    1. from 2 Traverse to the parameter -1
    2. Not divisible returns False
    3. Can be divided function else return True
  2. The length is 1 when : Output a string containing information by subscript (False Subscript to be 0,True Subscript to be 1)
  3. 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))


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved