After the Python core programming lesson, exercise one

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.