Pollard's P-1 Factoring Algorithm in Plain C
Coding Pollard's 1974 Paper on Factoring Within P-1 Multiplicative Subgroups
1.0 Paper Introduction
Theorems on Factorization and Primality Testing (Pollard, 1974)1 introduces a special-purpose p-1 algorithm for factoring integers composite integers N, into prime factors p, where (p-1) has small prime factors.
Fermat’s Little Theorem permits Pollard’s p-1 algorithm use information about the group’s order to find a factor p of N:
Here’s Pollard’s insight: if a composite N, has a prime factor p, then there exists a d such that:
Let m be the exponent of a, then we can find d if the exponent m is a multiple of p-1 and p-1 has small factors i.e m = c(p-1):
By choosing m as a product of small prime factors, we must get a factor of N because:
1.1 Pollard’s P-1 Algorithm
(Pollard, 1974) is unavailable online, so we follow the outline in (Charest, 2005)2 :
In practice (OisinResearch, 2026)3, one quickly computes a generator g such that:
Then tests for the gcd:
In our case, this works because if p is a prime factor of N, and p-1 divides always divides S.
1.2 Choosing S
S is called the Stage 1 exponent for p-1. This is simply the LCM of all numbers upto a bound B.
Smart researchers found a tree-like shortcut to find S and it involves computing the LCM of the largest prime powers upto the bound B.
S is a pretty large number. For instance, for B = 10, S = 2520.
Another instance, when B = 1000, S is a 1438 bit number.
2.0 Coding Pollard’s P-1
Code is available on GitHub.
First, we use the tree shortcut to find the LCM of all numbers upto a bound:
We write a test function to ensure the result divides all numbers inclusive of the given bound:
If you’re coding along, then for primeBound=1000, you get:
S(1438 bits) :7128865274665093053166384155714272920668358861885893040452001991154324087581111499476444151913871586911717817019575256512980264067621009251465871004305131072686268143200196609974862745937188343705015434452523739745298963145674982128236956232823794011068809262317708861979540791247754558049326475737829923352751796735248042463638051137034331214781746850878453485678021888075373249921995672056932029099390891687487672697950931603520000
Next, we write a terse wrapper for p-1:
2.1 Testing Our Code
We shall test using the Wikipedia example:
We call our function like this:
We find 13 as expected:
3.0 Improvements
There exists a two stage variant. One simply increases the bound such that the algorithm spends more time in this second phase than the first phase.
In this case, the p-1 algorithm will find p if its p-1 prime factors are all less than the Stage 1 bound, except for 1 factor that is smaller than the second bound (Charest, 2005) .
References
Pollard, J. M. (1974). Theorems on Factorization and Primality Testing. Mathematical Proceedings of the Cambridge Philosophical Society, 76(3), 521–528. doi:10.1017/S0305004100049252
OisinResearch. (2006). Sieve2dmono. GitHub repo.

















