This is what I have so far but its not printing out the correct answers. I'm trying to get it to print the number of primes. The rest of my code works fine but this part I just can't get.
I tried both methods provided and not getting the correct output.
It should return 4 if I was to choose a number from 1 to 10 or 25 if I was to choose a number from 1 to 100.
You're incrementing the number passed as an argument. You'll need some sort of counter to keep track of the number of primes. I really don't think you'll need to return an unsigned long long. An unsigned int is sufficient enough(up to about 4.3billion).
bool isprime(unsignedlonglong n)
{
int nFactors = 0;
for(int d = 1; d <= n; ++d)
{
if(n % d == 0) nFactors++;
}
return (nFactors == 2);
}
unsignedlonglong pi(unsignedlonglong n)
{
int N = 0; // The counter variable should be this one
for(int i = 1; i <= n; ++i)
{
if(isprime(i) == true) ++N;
}
return N;
}
@integralfx
Don't judge a book by its cover. At least test my code first before making any assumptions.