Python core programming version 2 Chapter 8 exercise continued 1-Python core programming answers-self-developed-

Source: Internet
Author: User

8-6. prime factor decomposition. Compile a function based on the isprime () and getfactors () functions in the previous exercise. It accepts an integer as a parameter and returns a list Of all prime factor values of the integer type. This process is called prime factor decomposition. The product of all factors output by this process should be the original number. Note that duplicate elements may exist in the list. For example, enter 20 and the returned result is [2, 2, 5].
[Answer]
The Code is as follows:

Def isprime (number): switch = True if number <= 1: switch = False for I in range (2, number/2 + 1): if number % I = 0: switch = False return switchdef getfactors (number): factorList = [] for I in range (1, number + 1): if number % I = 0 and isprime (I ): factorList. append (I) return factorList def findPrimeFactors (number): temp = getfactors (number) k = 1 for I in temp: k = k * I if k = number: return temp else: j = number/k return temp + findPrimeFactors (j) number = int (raw_input ("Please input your number... ") if isprime (number): print" You have already input a prime number. "else: factors = findPrimeFactors (int (number) factors. sort () print factors

[Execution result]
Please input your number ... 199876[2, 2, 107, 467]Please input your number ... 7099092[2, 2, 3, 3, 7, 11, 13, 197]Please input your number ... 197You have already input a prime number.Please input your number ... 20[2, 2, 5]

[Unfinished]
Recursion is used in the findPrimeFactors () function. The program is processing a large amount of data, such as 25200000, which takes a long time and is inefficient. In this case, you can replace the range in the isprime () and getfactors () Functions with xrange, which will improve the performance.

 

8-7. Full number. A complete number is defined as the sum of its approx. (excluding itself) and itself. For example, the approximate number of 6 is 1, 2, 3. Because 1 + 2 + 3 = 6, 6 is considered a full number. Compile a function named isperfect (). It accepts an integer as the parameter. If the number is full, 1: No is returned.
[Answer]
The Code is as follows:

def getfactors(number):    factorList = []    for i in range(1, number):        if number % i == 0: factorList.append(i)    return factorListdef isperfect(number):    if sum(getfactors(number)) == number: return True    else: return False    number = raw_input("Please input your number ... ")print number, getfactors(int(number)), isperfect(int(number))

8-8. factorial. The factorial of a number is defined as the product of all numbers from 1 to the number. The class of N is abbreviated as N !. N! = Factorial (N) = 1*2*3 *... * (N-2) * (N-1) * N. So 4! = 1*2*3*4. Write a function, specify N, and return N! .
[Answer]
The Code is as follows:

def factorial(number):    k = 1    for i in range(1, number+1): k = k*i    return knumber = raw_input("Please input your number ... ")print factorial(int(number))
Related Article

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.