天天看點

python3分解質因數_python3 分解質因數

python3 分解質因數

#!/usr/bin/env python

# -*- coding:utf-8 -*-

# Author:Hiuhung Wan

num = int(input("請輸入一個合數:"))

n = num

list1 = [] #存放質因數

for j in range(int(n/2)+1):

for i in range(2,n):

if num % i == 0: #可以整除

list1.append(i)

num = num // i

break

if len(list1) == 0:

print("此數是是質數,請重新輸入另一個數")

exit()

#print(list1)

print(‘%d = ‘%(n),end=‘‘)

for i in range(len(list1)):

if i == len(list1)-1:

print(‘%s‘ % (list1[i]))

else:

print(‘%s * ‘ % (list1[i]),end=‘‘)

效果:

C:\Python36\python.exe D:/Py/1704/day03/分解質因數.py

請輸入一個合數:15741

15741 = 3 * 3 * 3 * 11 * 53

Process finished with exit code 0