sieve of eratosthenes c++
C++
// C++ program to print all primes smaller than or equal to
// n using Sieve of Eratosthenes
#include <bits/stdc++.h>
using namespace std;
void SieveOfEratosthenes(int n)
{
// Create a boolean array "prime[0..n]" and initialize
// all entries it as true. A value in prime[i] will
// finally be false if i is Not a prime, else true.
bool prime[n+1];
memset(prime, true, sizeof(prime));
for (int p=2; p*p<=n; p++)
{
// If prime[p] is not changed, then it is a prime
if (prime[p] == true)
{
// Update all multiples of p greater than or
// equal to the square of it
// numbers which are multiple of p and are
// less than p^2 are already been marked.
for (int i=p*p; i<=n; i += p)
prime[i] = false;
}
}
// Print all prime numbers
for (int p=2; p<=n; p++)
if (prime[p])
cout << p << " ";
}
// Driver Program to test above function
int main()
{
int n = 30;
cout << "Following are the prime numbers smaller "
<< " than or equal to " << n << endl;
SieveOfEratosthenes(n);
return 0;
}
//sieve of eratosthenes or prime of sieve
#include<iostream>
#include<math.h>
using namespace std;
void primeofsieve(long long int n)
{
long long int arr[n]={};
for(int i=2;i<=sqrt(n);i++)
{
for(long long int j=i*i;j<=n;j+=i)
arr[j]=1;
}
for(long long int i=2;i<=n;i++)
{
if(arr[i]==0)
cout<<i<<" ";
}
}
int main()
{
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
long long int n;
cin>>n;
cout<<"PRIME NUMBERs ARE : ";
primeofsieve(n);
return 0;
}void eratosthenes_sieve(int N){
bool primes[N+1];
memset(primes, true, sizeof(primes));
primes[0] = false;
primes[1] = false;
for (int p=2; p*p<=N; p++){
if (primes[p]){
for (int i=p*2; i<=N; i+=p)
primes[p] = false;
}
}
}
Also in C++:
- Title
- array as parameter c++
- Category
- C++
- Title
- remove from unordered_set c++
- Category
- C++
- Title
- select elements from array C++
- Category
- C++
- Title
- vector concat c++
- Category
- C++
- Title
- arduino falling edge
- Category
- C++
- Title
- cpp pi from acos
- Category
- C++
- Title
- multiset c++
- Category
- C++
- Title
- getline in c++
- Category
- C++
- Title
- c++ base 10 to base 2
- Category
- C++
- Title
- map insert c++
- Category
- C++
- Title
- Newton's sqrt in c++
- Category
- C++
- Title
- c++ convert const char* to LPCWSTR
- Category
- C++
- Title
- c++ empty stream
- Category
- C++
- Title
- C++ string format ctime
- Category
- C++
- Title
- c++ sort
- Category
- C++
- Title
- passing array to function in c++
- Category
- C++
- Title
- command line options in c++
- Category
- C++
- Title
- user input c++
- Category
- C++
- Title
- how to compile opencv c++ in ubuntu
- Category
- C++
- Title
- find in vector in c++
- Category
- C++
- Title
- check for bst
- Category
- C++
- Title
- c++ typedef
- Category
- C++
- Title
- c++ class member initialization
- Category
- C++
- Title
- Combination Sum
- Category
- C++
- Title
- binary representation differ in bits
- Category
- C++
- Title
- how to swap string characters in c++
- Category
- C++
- Title
- Arrays hackerrank solution in c++
- Category
- C++
- Title
- how to inject a dll into a game c++
- Category
- C++
- Title
- how to sort in descending order c++
- Category
- C++
- Title
- time function c++
- Category
- C++
- Title
- how to sort an array c++
- Category
- C++
- Title
- regexp_like oracle c++
- Category
- C++
- Title
- initialize array c++
- Category
- C++
- Title
- how to compare lower case character to uppercase cpp
- Category
- C++
- Title
- how to run c++ file mingw cmd
- Category
- C++
- Title
- how to find the size of a character array in c++
- Category
- C++
- Title
- hobo 8
- Category
- C++
- Title
- how to delete an element in vector pair in cpp
- Category
- C++
- Title
- log base 10 c+_+
- Category
- C++
- Title
- c++ crash windows
- Category
- C++
- Title
- Find the duplicate in an array of N integers.
- Category
- C++
- Title
- how to sort vector in c++
- Category
- C++
- Title
- pass ss tream as parameter c++
- Category
- C++
- Title
- accept the noun and the output of plural c++
- Category
- C++
- Title
- dynamic 2d array c++
- Category
- C++
- Title
- merge sort in c++
- Category
- C++
- Title
- variabili in c++
- Category
- C++
- Title
- how to iterate through array in c++
- Category
- C++
- Title
- peak in c++
- Category
- C++
- Title
- pass by reference c++
- Category
- C++
- Title
- c++ try
- Category
- C++
- Title
- how to concatinate two strings in c++
- Category
- C++
- Title
- first prime numbers
- Category
- C++
- Title
- dfs in c++
- Category
- C++
- Title
- c++ create array
- Category
- C++
- Title
- how to modulo 10^9+7
- Category
- C++
- Title
- filling 2d array with 0 c++
- Category
- C++
- Title
- tree traversal c++ in order
- Category
- C++
- Title
- syntax c++
- Category
- C++
- Title
- Convert binary tree to a doubly linked list
- Category
- C++
- Title
- max three values c++
- Category
- C++
- Title
- what is difference between single inverted and double inverted in programming languages
- Category
- C++
- Title
- git branch in my bash prompt
- Category
- C++
- Title
- static_cast c++
- Category
- C++
- Title
- % operator in c++
- Category
- C++
- Title
- what is atoi in strinf
- Category
- C++
- Title
- templates of templates c++
- Category
- C++
- Title
- else if c++
- Category
- C++
- Title
- C++ pointer arithmetic
- Category
- C++
- Title
- c++ call method in same class
- Category
- C++
- Title
- c++ read file line by line
- Category
- C++
- Title
- loop c++
- Category
- C++
- Title
- c++ unittest in ros
- Category
- C++
- Title
- gfg bottom view of tree
- Category
- C++
- Title
- shortest path with bfs in c++
- Category
- C++
- Title
- uepic games github
- Category
- C++
- Title
- c++ how to loop through a vector but not the last element
- Category
- C++
- Title
- new keyword in cpp
- Category
- C++
- Title
- never gonna give you up lyrics
- Category
- C++
- Title
- if esle in c++
- Category
- C++
- Title
- registering a new QML type
- Category
- C++
- Title
- how to append one vector to another c++
- Category
- C++
- Title
- expected initializer before 'isdigit'|
- Category
- C++
- Title
- iterate 2d array c++
- Category
- C++
- Title
- invalid types int int for array subscript c++
- Category
- C++
- Title
- how to check datatype of a variable in c++
- Category
- C++
- Title
- how to switch to another branch in git
- Category
- C++
- Title
- programa para saber si un numero es primo
- Category
- C++
- Title
- what does map.count() return in c++
- Category
- C++
- Title
- c++ char print fixed
- Category
- C++
- Title
- c++ argv
- Category
- C++
- Title
- generate random double c++
- Category
- C++
- Title
- error: ‘memset’ was not declared in this scope in cpp
- Category
- C++
- Title
- comparing strings c++
- Category
- C++
- Title
- c++ pi
- Category
- C++
- Title
- least number of coins to form a sum
- Category
- C++
- Title
- find number of 1s in a binary cv::mat image
- Category
- C++
- Title
- c++ multidimensional vector
- Category
- C++
- Title
- gta san andreas
- Category
- C++
- Title
- primitive and non primitive data types in c++
- Category
- C++