Skip to content

Instantly share code, notes, and snippets.

@RohithKumar1368
Created October 1, 2017 14:13
Show Gist options
  • Save RohithKumar1368/986b463584d8815bdeebe6ff043e7b01 to your computer and use it in GitHub Desktop.
Save RohithKumar1368/986b463584d8815bdeebe6ff043e7b01 to your computer and use it in GitHub Desktop.
program to print the first 500 prime numbers
#include<iostream>
using namespace std ;
int main() {
int prime[501] ;
int n = 3 ;
int j = 1 ;
prime[1] = 2 ;
j++ ;
prime[j] = n ;
while(j < 500){
n += 2 ;
int k = 2 ; // prime[k] will run through possible prime divisors of N
do{
int q = n / prime[k] ;
int r = n % prime[k] ;
if(r == 0) break ;
if(q <= prime[k]){
j++ ;
prime[j] = n ;
break;
} else {
k++ ;
}
}while(k < j) ;
}
for(int i = 0 ; i < 500 ; i++){
cout << prime[i+1] << endl ;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment