C++ code not finding 10001th prime -
i wrote code project euler problem #7(find 10001st prime number) isn't working , throwing out sorts of wrong answers(such numbers)
#include <iostream> #include <math.h> using namespace std; int main() { long long numprime = 1; long long counter = 3; long long arrofprimes[10001]; for(int = 0; < 10001; i++) { arrofprimes[i] = 2; } while(true) { if(numprime == 10001) { break; } bool isprime = true; for(int = 0; < numprime; i++) { if((counter % arrofprimes[i]) == 0) { isprime = false; break; } } if(isprime) { numprime++; arrofprimes[numprime - 1] = counter; } counter++; } cout << counter << endl; }
you have incremented counter after finding 10001st prime, giving off-by-one error.
Comments
Post a Comment