Skip to content

Instantly share code, notes, and snippets.

@juanfal
Created October 23, 2024 12:59
Show Gist options
  • Save juanfal/036798644193c2de783a0d9ca7bc1dea to your computer and use it in GitHub Desktop.
Save juanfal/036798644193c2de783a0d9ca7bc1dea to your computer and use it in GitHub Desktop.
primes first n function
// 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