I have been trying to solve this problem for the last few days and I think I have worked out any obvious logical errors in my code, however, now nothing is printing to the console. I was wondering if anyone could help me and point out what could be going wrong with my code?
Original question: By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10001st prime?
int main(int argc, char *argv[]) {
int number = 2; //number being tested
int counter = 0; //keeps track of prime numbers found
bool check = true; //if check = false, number is not prime
while(counter != 10001) {
if(number == 2) {
counter++;
number++;
} else {
for(int i = 2; i < sqrt(number); i++) {
while(check == true) {
if(number % i == 0) {
check = false;
}
}
}
if(check == true) {
counter++;
}
printf("%d, %dn", number, counter); //testing
number += 2;
check = true;
}
}
printf("%d", number - 2);
return 0;
}
I know this is probably a very inefficient way to solve this problem, but I am just looking to get the correct answer first before I look to optimize it.
Trystan Allen is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.