?
Solved

C++ prime counter within range

Posted on 2005-02-24
6
Medium Priority
?
403 Views
Last Modified: 2008-02-01
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
Comment
Question by:dauler
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 11

Expert Comment

by:griessh
ID: 13396155
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
 
LVL 11

Expert Comment

by:griessh
ID: 13396203
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
 
LVL 11

Expert Comment

by:griessh
ID: 13396391
Just so soemthing is left for you, your loop criteria has one minor but fatal flow.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:dauler
ID: 13396714
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
 
LVL 13

Expert Comment

by:rettiseert
ID: 13397530
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
 
LVL 11

Accepted Solution

by:
griessh earned 500 total points
ID: 13397635
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Make the most of your online learning experience.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Six Sigma Control Plans
Suggested Courses

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question