Python recursive program to prime factorize a number

前端 未结 5 591
庸人自扰
庸人自扰 2021-01-22 14:07

I wrote the following program to prime factorize a number:

import math
def prime_factorize(x,li=[]):
    until = int(math.sqrt(x))+1
    for i in xrange(2,until)         


        
5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-22 14:36

    def primeFactorization(n):
        """ Return the prime factors of the given number. """
        factors = []
        lastresult = n
        while 1:
            if lastresult == 1:
                break
    
            c = 2
    
            while 1:
                if lastresult % c == 0:
                    break
    
                c += 1
    
            factors.append(c)
            lastresult /= c
    
        return factors
    

    is it fine.

提交回复
热议问题