how to get the prime number in c++ where time complexity is 0(log n)
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;
}
Also in C++:
- Title
- unordered_map c++ insert
- Category
- C++
- Title
- how to find the mode of a vector c++
- Category
- C++
- Title
- multiple words C++ in same
- Category
- C++
- Title
- how to iterate through array in c++
- Category
- C++
- Title
- how to include seld declared header file in c++
- Category
- C++
- Title
- binary tree deletion
- Category
- C++
- Title
- recursion in cpp with reference
- Category
- C++
- Title
- set lower bound c++
- Category
- C++
- Title
- how use global variables instead of local in c++
- Category
- C++
- Title
- empty string c++ value
- Category
- C++
- Title
- c++ convert const char* to LPCWSTR
- Category
- C++
- Title
- compare function in sort c++ stl
- Category
- C++
- Title
- is not a nonstatic data member or base class of class
- Category
- C++
- Title
- how to use a new node c++
- Category
- C++
- Title
- c++ get type name of object
- Category
- C++
- Title
- find in set of pairs using first value cpp
- Category
- C++
- Title
- create a dictionary cpp
- Category
- C++
- Title
- first prime numbers
- Category
- C++
- Title
- free or delete in c++
- Category
- C++
- Title
- getch c++ library
- Category
- C++
- Title
- c++ throw exception
- Category
- C++
- Title
- % operator in c++
- Category
- C++
- Title
- c++ files
- Category
- C++
- Title
- Newton's sqrt in c++
- Category
- C++
- Title
- delete 2d dynamic array c++
- Category
- C++
- Title
- insert elements in array in c++11
- Category
- C++
- Title
- find height of a tree
- Category
- C++
- Title
- shortest path with bfs in c++
- Category
- C++
- Title
- dfs in c++
- Category
- C++
- Title
- char* to int in cpp
- Category
- C++
- Title
- convert char to string - c++
- Category
- C++
- Title
- shuffle vector c++
- Category
- C++
- Title
- mkdir c++
- Category
- C++
- Title
- how to print eachh chars in string data type in c++
- Category
- C++
- Title
- how to turn int into string c++
- Category
- C++
- Title
- UPARAM(ref)
- Category
- C++
- Title
- c++ remove text file
- Category
- C++
- Title
- making random numbers in c++
- Category
- C++
- Title
- C++ user input
- Category
- C++
- Title
- arduino delay millis
- Category
- C++
- Title
- first fit algorithm
- Category
- C++
- Title
- how to iterate trough a vector in c++
- Category
- C++
- Title
- loop c++
- Category
- C++
- Title
- matrix eigen c++ example
- Category
- C++
- Title
- how to sort an array according to another array c++
- Category
- C++
- Title
- c++ loop through array
- Category
- C++
- Title
- c++ initialization list
- Category
- C++
- Title
- C++ remove element from set
- Category
- C++
- Title
- c++ cout int
- Category
- C++
- Title
- how to iterate over unordered_map c++
- Category
- C++
- Title
- c++ wait for user input
- Category
- C++
- Title
- c++ ros subscriber
- Category
- C++
- Title
- struct c++
- Category
- C++
- Title
- class in c++
- Category
- C++
- Title
- split string at index c++
- Category
- C++
- Title
- c++ initialize a vector
- Category
- C++
- Title
- leveling system c++
- Category
- C++
- Title
- convert GLFWwindow* to IntPtr
- Category
- C++
- Title
- how to have a queue as a parameter in c++
- Category
- C++
- Title
- apple and orange hackerrank solution in c++
- Category
- C++
- Title
- goto c++
- Category
- C++
- Title
- c++ declare char
- Category
- C++
- Title
- c++ stack
- Category
- C++
- Title
- variant hold type
- Category
- C++
- Title
- two elements with difference K in c++
- Category
- C++
- Title
- c++ get ascii value of char
- Category
- C++
- Title
- ue4 modular character
- Category
- C++
- Title
- iterate 2d array c++
- Category
- C++
- Title
- self in c++
- Category
- C++
- Title
- c++ method name
- Category
- C++
- Title
- passing reference in c++
- Category
- C++
- Title
- command line options in c++
- Category
- C++
- Title
- find in string c++
- Category
- C++
- Title
- google spreadsheets add two strings
- Category
- C++
- Title
- how to compare two strings lexicographically in c++
- Category
- C++
- Title
- tree traversal c++ in order
- Category
- C++
- Title
- reverse in vector c++
- Category
- C++
- Title
- c++ ternary operator
- Category
- C++
- Title
- how to find absolute value in c++
- Category
- C++
- Title
- what is meaning of 64 bit integer in c++
- Category
- C++
- Title
- how to make loop in c++
- Category
- C++
- Title
- make an x using asterisk c++
- Category
- C++
- Title
- create new file c++
- Category
- C++
- Title
- remove from unordered_set c++
- Category
- C++
- Title
- range of long long in c++
- Category
- C++
- Title
- basic cpp programs
- Category
- C++
- Title
- c++ course
- Category
- C++
- Title
- c++ overloaded == operator
- Category
- C++
- Title
- size of a matrix using vector c++
- Category
- C++
- Title
- cpp pi from acos
- Category
- C++
- Title
- c++ dereference a pointer
- Category
- C++
- Title
- cube mapping sdl
- Category
- C++
- Title
- power in c++
- Category
- C++
- Title
- c++ class constructor
- Category
- C++
- Title
- c++ split at character
- Category
- C++
- Title
- pbds in c++
- Category
- C++
- Title
- function template
- Category
- C++
- Title
- c++ typedef
- Category
- C++
- Title
- memcmp in cpp
- Category
- C++
- Title
- How to find the suarray with maximum sum using divide and conquer
- Category
- C++