| | |
function to display number of primes in a specific range
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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:
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:
C++ Syntax (Toggle Plain Text)
#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; }
0
#2 Oct 31st, 2009
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.
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.
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e, Paul Thompson] 2) What does this sequence equal to : (.5u - .5a)(.5u-.5b)(.5u-.5c) ...[*] [*solved by : murtan] 3) What is the 123456789 prime numer?
0
#3 Oct 31st, 2009
•
•
•
•
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.
0
#4 Oct 31st, 2009
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:
here is code:
C++ Syntax (Toggle Plain Text)
#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; }
0
#5 Oct 31st, 2009
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.
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.
0
#6 34 Days Ago
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:
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 . . .
C++ Syntax (Toggle Plain Text)
#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 . . .
![]() |
Other Threads in the C++ Forum
- Previous Thread: Why I cannot display the price,sale & total sale in point form?
- Next Thread: How to extract dynamic data from a .txt file/fstream?
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






