Since, the question is asked with category C, so I am writing answer in C.
#include<stdio.h>
int main(void)
{
int i,k=2;
int j;
int prime[50001] = {};
int n = 50001;
// populating array with naturals numbers
for(i=2;i<n;i++){
prime[i]=i;
}
//Sieve algorithm
for(i=2;i<n;i++){
if (prime[i]!=0){
for(j=2;j<n;j++){
if (prime[i]*j>n){
break;
}
else{
// Instead of deleteing , making elemnets 0
prime[prime[i]*j]=0;
}
}
}
}
//IF number is not 0 then it is prime
for(i=30009;i<n;i++){
if(prime[i]!=0){
printf("%d\n",prime[i]);
}
}
}
This is based on Sieve algorithm.
Sieve algorithm is :
Let you have a list of 20 first natural numbers, starting from 2 : 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
Start from the first number and delete all multiples of the number except the number. First number is 2. So, delete all its multiples.
New list is : 2,3,5,7,9,11,13,15,17,19
Next number is 3. So, delete all its multiples. New list is : 2,3,5,7,11,13,15,17,19
Similar try for rest of elements and the remaing list will contain only prime numbers : 2,3,5,7,11,13,15,17,19
P.S. : We encourage this kind of questions.
# Initialize a list
primes = []
for i in range(2,101):
# Assume number is prime until shown it is not.
isPrime = True
for num in range(2, i):
if i % num == 0:
isPrime = False
if isPrime:
primes.append(i)
print(primes)