This C++ program implements a set of number theory utilities related to prime numbers. It allows users to input an integer and:
- Generate all prime numbers less than the input using the Sieve of Eratosthenes.
- Check if the number is prime using the Miller-Rabin primality test.
- Return all prime factors (and the number itself).
- Efficient computation of all primes less than
n. - Probabilistic primality testing via the Miller-Rabin algorithm.
- Prime factor identification using precomputed primes.
- User Input: Prompts for an integer
n. - Sieve of Eratosthenes: Computes all primes
< n. - Miller-Rabin: Tests if
nis likely prime usingk=100rounds. - Prime Factors: Finds all prime factors of
nfrom the sieve (and includesnitself). - Output:
- List of all primes less than
n - Primality result (
primeorcomposite) - Prime factors of
n
- List of all primes less than
Enter number:
30
2 3 5 7 11 13 17 19 23 29
composite
2 3 5 30
main.cpp– Main program source code
- The Miller-Rabin test is probabilistic; using
k = 100gives high confidence. - The
primeFactorsfunction only checks divisibility by primes less thann, then appendsnat the end.
To compile and run:
g++ -std=c++17 main.cpp -o prime_utils
./prime_utils