• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 421
  • Last Modified:

C++ prime counter within range

Great site!

I have written a c++ program that is supposed to count prime numbers in a range entered by the user but I think my prime tester function is not working.  Here is what I have:

#include <iostream>
using namespace std;
bool isPrime (int n);
int primeCount (int x, int y);
int main ()
{
    int x, y;
    cout << "Enter a range : " << endl;
    cin >> x >> y;
    cout << "The number of primes between  " << x << " and " << y << "is " << primeCount (x, y) << endl;
    return 0;
}
bool isPrime (int n)
{
int i;
for (i=2; i<=n;i++)
   {
        if ((n % i) == 0)
       
        return false;
            
        return true;
            
   }
}
int primeCount (int x, int y)
{
int a=0;
    for (x=x;x<=y;x++)
    {
          if (isPrime(x))
          a++;
          return a;
     }
}
 I am very frustrated because I have tried everything.  Any help would be appreciated :)!
0
dauler
Asked:
dauler
  • 4
1 Solution
 
griesshCommented:
for (i=2; i<=n;i++)
   {
        if ((n % i) == 0)
       
        return false;
         
        return true;
         
   }

The return statement does what the name says: it RETURNS from the function. That means for i=2 you get a result true or false and with that you return right away to the calling function.
You want to assign the return value AFTER the loop.
0
 
griesshCommented:
To speed up your process:

You can return once you figured out that the number is NOT prime
  if ( (N%i) == 0) return false;
You don't have to test even numbers, they are NEVER prime
  if (i ==2 ) return true;
  for (i=3 ; i<=n; i=i+2) {}
0
 
griesshCommented:
Just so soemthing is left for you, your loop criteria has one minor but fatal flow.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
daulerAuthor Commented:
Thanks griessh,

My apologies but I am new to this and am a bit confused.  I also know that I need to include n == 1 as a condition for a false return since 1 is not prime.  I tried returning after the loop but still had problems here is what I have now.

if bool isPrime (int n)
{
int i=2;
if ((n % i) == 0 || n==1)return false;
for (i=2; i<=n;i++)
{}
return true;
}
it says 9 is prime so I am having the same problem.  These loops are killing me :(  Thanks again for any help griessh
0
 
rettiseertCommented:
bool isPrime (int n){
   int i;
   for (i=2; i<n;i++){
      if ((n % i) == 0) return false;
   }
   return true;
}

int primeCount (int x, int y){
   int a=0;
   for (;x<=y;x++){
      if (isPrime(x))a++;
     }
   return a;
}
0
 
griesshCommented:
You want to check for each i between 2 and n if it is divisible.
If it is you found a number that is not prime and you can exit (return false).
If you cannot find any of these you return true.

bool isPrime (int n)
{
int i;

if (n<2)
{
      cout << "Invalid number:" << n << endl;
      return false;
}

for (i=2; i<n;i++)
{
        if ((n % i) == 0)    return false;          
}
return true;

}
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now