A collection of exercises for Python beginners(Q32)
As a Python beginner, I’ve done a lot of practice. Most of these exercises are very interesting. In order for the other beginners to get to learn the basic usage of the language faster, I have compiled the results of my exercises for your reference. As I am a foreign student, this exercise is written in English. Most of the exercises come from 《C语言初学者趣味编程100例》(贾蓓/郭强/刘占敏). A small number of cases come from the Internet. If any case violates your rights, please tell me, and I will delete it as soon as possible.
Q32. Decomposition prime factor
Question:
Decompose a range of positive integers into the product of prime factors.
Python Code:
def primefactor(n):
list1=[]
list2=[]
y=n
for i in range(2,1000):
j=2
for j in range(2,i):
if i%j==0:
break
else:
list1.append(i)
for m in range(len(list1)):
while n%list1[m]==0:
n=n/list1[m]
list2.append(list1[m])
print(y,end='=')
for x in range(len(list2)-1):
print(list2[x],end='*')
print(list2[len(list2)-1])
primefactor(240)
for k in range(240,251):
primefactor(k)
Running Results:
240=22223*5
240=222235
241=241
242=21111
243=33333
244=2261
245=577
246=2341
247=1319
248=22231
249=383
250=255*5