Skip to content

Instantly share code, notes, and snippets.

@Kaminate
Created August 6, 2013 06:10
Show Gist options
  • Save Kaminate/6162435 to your computer and use it in GitHub Desktop.
Save Kaminate/6162435 to your computer and use it in GitHub Desktop.
Returns the number of primes before int N. This is my implementation of a programming internship practice test problem from some game company. I used the sieve of eratosthenes I saw on wiki once.
// Nathan Park
//
// This is my implementation of a programming intership practice test problem
// from some company.
#include <vector>
int getNumberOfPrimes(int N)
{
std:vector <int> primes(N, 1); // 0 = prime, 1 means not prime
for (unsigned i = 2; i < N; ++i)
{
if (!primes[0]) continue;
for (unsigned j = 2; j <= N/i; ++j)
primes[j * i] = 0;
}
unsigned count = 0;
for (unsigned i = 2; i < N; ++i)
count += primes[i];
return count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment