Welcome, guest | Sign In | My Account | Store | Cart

Obtains the total number of factors of p in n! for any prime p.

Python, 23 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Author: Samuel J Erickson
# Date: 8/9/2013
# Description: de Polignac's formula gives all factors of p in n! for any prime
# p.


def polignac(num,p):
    """
    input: num can be any positive integer and p and prime number.

    output: Gives the total number of factors of p in num! (num factorial).
    Stated another way, this function returns the total number of factors
    of p of all numbers between 1 and num; de Polignac's formula is pretty 
    nifty if you are into working with prime numbers.
    """
    factorsDict={}
    if num>1 and p==1:
        print("Invalid entry since 1 is the identity.")
    else:
        while num>1:
            factorsDict[num//p]=num//p
            num=num//p
        return sum(factorsDict)

1 comment

Samuel James Erickson (author) 8 years, 3 months ago  # | flag

I forgot to mention that you will want to call getPLargest(num,p) in order to actually obtain the output from polignac's formula.