Whenever I try to compile the following code, I can enter the initial value I ask for but then nothing happens. Also, there is no build error that being picked up so i don't know why this is happening. Please help and thank you in advance.
#include <stdio.h>
#include <stdbool.h>
int n;
int allDaPrimes[1000];
int counter = 0;
bool isPrime(int number);
int i;
int main()
{
printf("Please enter a numeric value now: ");
scanf("%d", &n);
for (i = 2; i <n; i++){
isPrime(i);
if(isPrime(i)) {
allDaPrimes[counter] = i;
counter++;
}
}
for(i= 0; i==counter; i++){
printf("%d", allDaPrimes[i]);
}
return 0;
}
bool isPrime(int number) {
for (i= 2; i <= number; i++){
if(number % i == 0 && number != i){
return false;
}
}
return true;
}
isPrimefunction has a terrible complexity, so it runs forever. Try limiting toint(sqrt(number)+0.5)you'll have the same results, much faster. note: you're calling it twice in your loop,; making it even worse.