Eighth Chapter
8-6. Decomposition of the element factor. Write a function based on the IsPrime () and getfactors () functions in the previous exercise, which takes an integer as a parameter and returns a list of all prime factors for that integer type. This process is called the decomposition of the element factor, and the product of all the factors it outputs should be the original number. Note that there may be duplicate elements in the list. For example, enter 20 and the return result should be [2,2,5].
Here is my code to solve this problem:
1 ImportMath2num = Int (raw_input ('Please input a number:'))3 defgetfactors (num):4Seq = []5 foreachinchRange (2,num+1):6 ifnum% each = =0:7 seq.append (each)8 returnseq9 defIsPrime (i):Ten foreachinchRange (2,int (math.sqrt (i) +1)): One ifI% each = =0: A returnFalse - Else: - Continue the returnTrue -result =getfactors (num) -data = [x forXinchResultifIsPrime (x)] -Myresult =Data + defSolve (data,num): - forJinchData: +Num/=J A ifNum! = 1: atresult =getfactors (num) -data = [x forXinchResultifIsPrime (x)] - myresult.extend (data) - Solve (Data,num) - Else: - PrintSorted (myresult) inSolve (Data,num)
After the Python core programming lesson, exercise one