forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSieve_of_Eratosthenes.c
55 lines (47 loc) · 1.13 KB
/
Sieve_of_Eratosthenes.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//Sieve of Eratosthenes
#include <stdio.h>
#include <stdlib.h>
#define ll long long
void Sieve(ll n)
{
// Creating an array for storing prime factors.
// and if the i is not prime, mark it as 0 else 1
ll primefac[n + 1];
// Initialising all elements as 1
for(ll i = 2; i <= n; i++)
primefac[i] = 1;
for(ll i = 2; i * i <= n; i++)
{
// If prime[i] is marked 1, then it is a prime.
if(primefac[i] == 1)
{
// Marking all multiples of i as 0
for(int j = i * i; j <= n; j += i)
primefac[j] = 0;
}
}
// Printing all the prime numbers
for(ll i = 2; i <= n; i++)
if(primefac[i])
{
printf("%lld ", i);
}
}
// Main method
int main()
{
ll num;
printf("Enter the number: ");
scanf("%lld", &num);
printf("Below are the prime numbers smaller than or equal to %lld \n", num);
Sieve(num);
return 0;
}
/*
Input:
25
Output:
Enter the number:
Below are the prime numbers smaller than or equal to 25
2 3 5 7 11 13 17 19 23
*/