Skip to content

Commit 37f9d34

Browse files
authored
Fastest way to find prime number
1 parent ece09bc commit 37f9d34

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
written by Pankaj Kumar.
3+
country:-INDIA
4+
*/
5+
#include <bits/stdc++.h>
6+
#include <ext/pb_ds/assoc_container.hpp>
7+
#include <ext/pb_ds/tree_policy.hpp>
8+
using namespace std;
9+
using namespace __gnu_pbds;
10+
typedef long long ll ;
11+
typedef vector<ll> vl;
12+
#define speed cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);
13+
// define values.
14+
// #define mod 1000000007
15+
#define phi 1.618
16+
/* Abbrevations */
17+
#define ff first
18+
#define ss second
19+
#define mp make_pair
20+
#define line cout<<endl;
21+
#define pb push_back
22+
#define Endl "\n"
23+
// loops
24+
#define forin(arr,n) for(ll i=0;i<n;i++) cin>>arr[i];
25+
// Some print
26+
#define no cout<<"NO"<<endl;
27+
#define yes cout<<"YES"<<endl;
28+
#define cc ll test;cin>>test;while(test--)
29+
// sort
30+
#define all(V) (V).begin(),(V).end()
31+
#define srt(V) sort(all(V))
32+
#define srtGreat(V) sort(all(V),greater<ll>())
33+
#define printv(v) for(ll i=0;i<ll(v.size());i++){cout<<v[i]<<" ";} line;
34+
// some extra
35+
#define sz(V) ll(V.size())
36+
/* ONLINE JUDGE */
37+
// #ifdef ONLINE_JUDGE
38+
// freopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);
39+
// #endif
40+
// function
41+
42+
ll power(ll x,ll y,ll mod)
43+
{
44+
ll res=1;
45+
// x=x%mod;
46+
while(y>0)
47+
{
48+
if(y%2==1)
49+
{
50+
res*=x;
51+
// res=res%mod;
52+
}
53+
y/=2; x*=x; // x=x%mod;
54+
}
55+
return res;
56+
}
57+
// datatype definination
58+
#define ordered_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
59+
60+
/* ascii value
61+
A=65,Z=90,a=97,z=122
62+
*/
63+
/* -----------------------------------------------------------------------------------*/
64+
65+
ll solve()
66+
{
67+
cout<<"This is to print all prime number smaller of equal to given number."<<endl;
68+
ll n;
69+
cin>>n;
70+
vector<bool> prime(n+1,true);
71+
for(ll i=2;i*i<=n;i++)
72+
{
73+
if(prime[i])
74+
{
75+
for(ll j=i*i;j<n;j+=i)
76+
prime[j]=false;
77+
}
78+
}
79+
cout<<"List of all prime number is: ";
80+
for(ll i=2;i<=n;i++)
81+
{
82+
if(prime[i])
83+
cout<<i<<" ";
84+
}
85+
line;
86+
return 0;
87+
}
88+
89+
int main()
90+
{
91+
speed;
92+
// freopen("input.txt","r",stdin);
93+
// freopen("output.txt","w",stdout);
94+
solve();
95+
}
96+
97+
/* stuff you should look before submission
98+
* int overflow
99+
* special test case (n=0||n=1||n=2)
100+
* don't get stuck on one approach if you get wrong answer
101+
*/

0 commit comments

Comments
 (0)