Created
October 23, 2024 12:59
-
-
Save juanfal/036798644193c2de783a0d9ca7bc1dea to your computer and use it in GitHub Desktop.
primes first n function
This file contains hidden or 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
// t03e3.nprimes.cpp | |
// juanfc 2024-10-23 | |
// | |
#include <iostream> | |
using namespace std; | |
int main() | |
{ | |
bool isPrime(int n); | |
int noPrimes = 100; | |
int cnt = 0; | |
int trying = 2; | |
while (cnt < noPrimes) { | |
if (isPrime(trying)) { | |
cout << trying << " "; | |
++cnt; | |
} | |
++trying; | |
} | |
cout << endl; | |
return 0; | |
} | |
bool isPrime(int n) | |
{ | |
int i = 2; | |
while ( n % i != 0 ) | |
++i; | |
return i >= n; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment