san1412
发表于 2020-5-10 19:09:23
num =int(input("请输入一个正整数:"))
a_list = list(range(2,num + 1))#使用range()创建数字列表
for i in range(2,num + 1):
for j in range(2,i):
if i % j == 0:
a_list.remove(i)#remove() 函数用于移除列表中某个值的第一个匹配项
break#break 语句可以跳出 for 和 while 的循环体
print("正整数分解质因数:%d="%num , end = "")
max_one = max(a_list)
min_one = min(a_list)
if num == max(a_list):
print(num)
else:
while num >= min_one:
for g in a_list:
if num % g == 0:
print(g,end = "")
break
num = num / g
if num != 1:
print("*",end = "")
ForPorsche
发表于 2020-5-22 23:45:46
n = int(input('输入一个正整数:'))
print('%d=' % n, end='')
k = 2
list = []
while True:
if k == n:
list.append(str(n))
break
if n > k:
if n % k == 0:
list.append(str(k))
n = n // k
else:
k = k + 1
print('*'.join(list))
nononoyes
发表于 2020-5-30 15:21:05
#将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5
#定义一个方法判断这个数是否是素数
def isPrime(num):
flag = 0
if(num == 2):
return True
else:
for i in range(2 , num // 2 +1):
if num % i == 0:
flag = 1
break
if flag == 0:
return True
else:
return False
#定义num 用于接收输入的数字
#定义result 用于接收分解的质因子
num = int(input("请输入一个数字:"))
result = []
#首先判断是否是质因子,如果是直接输出结果
if isPrime(num):
result =
else:
for i in range(2,num // 2+1):
#只对质数求余数
if isPrime(i):
#对同一个质数反复求余,直到商对于此质数不能整除(会将每轮求余数的商赋值给num)
while (num % i == 0):
num = num // i
result.append(i)
if isPrime(num):
result.append(num)
break
print(result)
请输入一个数字:1800
谢啾啾
发表于 2020-10-29 17:41:43
借鉴了一些别人的想法
import math as m
n = int(input('请输入一个正整数:'))
# 生成比n小的素数列表
list1 = []
list2 = []
prime_num = []
for each in range(2, n):
list1.append(each)
for i in range(2, n):
for j in range(2, int(m.sqrt(i))+1):
if i%j == 0:
list2.append(i)
for each in list1:
if each not in list2:
prime_num.append(each)
# 判断
length = int(len(prime_num))
point = 0
print('%d='%n, end='')
while True:
if point:
break
for i in range(0, length):
if n % prime_num == 0:
print('%d'%prime_num, end='')
if n == prime_num:
point = 1
break
print('*',end='')
n //= prime_num
break
逃兵
发表于 2020-12-18 14:10:09
num = int(input())
lst = []
def zys(num):
if num !=1:
for i in range(2,num+1):
if num %i ==0:
lst.append(i)
num=num//i
break
zys(num)
zys(num)
print(lst)