其他分享
首页 > 其他分享> > 分解质因数

分解质因数

作者:互联网

def isPrime(n):
for i in range(2,int(n/2)+1):
if n%i==0:
return False
break
return True
n = int(input())
a = [] #存放质因子
s = n
i = 2
while i <= int(n/2):
if s%i == 0 and isPrime(i):
a.append(i)
s/=i
if isPrime(s): #说明找到合数的最后一个分解质因数
a.append(int(s))
break
i=1 #为了每次从2开始
i+=1
print(a)

'''
#参考答案
import math
number = int(input())
ls = []
def getChildren(num):
isZhishu = True
for i in range(2, int(math.sqrt(1 + num)) + 1): # 多加个1
if num % i == 0 and i != num:
ls.append(i)
isZhishu = False
getChildren(num // i)
break
if isZhishu:
ls.append(num)

getChildren(number)
print(ls)
'''

标签:int,分解,isZhishu,getChildren,num,ls,质因数,append
来源: https://www.cnblogs.com/cyt423017/p/15674379.html