Welcome, guest | Sign In | My Account | Store | Cart
# 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)

Diff to Previous Revision

--- revision 3 2013-08-10 03:27:11
+++ revision 4 2013-08-10 03:40:02
@@ -14,7 +14,10 @@
     nifty if you are into working with prime numbers.
     """
     factorsDict={}
-    while num>1:
-        factorsDict[num//p]=num//p
-        num=num//p
-    return sum(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)

History