Here is the assignment:

Write a C++ program to calculate and display the number of primes in the first 50 “chiliads”. Your results should be exactly as presented below, under testing.

Design Considerations:

I think you will find your program easier to structure and write if you break it down into functions with the following prototypes:

bool isPrime (long n);
// Returns true if n is a prime, else false

long primeCount (long x, long y);
// Returns the number of primes between x and y, inclusive.

I am having problems with the total and counter, please help

my code:

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

bool isPrime(long n);                       // Returns true if n is a prime, else false
long primeCount (long x, long y);           // Returns the number of primes between x and y, inclusive 
int main ()
{
    int counter, total;
    cout << "Start" <<setw(6) << "End" << setw(24) << "Number of Primes" << endl;

    primeCount (0 , 50000);
    cout << "Total primes in the first 50 chiliads:" << total << endl;

    cout << "Average number per chiliad:" << counter/1 << endl;
    system("pause");
}
long primeCount (long x, long y)
{
    int i; 
    int counter = 0; 
    int total = 0;
    bool test; 

    for ( int k = x; k < y; k += 1000)
{
   for (i =1; i < 1000; i++ )
   {
    test = isPrime(i+k);
    if ( test == true )
    {
        counter++;
        total += counter;
    }
   }
   cout << i+k-999 <<setw(10) << i+k << setw(12) << counter << endl;
   counter = 0;
 }}
bool isPrime (long num)
{
 int j;

 if(num <= 1)
 return false;

 for ( j = 2; j*j <= num; j++ )


    if (num%j == 0)

    return false; 
 return true;

 }

Recommended Answers

All 5 Replies

Basically, you want to know the average of prime per thousand number below 50,000 and total number of prime below 50,000.

Your primeCount is a little mess up. From my understadning primeCount(x, y) should return the number of prime between x to y.

long primeCount (long x, long y) {
    int count = 0;
    for(; x <= y; x++) {
        if (isPrime(x)) {
            count++;
        }
    }

    return count;
}

To calculate average of prime per thousand, you can use this formula: average = total prime / 1000.

_____________________________________________________________
Better solution would be using Sieve of Eratosthenes algorithm

Your code was clos as written. A couple of things, you're incrementing a local variable in primecount, then displaying different variable in main. Also you're incrementing total in the wrong place, you need it after the end of the for loop, and just before you clear counter. Here's some adjusted code for you:

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
bool isPrime(long n); // Returns true if n is a prime, else false
long primeCount (long x, long y); // Returns the number of primes between x and y, inclusive    
int main ()
{
    int total=0;
    cout << "Start" <<setw(6) << "End" << setw(24) << "Number of Primes" << endl;
    total = primeCount (0 , 50000);
    cout << "Total primes in the first 50 chiliads:" << total << endl;
    cout << "Average number per chiliad:" << (long)(total/50) << endl;
    system("pause");
}
long primeCount (long x, long y)
{
    int i;
    bool test = false;
    for ( int k = x; k < y; k += 1000)
    {
        for (i =1; i < 1000; i++ )
        {
            test = isPrime(i+k);
            if ( test == true )
            {
                counter++;
            }
        }
        cout << i+k-999 <<setw(10) << i+k << setw(12) << counter << endl;
        total += counter;
        counter = 0;
    }
    return total;
}
bool isPrime (long num)
{
    int j;
    if(num <= 1)
        return false;
    for ( j = 2; j*j <= num; j++ )
        if (num%j == 0)
            return false;
    return true;
}

oops got ahead of myself a little:

long primeCount (long x, long y)
{
    int i, total = 0, counter = 0;
    bool test = false;
    for ( int k = x; k < y; k += 1000)
    {
            for (i =1; i < 1000; i++ )
            {
                test = isPrime(i+k);
                if ( test == true )
                {
                    counter++;
                }
            }
        cout << i+k-999 <<setw(10) << i+k << setw(12) << counter << endl;
        total += counter;
        counter = 0;
    }
    return total;
}

Okay that makes sense. Thank you so much, I knew I was close to getting there!!

Glad it helps. Please remeber to mark this solved. Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.