Prime factorization - list

前端 未结 17 868
说谎
说谎 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:17

    This is the code I made. It works fine for numbers with small primes, but it takes a while for numbers with primes in the millions.

    def pfactor(num):
    div = 2
    pflist = []
    while div <= num:
        if num % div == 0:
            pflist.append(div)
            num /= div
        else:
            div += 1
    # The stuff afterwards is just to convert the list of primes into an expression
    pfex = ''
    for item in list(set(pflist)):
        pfex += str(item) + '^' + str(pflist.count(item)) + ' * '
    pfex = pfex[0:-3]
    return pfex
    

提交回复
热议问题