python 将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5
一、参考解法:
while 1: n = int(input('请输入一个整数:')) print('%d='%n,end='') while n>1: for i in range(2,n+1): if n%i==0: n=int(n/i) if n==1: print('%d'%i,end='') else: print('%d*'%i,end='') break print()
二、参考解法:
from math import sqrt while 1: n=int(input('请输入整数:')) print ("%d = " %n , end = '') while 1: for i in range(2,int(sqrt(n)+1)): if n%i==0: print('%d*'%i,end='') n=int(n/i) break else: print(n) break
三、参考解法:
def prime(n): L=[ ] while n>1: for i in range(2,n+1): if n % i ==0: n = int(n/i) L.append(i) break return L while 1: s = input('请输入一个正整数:') if s.isdigit() and int(s)>0: print(s,'=','*'.join([str(x) for x in prime(int(s))]))#*.join(sequence)用*号连接元素序列 else: print('请输入一个正整数:')