double-lee
发表于 2018-12-4 10:06:31
def num(n):
num_l =
while n:
for i in range(2,n+1):
if n % i == 0:
if n != i:
num_l.append(i)
n = n // i
break
if n == i:
num_l.append(i)
n = 0
break
length = len(num_l)
print("%d = "%num_l,end="")
for i in range(1,length):
if i != length-1:
print(num_l,'*',end=' ')
if i ==length-1:
print(num_l)
xxt
发表于 2019-1-17 21:15:18
n = int(input('请输入一个正整数:'))
print("%d ="%n,end='')
for k in range(2, 200):
while n!=k:
if n % k == 0:
print(k,end='*')
n= n/k
else:
k= k+1
if n == k:
print(k)
break
北城以北灬
发表于 2019-2-26 10:15:31
def ex9(num):
x = 2
while num != 1:
if num % x == 0:
print(x)
return ex9(num / x)
x += 1
ex9(90)
lwy520
发表于 2019-3-13 11:17:25
def fenjie(all_num):
if all_num>=2:
k=2
while(True):
if all_num % k:
k=k+1
else:
break
return k
else:
return 1
yinshifenjie=[]
all_num=int(input("请输入一个正整数:"))
if all_num==1:
yinshifenjie.append(1)
else:
while(True):
if all_num==1:
break
else:
numout=fenjie(all_num)
yinshifenjie.append(numout)
all_num=all_num/numout
all_num=1
for i in yinshifenjie:
all_num*=i
print(str(all_num)+"的因式分解为: "+str(all_num),end=' = ')
for i in range(len(yinshifenjie)-1):
print(str(yinshifenjie),end=" * ")
print(yinshifenjie[-1])
lwy520
发表于 2019-3-13 12:02:40
新手·ing 发表于 2017-3-28 18:09
我的解答!!!
def qiusushu(all_num):
if all_num >3:
for i in range(2,int(all_num **0.5)+1):
if all_num % i:
i=i+1
else:
print(i,end=" * ")
return qiusushu(all_num//i)
print(all_num)
return -1
else:
print(all_num)
return all_num
all_num=int(input("请输入一个正整数:"))
print(all_num,end=" = ")
qiusushu(all_num)
yu123py
发表于 2019-4-26 20:51:58
这不是一个理想的方案,因为每次使用都必须先运行一下代码。
i = 2
def devi(n):
if n == 2:
print(i)
elif n > i:
if n % i == 0:
print(i)
n = n/i
else:
i += 1
devi(n)
山岂乎不在高
发表于 2019-7-7 21:13:40
import math
def zhishu(n):
for i in range(2,n+1):
if not (n % i):
if i == n:
return
else:
return +zhishu(int(n/i))
def bprime(n):
lista =[]
d = int(math.sqrt(n+1))
for j in range(2,d+1):
if not(n % j):
lista.append(j)
lista += zhishu(int(n/j))
return lista
if j == d:
return 0
if __name__ == "__main__":
for x in range(2,100):
a = bprime(x)
if not a:
print("%d是素数"%x)
else:
print('%d=%s'%(x,'*'.join(str(s) for s in a)))
panheng
发表于 2019-8-8 23:07:47
本帖最后由 panheng 于 2019-8-8 23:13 编辑
写得有点长,交作业后看大神的代码,感觉可以用递归方法。
#题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。
#解法1:先求出小于等于此数的质数加入列表,然后从列表每次取出一个质数循环查找质因数直到其本身为质数。
def answer1(n):
result = "%d = " % n #输出结果固定传入初始数字
list1 = [] #质数数列
list2 = [] #目标质因数数列
for num in range(2,n):
for j in range(2,int(num**0.5)+1):
if num % j == 0:
break
else:
list1.append(num) #求得n以内质数数
conctrl = 1 #定义循环控制变量
while conctrl:
for each in list1:
if n % each == 0:
n = n / each # n重新赋值为分解质因数后结果
list2.append(each) # 加入目标数列
break
else:
conctrl = 0
for each in list2: #定义输出格式
each = str(each)+" * "
result += each
if len(list2) == 1:
print('输入的是一个质数!')
else:
print(result[:-3]) #切片去除最后的星号
if __name__ == '__main__':
n = int(input("请输入一个正整数:"))
answer1(n)
克里斯保罗
发表于 2019-8-26 20:57:22
from math import *
#判断是否是质数
def judge(num):
a = int(sqrt(num))
flag = 0
for each in range(2,a):
if num%each==0:
flag =1
return flag
def main():
num = int(input('请输入一个正整数:'))
print('%d='%num,end='')
list1=[]
for each in range(2,num):
if judge(each):
if (num%each==0):
list1.append(each)
last = list1.pop()
for each in list1:
print('%d*'%each,end='')
print('%d'%last)
if __name__ =='__main__':
main()
克里斯保罗
发表于 2019-8-26 20:58:13
冬雪雪冬 发表于 2017-3-28 22:54
在知乎上看到一个程序,写的不错,大家可以借鉴一下。
看不太懂,直接加一怎么判断是否是素数呀~
Jung
发表于 2019-11-22 13:01:50
import math
X = int(input('please enter an int number:'))
L = []
def fun9(X):
for N in range(2,int(math.sqrt(X))+1):
if((X%N)==0):
L.append(N)
return fun9(X/N)
L.append(int(X))
print(L)
天下一统2014
发表于 2019-12-5 14:57:24
a = input("请输入一个:")
while a.isdigit() != True:
a = input("输入错误,重新输入:")
b = []
d = a
a = int(a)
c = 2
while True:
if a == c:
b.append(str(c))
break
elif a % c == 0:
b.append(str(c)+'*')
a = a / c
else:
c = c+1
e =d+'='
for i in range(len(b)):
e = e+b
print(e)
qq614704680
发表于 2020-2-3 13:38:27
def prime(x):
for i in range(2,x+1):
if i == x:
print(x)
return x
else:
if not x%i:
print(int(i))
return prime(int(x/i))
x = int(input('enter:'))
prime(x)
小白Alvin
发表于 2020-2-10 17:50:11
def prime(k):
A = []
# 从2-k判断有多少个质数
for i in range(2,k+1):
leap = 1
# 判断这个数是否为质数
for j in range(2,int(i**0.5+1)):
if i % j == 0:
leap = 0
break
if leap == 1:
A.append(i)
return A
num = int(input("请输入一个数:"))
B = []
print(num,'=',end=' ')
while num >1:
for i in prime(num):
if num%i == 0:
B.append(i)
num=num//i
break
for j in range(len(B)):
if j==len(B)-1:
print(B)
else:
print(B,'*',end=' ')
ouyunfu
发表于 2020-2-12 05:54:15
def f9(n):
if n==2:
print(n)
else:
print("%d="%n,end='')
k=2
while n>k:
if n%k==0:
print('%d*'%k,end='')
n=n/k
else:
k+=1
print('%d'%n)
return "Well done!"
print(f9(2*3*5*7*11*13*17))
Yedada
发表于 2020-2-25 21:57:56
n = int(input())
n1 = 1.0
while n != n1:
for i in range(2, int(n)+1):
if n % i == 0:
print("{} ".format(i), end="")
n /= i
break
else:
continue
xiaofan1228
发表于 2020-2-29 22:51:13
def factor(x):
n = 2
for i in range(2, int(x+1)):
if x % i == 0:
print(i)
return factor(x/i)
break
while 1:
factor(int(input('Please type in a number for prime factor\n')))
杜若左
发表于 2020-3-5 10:32:31
j = int(input("请输入一个数字:"))
k = j
for t in range(1,j):
for i in range(2,j+1):
if j%i==0:
print('%s*'%i,end='')
j = int(j/i)
break;
print("=",k)
小生献丑
杜若左
发表于 2020-3-5 10:41:47
n=int(input('请输入一个正整数:'))
for i in range(2,n+1):
while n%i==0:
n=n/i
print(i,"*",end='')
把四楼大哥的代码精简优化了一下
君子好逑
发表于 2020-4-7 19:31:14
import math
c=input('请输入一个正整数:')
d=c+'='
c=int(c)
#质数表
zsb=[]
b=[]
zsb.append(2)
zsb.append(3)
for i in range (2,c+1):
a=int(math.sqrt(i))
for each in range(2,a+1):
n=i%each
if (n==0):
break
elif(each==a):
zsb.append(i)
while (c != 1):
for each in zsb:
n=c%each
if(n==0):
b.append(each)
c=c/each
break
b.sort(reverse=True)
m=str(b.pop())
d=d+m
while(b):
m=str(b.pop())
d=d+'*'+m
print(d)