MSK
发表于 2017-8-8 21:56:43
def mab(t):
result = []
res = '%d=' % t
i = 2
result= []
while True:
if t == 1:
break
if not (t%i):
result.append(i)
t /= i
else:
i += 1
#拼接
temp = ''
for i in result:
temp += str(i)
return ''.join()
木一
发表于 2017-8-20 21:37:11
n = int(input('请输入需要分解的数:'))
list1 = []
def fp(n):
i = 2
while n % i != 0:
i += 1
list1.append(i)
n /= i
if n != 1:
fp(n)
fp(n)
result = '%d='%n
for each in list1:
result = result + '%d*'%each
result = result[:-1]
print(result)
{:10_243:}
gausser
发表于 2017-9-12 23:12:47
def Decomposing_Factor(number):
Factor_List = []
Min_Prime = 2
while(number > 0):
if 1 == number:
Factor_List.append(number)
print "1 decomposing factor is 1."
break;
if Min_Prime == number: # Description the decomposing_factor process ends
Factor_List.append(number)
break;
else:
if number % Min_Prime == 0:# Divisible 2, this number is even
number = number / Min_Prime
Factor_List.append(Min_Prime)
else:
Min_Prime += 1
return Factor_List
number = int(raw_input("Enter a Positive number: "))
Factor = []
result = str(number) + "="
if number > 0:
Factor = Decomposing_Factor(number)
for each in Factor:
result = result + '%d*' % each
result = result[:-1]
print result
8位数字就比较慢
张大象
发表于 2017-9-14 16:28:17
def fac1(n):
result = []
result.extend()
def fac(n):
for i in range(2,n+1):
if n % i == 0:
if n == i:
result.append(i)
for i in result:
print(i,end='')
break
else:
result.append(i)
result.append('*')
return fac(n//i)
fac(n)
fac1(100)
驻火蚁
发表于 2017-9-28 23:43:29
n=int(input())
if n == 1:
print (n,'=',n)
else:
print (n,'=',end=' ')
for k in range(2,n+1):
while n%k==0 :
print (k,end='')
n=n/k
if n>1:
print ('*',end='')
驻火蚁
发表于 2017-9-29 00:15:46
for i in range(1,100000):
n=int(input('请输入一个整数:'))
if n == 1:
print (n,'=',n)
else:
print (n,'=',end=' ')
for k in range(2,n+1):
while n%k==0 :
print (k,end='')
n=n/k
if n>1:
print ('*',end='')
可以无限循环输入,本来想用while循环呢,但是搞不出来,哪位大神可以帮忙编一下while循环的{:5_100:}
朔夜
发表于 2017-10-16 15:55:43
m=[]
def zs(n):
for x in range(2,n+1):
if n%x==0:
m.append(x)
mix=x
if n==mix:
return m
if n!=mix:
return zs(int(n/x))
zs(50)
print(m)
有什么意见或问题请大佬指正
石小龙
发表于 2017-11-9 18:00:53
n=int(input('请输入一个正整数:'))
for i in range(2,n+1):
if n>=i:
while n%i==0:
n=n/i
print(i)
新手·ing
发表于 2017-11-9 18:01:20
石小龙 发表于 2017-11-9 18:00
刷题么。
Python玲玲
发表于 2017-11-21 22:08:26
n = int(input('输入正整数:'))
k = 2
list1 = []
while k:
if n == k:
list1.append(k)
break
if n != k:
if n%k == 0:
list1.append(k)
n /= k
else:
k += 1
print(list1)
shigure_takimi
发表于 2017-12-1 17:35:47
本帖最后由 shigure_takimi 于 2017-12-1 19:30 编辑
def isPrime(n):
if n < 2:
return False
elif n == 2:
return True
else:
for i in range(2, int(n**0.5)+1):
if n%i == 0:
return False
else:
return True
def allPrimeLessThan(n): # 小于n的所有质数
allPrimeList = []
for i in range(2, n+1):
if isPrime(i):
allPrimeList.append(i)
return allPrimeList
def splitNumberByPrime(n):
number = n
if n < 2:
print('%s小于2,无法分解!' % n)
elif isPrime(n):
print('%s是质数' % n)
else:
allFactor = []
allPrime = allPrimeLessThan(n)
while n != 1:
for i in allPrime:
if n%i == 0:
allFactor.append(i)
n = n // i
print(number,'=', ' * '.join())
splitNumberByPrime(90)
splitNumberByPrime(1)
splitNumberByPrime(2)
splitNumberByPrime(3)
splitNumberByPrime(150)
Result:
90 = 2 * 3 * 3 * 5
1小于2,无法分解!
2是质数
3是质数
150 = 2 * 3 * 5 * 5
##以上方法很笨,很大的数就很费时间。
## 楼上的Python玲玲的方法就很好,简洁高效,学习了。
n = int(input('输入正整数:'))
k = 2
list1 = []
while k:
if n == k:
list1.append(k)
break
if n != k:
if n%k == 0:
list1.append(k)
n /= k
else:
k += 1
print(list1)
SylarPu
发表于 2017-12-1 18:14:24
仰望楼上的大佬们互秀操作
z1446773686
发表于 2018-2-13 13:47:22
def F_J_Z_Y_S(n):
k = 2
number = []
while( k < int(n/2)+2):
while(n % k != 0):
k += 1
print(k)
if(n % k ==0) or n / k == 1:
number.append(k)
n = n / k
print(n)
#print(k)
return number
print(F_J_Z_Y_S(90))
运行结果如下:
45.0
3
15.0
5.0
4
5
1.0
{:10_266:}
小强森
发表于 2018-2-18 17:55:54
# 题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5
num=int(input("请输入一个正整数:"))
a=num
b=[]
i=2
while i<a:
if a%i==0:
b.append(i)
a/=i
i=2
continue
i+=1
if i==a:
b.append(i)
c=len(b)
if c!=0:
print('%s='%num,end='')
for j in range(1,c+1):
d=b.pop()
print(d,end='')
if j!=c:
print('*',end='')
ultraji
发表于 2018-2-20 09:13:41
n = int(input())
print(n,end='=')
k = 2
while k != n:
if n%k == 0:
print(k,end='*')
n /= k
else:
k = k+1
print(int(n))
瞬秒爆加速
发表于 2018-3-5 15:02:55
num = int(input())
print(num,end="")
print("=",end="")
a = iter(range(2,num))
while True:
c = next(a)
b = divmod(num,c)
if b ==1 and b==0:
print(c,end="")
break
if b == 0:
print(c,end="")
print("*",end="")
a = iter(range(2,b+1))
皮皮小发
发表于 2018-3-5 15:59:18
新手·ing 发表于 2017-3-28 18:09
我的解答!!!
如果整数是120,这样输出的是2*3*4*5,然而结果应该是2*2*2*3*5啊,怎么解释
诸葛靓仔
发表于 2018-3-22 22:06:39
list1 = []
number = int(input("number:"))
i=2
while number != i :
while number % i ==0:
list1.append(i)
number = int(number / i)
i+=1
list1.append(number)
print(list1)
checkily
发表于 2018-3-29 00:14:36
本帖最后由 checkily 于 2018-3-29 00:17 编辑
为了输出符合要求,绞尽脑汁了
from functools import reduce
def permit(n):
if n == 1:
return '%d=%s' % (reduce(lambda x, y: x*y, lis), '*'.join(str(i) for i in lis))
else:
for i in range(2, n+1):
if not n % i:
lis.append(i)
return permit(n // i)
if __name__ == '__main__':
lis = []
print(permit(90))
tsembrace
发表于 2018-4-7 15:50:47
#将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。
import math
def isPrime(n):
if n==2:
return True
else:
x=int(math.sqrt(n))
for i in range(2,x+1):
if n%i==0:
return False
return True
def primelist(n):
if isPrime(n):
return str(n)
else:
for i in range(2,int(math.sqrt(n)+1)):
if n%i==0:
break
return str(i)+'*'+primelist(n//i)
print('90 = '+primelist(90))