Python factor decomposition

Source: Internet
Author: User

Before using Python to solve the problem, let's say, what is the factor decomposition

The so-called factor decomposition is the first to find the number of all the approximate (approximate: A%b = = 0, that is, a can be divisible by B)

For example: 20 of the approximate set is [1, 2, 5, 10, 20]

What about the decomposition of the element factor?

is to start with the smallest number of primes, that is, the divisor to meet two conditions, one is approximate, and the other is prime

So the smallest of the 20 here is 2, so we're going to divide it from 2 and keep it apart until it can't be rounded out:

num = 20

num = NUM/2

num = 10 (num can still be divisible by 2, so try again)

num = NUM/2

num = 5 (num is obviously divided by 2 not divisible)

So next dividend need to go backwards, that is, 5 (and then take a number to divide before the first to determine whether it is a prime, here 5 is a prime number, if not a prime number such as 4 need to continue to take back)

num = NUM/5

num = 1

At this point, the operation is complete. So, we get 20 of the vegetarian factor set to: [2, 2, 5]

So the next step is to use the Python implementation section.

Through the previous parsing process, we can see that we need so two small modules:

1. Determine if the number is a prime, if the number itself is a prime, then directly return 1 and it itself, because the prime can not be decomposed, that is, [1, Num]

2. We need to obtain all the approximate numbers for this number so that we can divide

Here are the implementations of these two sections:

 1  #   Determine if it is a prime number  2  def   IsPrime (num):  3  count = Num/24  while  count > 1:  5  if  num% count == 0:  6  return   False  7  count-= 18  else  :  9  return True 
1 # get all the approximate 2 def getfactors (num): 3     return  for inch if num% x = = 0]

With the two sections above, we can get all the basics, and then we're going to do it.

1 defprimefactor (num):2     ifisprime (num):3         return[1, Num]4factors =getfactors (num)5Retlist = []6consult =Num7      forIinchRange (1, Len (factors)):8         ifconsult = = 1:9              BreakTen          whileTrue: One             ifConsult% factors[i]! =0: A                  Break -             ifIsPrime (Factors[i]): -Consult/=Factors[i] the retlist.append (factors[i]) -             Else: -                  Break -     returnRetlist

OK, the last call to Primefactor can get results, note that the return is a list object oh

Python factor decomposition

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.