Hi!
I have a sub program where I find every prime numbers between 1..10000. Now in driver program I need to count how many prime numbers I found. How should I do that? I tried different kind of loops but none of thmen works.
Here is my sub program if necessary (num1=1, num2=10000).
#include <iostream>
#include <string>
bool is_prime(int number)
{
// your logic to determine if a number is prime
returnfalse; // or true
}
int main()
{
int counter = 0;
for (int i = 3; i < 10000; ++i)
{
if (is_prime(i))
++counter;
}
std::cout << "Number of primes " << counter << '\n';
}
I played around with this alot a few months back and actually for whatever reason revisited it the other day for fun. That sieve method is probably more efficient at smaller numbers but I found that using it to find numbers that were like 999 billion seemed to break it bc its bigger that the max vector size.
Using something like a binary string would probably work better. Harder to deal with but much smaller. Bitset maybe. I'd like to look into that.
Even with numbers that fall within the upper limits of the max vector size seemed to bog down pretty good just bc of the amount of memory used and the colossal number of read and writes involved.
What I posted above is good at determining if a huge number like 999 billion is prime or not. Not really meant to find all primes between 1 and n.