Python exercise question 010: Decomposition prime factor, python prime factor

Source: Internet
Author: User

Python exercise question 010: Decomposition prime factor, python prime factor

[Python exercise question 010]Factorization a positive integer into a prime factor. For example, enter 90 and print 90 = 2*3*3*5.

---------------------------------------------------------------

I thought this was another issue. It took me one and a half hours to solve it!

At first, I thought that if I divide the input integer into a list of numbers one by one, the integer that can be divisible can be considered as a prime factor. But in fact it won't work, because it will be included together with numbers like 4, 6, and 9, and when the prime factor is repeated (such as 12 = 2*2*3 ), will be omitted.

Based on the above considerations, the conversion principle is to divide the input INTEGER (n) by a number list, but it must be divided several times, and each time, once a prime factor exists, the number is immediately removed. Divide the input number by the prime factor and assign it to n again. Then, stop the cycle and enter the next cycle. In this way, the above problems can be solved.

Note: If this number has been divisible by any prime factor, the final n must be one of them after all cycles, it should be included in the list.

The Code is as follows:

N = num = int (input ('enter a number :')) # Use num to retain the initial value f = [] # prime factor list for j in range (int (num/2) + 1 ): # Only half of the number is needed for I in range (2, n): t = n % I # I cannot be n itself if t = 0: # If f. append (I) # indicates that I is the prime factor n = n // I # divided by the prime factor n and then re-enters the judgment. Note that two division numbers are applied, enable n to keep the integer break # immediately after finding one prime factor, to prevent non-prime numbers from entering the prime factor list if len (f) = 0: # If a prime factor does not have print ('the number does not have any prime factor. ') Else: # If there is at least one prime factor f. append (n) # at this time, n has been divisible by a prime factor, and the last n is also a prime factor f. sort () # print ('% d = % d' % (num, f [0]), end = '') for I in range (1, len (f): print ('* % d' % f [I], end = '')

The output result is as follows:

Enter a number: 1482
1482 = 2*3*13*19

Decomposition of prime factors is a relatively basic mathematical problem and should have a very mature solution. I will try to update other people's algorithms later.

 

++

Source: getting started with programming languages: 100 typical examples [Python]

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.