forked from gouravthakur39/beginners-C-program-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrimeByEratosthenes.c
28 lines (20 loc) · 854 Bytes
/
PrimeByEratosthenes.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
#include <stdio.h>
#include <stdlib.h>
#define limit 100 /*size of integers array*/
int main(){
unsigned long long int i,j;
int *primes; //pointer created for prime
int z = 1;
primes = malloc(sizeof(int) * limit); //allocating 100 spaces in memory with the starting address to prime pointer
for (i = 2;i < limit; i++) //initializing the 100 memory spaces
primes[i] = 1;
for (i = 2;i < limit; i++) //Filling the array with prime numbers as per the method proposed by seive of eratosthenes
if (primes[i])
for (j = i;i * j < limit; j++)
primes[i * j] = 0;
printf("\nPrime numbers in range 1 to 100 are: \n");
for (i = 2;i < limit; i++) //Printing the prime numbers in the range from 1 to 100
if (primes[i])
printf("%d\n", i);
return 0;
}