A collection of exercises for Python beginners(Q32.Decomposition prime factor)

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=2
1111
243=3
3333
244=2
261
245=5
77
246=2
341
247=13
19
248=22231
249=3
83
250=255*5

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值