Created
October 1, 2017 14:13
-
-
Save RohithKumar1368/986b463584d8815bdeebe6ff043e7b01 to your computer and use it in GitHub Desktop.
program to print the first 500 prime numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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