Professional Documents
Culture Documents
Optimized Trial Divisions For Finding Prime Factors
Optimized Trial Divisions For Finding Prime Factors
NUMBER THEORY
c Steven & Felix, NUS
#include <bitset> // compact STL for Sieve, more efficient than vector<bool>!
ll _sieve_size; // ll is defined as: typedef long long ll;
bitset<10000010> bs; // 10^7 + small extra bits should be enough for most prime-related problems
vi primes; // compact list of primes in form of vector<int>
// in int main()
sieve(10000000); // can go up to 10^7
printf("%d\n", isPrime(5915587277)); // 10 digit prime
In number theory, we know that a prime number N only have 1 and itself as factors but a composite
numbers N , i.e. the non-primes, can be written uniquely it as a multiplication of its prime factors.
That’s it, prime numbers are multiplicative building blocks of integers. For example, N = 240 =
2 × 2 × 2 × 2 × 3 × 5 = 24 × 3 × 5 (the latter form is called prime-power factorization).
A naı̈ve algorithm generates a list of primes (e.g. with sieve) and check how many of them can
actually divide the integer N – without changing N . This can be improved!
A better algorithm utilizes a kind of Divide and Conquer spirit. An integer N can be expressed
as: N = P F × N , where P F is a prime factor and N is another number which is N/P F – i.e.
we can reduce the size of N by taking out its factor P F . We can keep doing this until eventually
N = 1. Special case if N is actually a prime number. The code template below takes in an integer
N and return the list of prime factors – see code below.
vi primeFactors(int N) {
vi factors; // vi "primes" (generated by sieve) is optional
int PF_idx = 0, PF = primes[PF_idx]; // using PF = 2, 3, 4, ..., is also ok.
while (N != 1 && (PF * PF <= N)) { // stop at sqrt(N), but N can get smaller
while (N % PF == 0) { N /= PF; factors.push_back(PF); } // remove this PF
PF = primes[++PF_idx]; // only consider primes!
}
if (N != 1) factors.push_back(N); // special case if N is actually a prime
return factors;
}
96