Prime factorization - list

前端 未结 17 784
说谎
说谎 2020-11-27 05:06

I am trying to implement a function primeFac() that takes as input a positive integer n and returns a list containing all the numbers in the prime

相关标签:
17条回答
  • 2020-11-27 05:31

    You can use sieve Of Eratosthenes to generate all the primes up to (n/2) + 1 and then use a list comprehension to get all the prime factors:

    def rwh_primes2(n):
        # http://stackoverflow.com/questions/2068372/fastest-way-to-list-all-primes-below-n-in-python/3035188#3035188
        """ Input n>=6, Returns a list of primes, 2 <= p < n """
        correction = (n%6>1)
        n = {0:n,1:n-1,2:n+4,3:n+3,4:n+2,5:n+1}[n%6]
        sieve = [True] * (n/3)
        sieve[0] = False
        for i in xrange(int(n**0.5)/3+1):
          if sieve[i]:
            k=3*i+1|1
            sieve[      ((k*k)/3)      ::2*k]=[False]*((n/6-(k*k)/6-1)/k+1)
            sieve[(k*k+4*k-2*k*(i&1))/3::2*k]=[False]*((n/6-(k*k+4*k-2*k*(i&1))/6-1)/k+1)
        return [2,3] + [3*i+1|1 for i in xrange(1,n/3-correction) if sieve[i]]
    
    def primeFacs(n):
        primes = rwh_primes2((n/2)+1)
        return [x for x in primes if n%x == 0]
    
    print primeFacs(99999)
    #[3, 41, 271]
    
    0 讨论(0)
  • 2020-11-27 05:32
    def factorize(n):
      for f in range(2,n//2+1):
        while n%f == 0:
          n //= f
          yield f
    

    It's slow but dead simple. If you want to create a command-line utility, you could do:

    import sys
    [print(i) for i in factorize(int(sys.argv[1]))]
    
    0 讨论(0)
  • 2020-11-27 05:32

    Simple way to get the desired solution

    def Factor(n):
        d = 2
        factors = []
        while n >= d*d:
            if n % d == 0:
                n//=d
                # print(d,end = " ")
                factors.append(d)
            else:
                d = d+1
        if n>1:
            # print(int(n))
            factors.append(n)
        return factors
    
    0 讨论(0)
  • 2020-11-27 05:36
    def get_prime_factors(number):
        """
        Return prime factor list for a given number
            number - an integer number
            Example: get_prime_factors(8) --> [2, 2, 2].
        """
        if number == 1:
            return []
    
        # We have to begin with 2 instead of 1 or 0
        # to avoid the calls infinite or the division by 0
        for i in xrange(2, number):
            # Get remainder and quotient
            rd, qt = divmod(number, i)
            if not qt: # if equal to zero
                return [i] + get_prime_factors(rd)
    
        return [number]
    
    0 讨论(0)
  • 2020-11-27 05:36
    def prime_factors(num, dd=2):
        while dd <= num and num>1:
            if num % dd == 0:
                num //= dd
                yield dd
            dd +=1
    

    Lot of answers above fail on small primes, e.g. 3, 5 and 7. The above is succinct and fast enough for ordinary use.

    print list(prime_factors(3))

    [3]

    0 讨论(0)
提交回复
热议问题