Hello i am quite stumped with a function to print the following results:


Start End Number of Primes
1 1000 168
1001 2000 135
2001 3000 127
3001 4000 120
4001 5000 119
5001 6000 114
6001 7000 117
7001 8000 107
8001 9000 110
9001 10000 112



49001 50000 98

Total primes in the first 50 chiliads: 5133
Average number per chiliad: 102.66


I have some pseudo code and the function for determining whether or not a number is prime. but i am unable to figure out how to write the function to display the ranges and the counts.

Any help would be much appreciated.


Here is my pseudo-code and my function to determine if a number is prime:

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

using namespace std;

// funtions
bool isPrime (long num);          
// 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()
{
// create a loop that calls primeCount(x,y) to test 1000 numbers at a time
 /*
use the value returned from primeCount to output results

// end of the loop
// output final statistics
}

longprimeCount (long x, long y)
{
// loop through all of the thousand numbers
// call isPrime to check each number
// some kind of logic to deal with the result of isPrime
// return how many primes found between x and y
}

bool isPrime (long num)
{
// determine if num is a prim number
// return true if it is
// return false if it is not
}


bool isPrime(int n)
{
    if((n == 0)||(n == 1))
        return false;
        
    int factors = 2;
    numPrimes++
            
    for(int i=2; i<=((int)sqrt((double)n)); i++){
        if(n%i == 0){
            factors++;
            break;
        }
    }
            
    if(factors == 2)
        return true;
    
   return false;
}

Good start, now first implement isPrime function first, and test it out
with a loop from 0 to 100.

Remember a prime number can only be a prime if it is only EVENLY divisible
by itself and 1. That means if the mod operator return testPrime %
index== 0 is true and the index is > 2 and not equal to testPrime
then its not a prime.

Good start, now first implement isPrime function first, and test it out
with a loop from 0 to 100.

Remember a prime number can only be a prime if it is only EVENLY divisible
by itself and 1. That means if the mod operator return testPrime %
index== 0 is true and the index is > 2 and not equal to testPrime
then its not a prime.

I the isPrime function from an old C program i wrote so i know it works. I'm not sure how to write the function that calls 1000 numbers at a time and returns the primes between x and y..... i need a for loop and then i need to call isPrime and increment a counter but i just cant seem to logicaly put it all together from my head to paper.

Okay, so ive been working on this for a while now, i have a little more code figured out i think. However when i pass 1 and 1000 to my function i do not get the right output....

here is code:

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

bool isPrime(long n);
long primeCount (long x, long y);

int main ()
{
	int i, counter =0;
	bool test;
    char reply;


    cout << "Start" <<setw(6) << "End" << setw(24) << "Number of Primes" << endl;
    
    
    
        primeCount ( 1, 1000);
        
        cout << 1<< setw(6) << 1000 << setw(24) << counter << endl;  
          	
        
     
      	
    
    
    cout << "Press 'q' followed by 'Enter' to quit: ";
    cin >> reply;
    
    return 0;
  
}

long primeCount (long x, long y)
{
     int i = 0, counter = 0;
     bool test;
     
     for (i = x; i <= y; i++ )
	 {
        test = isPrime(i);
		if ( test == true )
		{
		   counter++;
		}
		
}}

bool isPrime (long n)
{
 int j, num;
 
 if(num <= 1)
 return false;
 
 for ( j = 2; j*j <= num; j++ )
 
	
	if (num%j == 0)
	
	return false; 
 return true;
 

 }

The Prime testing method is absolutely mis-interpreted.

http://www.daniweb.com/forums/thread233963.html
Here is a recent prime number test code. Try to change it such that it matches your requirements. Though, i would prefer you try to understand what exactly is going on.

I rewrote most of the code so it works mostly correct now. I am still having problems with the column formatting and total number of chiliads as well as average # of chiliads. Any guidance would be appreciated. here is current code and output:

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

bool find_prime(long);
long primeCount (long x, long y);

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, counter =0, total = 0;
	bool test; 
    
    for ( int k = x; k < y; k += 1000)
{
   for (i =1; i < 1000; i++ )
   {
	test = find_prime(i+k);
   	if ( test == true )
	{
	    counter++;
	    total += counter;
	}
   }
   cout << i+k-999 <<setw(10) << i+k << setw(12) << counter << endl;
   counter = 0;
 }}

bool find_prime (long num)
{
 int j;
 
 if(num <= 1)
 return false;
 
 for ( j = 2; j*j <= num; j++ )
 
	
	if (num%j == 0)
	
	return false; 
 return true;
 

 }

output:

Start End Number of Primes
1 1000 168
1001 2000 135
2001 3000 127
3001 4000 120
4001 5000 119
5001 6000 114
6001 7000 117
7001 8000 107
8001 9000 110
9001 10000 112
10001 11000 106
11001 12000 103
12001 13000 109
13001 14000 105
14001 15000 102
15001 16000 108
16001 17000 98
17001 18000 104
18001 19000 94
19001 20000 104
20001 21000 98
21001 22000 104
22001 23000 100
23001 24000 104
24001 25000 94
25001 26000 98
26001 27000 101
27001 28000 94
28001 29000 98
29001 30000 92
30001 31000 95
31001 32000 92
32001 33000 106
33001 34000 100
34001 35000 94
35001 36000 92
36001 37000 99
37001 38000 94
38001 39000 90
39001 40000 96
40001 41000 88
41001 42000 101
42001 43000 102
43001 44000 85
44001 45000 96
45001 46000 86
46001 47000 90
47001 48000 95
48001 49000 89
49001 50000 98
Total primes in the first 50 chiliads:94
Average number per chiliad:2
Press any key to continue . . .

Okay i figured out how to get totals and averages right, but i still need some help on the formatting.

Is there a way i could use a loop in main to check 1000 numbers at a time and insert them into the columns formatted correctly. Everything is supposed to line up in the output at the end.

Thanks!

well your could use cout.width() function..... with an argument. to set up the allignment

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.