

Large Prime Check Using The Sieve Of Eratosthenes in C++
source link: https://www.journaldev.com/61242/large-prime-check-sieve-of-eratosthenes-cpp
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Large Prime Check Using The Sieve Of Eratosthenes in C++
In this article, we will learn the large prime check using the Sieve of Eratosthenes. For competitive programming, you must be very clear with the concept of Sieve of Eratosthenes. Even if you’re not a competitive programmer, it’s good to have a tight grip on this algorithm. It is very handy to work with prime numbers and related problems. First, we will go through the problem statement and then move toward the algorithm.
Also read: Prime Factorization Using The Sieve Of Eratosthenes in C++
Problem Statement
Given a number as input, check if this number is a prime number or not.
Note: The number can take large values, i.e. 1 <= N <= 1014
For example,
Number: 10
Output: Not a prime number
Number: 97
Output: Prime number
Number: 2147483647
Output: Prime number
Number: 8449771677
Output: Not a prime number
Number: 1324925285823
Output: Not a prime number
Number: 24874529710111
Output: Prime number
Number: 3593569369386677
Output: Not a prime number
Number: 12134345676707
Output: Prime number
Number: 100000000000
Output: Not a prime number
Concept of Large Prime Checks
The problem is really simple to figure out. We know that the maximum size of a sieve that is allowed is 107. We will take advantage of the fact that the factors of a number lie in the range 1 to square_root(num). This method would allow us to check for primes up to 1014. Below are the steps to code the algorithm.
- Initialize a bitset of the size n = 107.
- Initialize a vector primes to store prime numbers.
- First, generate the prime sieve up to 107.
- Once the sieve is ready, we will continue with the steps below.
- Create a function bool is_prime(int num)
- Inside this function, we will have two cases
- The number lie in the range: 0 <= num <= 107
- return b[num] == 1
- Otherwise, we will have to check for the divisors of the number.
- To do this, iterate over all the primes in the range 2 to square_root(num)
- for(int i = 0; primes[i] * primes[i] <= num; i++)
- if(num % primes[i] == 0)
- return false
- if(num % primes[i] == 0)
- Otherwise, return true
- The number lie in the range: 0 <= num <= 107
Large Prime Check Using Sieve Of Eratosthenes C++ Program
#include <iostream>
#include <vector>
#include <bitset>
using
namespace
std;
const
int
n = 10000000;
// this operation takes O(1) time to initialize
// the bitset
bitset <10000000> b;
vector <
int
> primes;
void
sieve()
{
// set all bits
b.set();
b[0] = b[1] = 0;
for
(
long
long
int
i = 2; i <= n; i++)
{
if
(b[i])
{
primes.push_back(i);
for
(
long
long
int
j = i * i; j <= n; j += i)
{
b[j] = 0;
}
}
}
}
bool
is_prime(
long
long
int
num)
{
// here we will have two cases
// 1. the number is in the range
// 0 <= num <= n
if
(num <= n)
return
b[num] == 1;
// 2. num >= n
// we will find the divisors of this number
// using a loop, this loop will
// run in the range 2 to square_root(num)
// if we find any divisor of this number
// we will mark this number as non - prime
for
(
long
long
int
i = 0; primes[i] * (
long
long
)primes[i] <= num; i++)
if
(num % primes[i] == 0)
return
false
;
// return true if it is not divisible by any of the numbers
return
true
;
}
int
main()
{
// precompute the sieve
sieve();
cout <<
"Enter a number"
<< endl;
long
long
int
num;
cin >> num;
if
(is_prime(num))
cout <<
"Prime Number"
<< endl;
else
cout <<
"Not a prime number"
<< endl;
return
0;
}

Output

Conclusion
Today, we learned to check if a large number is prime or not. To program the solution, we used the Sieve of Eratosthenes. The time complexity of this algorithm will be O(Nlog2(log2N) + O(N1/2)), here O(Nlog2(log2N)) is for generating the sieve and O(N1/2) for calculating the divisors. In the end, we coded a C++ program to check the validity of the algorithm. That’s all for today, thanks for reading.
Further Readings
To learn more about data structures and algorithms, please visit the following articles,
https://www.journaldev.com/60732/remove-consecutive-duplicates-from-string-cpp
Recommend
-
38
Lately, on invitation of my right honourable friend Michal , I've been trying to solve some problems from the Euler project and felt the need t...
-
9
RAGANWALD.COM Implementing the Sieve of Eratosthenes with Functional Programming Programming interviews often include a “Fizzbuzz” test,...
-
6
The sieve of Eratosthenes finds all prime numbers up to a given limit. Method The algorithm st...
-
11
In this article, we will learn to implement the Sieve of Eratosthenes using C++. If you’re a competitive programmer, most probably you’d be familiar with this term. But if you’re not, sit back and relax, we’ll see in the following sections wh...
-
12
Prime Factorization Using The Sieve Of Eratosthenes in C++ Filed Under: JavaToday we will learn prime factoriz...
-
7
[JavaScript] Sieve of Eratosthenes April 28, 2018 JavaScript implementation...
-
7
Online Sieve of Eratosthenes Demo via Go and Vue.js May 01, 2018 Onli...
-
21
[Vue.js] Online Sieve of Eratosthenes Demo May 02, 2018
-
7
[Golang] Sieve of Eratosthenes April 17, 2017 Go implementation of
-
3
Benchmark prime sieve speed between vector<char> and vector<bool> ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK