Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions c++/prime-numbers-between-0-n.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// https://practice.geeksforgeeks.org/problems/find-prime-numbers-in-a-range/0

#include <iostream>
using namespace std;
void solve_test()
{
long long n;
cin>>n; // taking the value till we want to get the primes
vector<bool> prime(n+1 , true); //intializing a vector of bool with all initial value true
prime[0] = false; //marking 0 and 1 non-primes
prime[1] = false;
for(int i=2;i<=n;i++){
//if a number is prime then we are marking its multiples as prime
if(prime[i]){
for(int j=i+i;j<=n;j+=i)
prime[j] = false;
}
}
//printing all the prime that are true in the vector
for(int i=0;i<=n;i++){
if(prime[i])
cout<<i<<" ";
}
cout<<endl;
//TC = O(log(log(n))) + O(n)
//SC = O(n)
}

int main()
{
int t;
cin >> t;

while(t--)
solve_test();
return 0;
}
1 change: 0 additions & 1 deletion c++/prime-numbers-in-range.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ bool isPrime(unsigned long int n)
}
return true;
}

void solve_test()
{
unsigned long int a , b, number;
Expand Down