1 #题目: decomposes a positive integer factorization. For example: Enter 90 and print out 90=2*3*3*5.
Code:
2 import pdb 3 tmp = int (Input (' Please enter a positive integer: ')) 4 print ('%d= '%tmp,end = ') 5 6 i = 2 7 li = [] 8 #pdb. Set_trace () 9 while i > 1: 10 if tmp%i == 0: 11 tmp = tmp/i 12 Li.append (i) 13 i = 1 14 if tmp == 1: 15 i = 0 16 i += 1 17 for x in li[:len (LI) -1]: 18 print ('%d* '% X,end= ') 19 print (li[-1]) ~
Operation Result:
[[email protected] code_100]# python code_14.py Please enter a positive integer: 10001000=2*2*2*5*5*5[[email protected] code_100]# python Code_ 14.py Please enter a positive integer: 6987369873=3*23291[[email protected] code_100]# python code_14.py Please enter a positive integer: 208628064208628064=2*2*2*2 *2*3*3*724403[[email protected] code_100]# python code_14.py Please enter a positive integer: 9090=2*3*3*5[[email protected] code_100]# python code_14.py Please enter a positive integer: 879236872432879236872432=2*2*2*2*17099*3213773[[email protected] code_100]#
Code Explanation:
2 import pdb 3 tmp = int (Input (' Please enter a positive integer: ') ') #接收输入数字 and convert to int mode 4 print ('%d= '%tmp,end = ') #格式化输出 5 6 i = 2 #求质数从2开始除 7 li = [] #定义一个空的列表 8 #pdb. set_ Trace () 9 while i > 1: #开启while循环 10 if tmp%i == 0: # This number is a division from 2, and if the remainder is 0, this number is the prime numbers of TMP 11 tmp = tmp/i #tmp相应的获取除以后的值 12 li.append (i) #将可以把tmp整除的数添加到列表li [] 13 i = 1 #当有一个数字可以整除tmp时, re-start the cycle with I reset 14 if tmp == 1: #当tmp被除等于1时, Indicates that TMP was removed from the 15 i = 0 #通过赋值i = 0, end While Loop 16 i += 1 #每次计算后, i+1, Division test, starting from 2 to the back, the number that can be taken is prime, for example, Must be divisible by 2 before dividing by 4, so there is no case of being removed by a non-prime number 17 for x in li[:len (LI) -1]: #遍历这个li列表的前n-1 numbers 18 print ('%d* '% X,end= ') # Formatted output this n-1 number 19 print (Li[-1]) #格式化输出li的最后一个数, The last three lines of code are basically to achieve the format of all the elements of the Li list in the topic ~
This article is from the "Learning Notes" blog, so be sure to keep this source http://netsyscode.blog.51cto.com/6965131/1745973
"Python" programming language Introduction Classic 100 cases--14