Prime factorization of a factorial

后端 未结 6 1464
陌清茗
陌清茗 2021-01-05 17:38

I need to write a program to input a number and output its factorial\'s prime factorization in the form:

4!=(2^3)*(3^1)

5!=(2^3)*(3^1)*(5^1)
6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-05 18:14

    Every number can be represented by a unique (up to re-ordering) multiplication of prime numbers, called the prime factorization of the number, as you are finding the prime factors that can uniquely create that number.

    2^3=8

    3^1=3

    5^1=5

    and 8*3*5=120

    But this also means that: (2^3)*(3^1)*(5^1) = 120

    It's not saying that 2 occurs 3 times as a digit in the number 120, which it obviously does not, but rather to multiply 2 by 2 by 2, for a total of 3 twos. Likewise for the 3 and 5, which occur once in the prime factorization of 120. The expression which you mention is showing you this unique prime factorization of the number 120. This is one way of getting the prime factorization of a number in Python:

    def pf(number):
        factors=[]
        d=2
        while(number>1):
            while(number%d==0):
                factors.append(d)
                number=number/d
            d+=1
        return factors
    

    Running it you get:

    >>> pf(120)
    [2, 2, 2, 3, 5]
    

    Which multiplied together give you 120, as explained above. Here's a little diagram to illustrate this more clearly:

    enter image description here

提交回复
热议问题