944,103 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 162283
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 16th, 2005
-2

C++ prime number program help

Expand Post »
Below is my code for printing prime numbers up to a given input by user. However, my calculations are not working correctly and my output is printing twice. Any idea?


#include <iostream>
#include <cmath>

using namespace std;
void prime_num(int);

int main()
{



cout << " Enter a number and I will generate the prime numbers up to that number: ";
int num = 0;
cin >> num;

prime_num(num);
}



void prime_num( int num)
{
int check_prime = 0;

for ( int i = 0; i <= num; i++)
{
check_prime = 1;

for ( int j = 2; j <= i/2; j++)
{
if ( i % j == 0 )

check_prime = 0;

if ( check_prime != 0 )
{
cout << i << endl;
}
}
}
}
Similar Threads
Reputation Points: 14
Solved Threads: 0
Junior Poster in Training
djbsabkcb is offline Offline
92 posts
since Jun 2005
Jul 16th, 2005
0

Re: C++ prime number program help

Here's what I went with. Note you want to do a break if you find out it's divisible as there is no point in checking all the numbers beyond it for divisibility..

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3. void prime_num(int);
  4. int main()
  5. {
  6. cout << " Enter a number and I will generate the prime numbers up to that number: ";
  7. int num = 0;
  8. cin >> num;
  9. prime_num(num);
  10. }
  11.  
  12. void prime_num( int num)
  13. {
  14. bool isPrime=true;
  15. for ( int i = 0; i <= num; i++)
  16. {
  17. for ( int j = 2; j <= num; j++)
  18. {
  19. if ( i!=j && i % j == 0 )
  20. {
  21. isPrime=false;
  22. break;
  23. }
  24. }
  25. if (isPrime)
  26. {
  27. cout <<"Prime:"<< i << endl;
  28. }
  29. isPrime=true;
  30. }
  31. }
Enter a number and I will generate the prime numbers up to that number: 100
Prime:1
Prime:2
Prime:3
Prime:5
Prime:7
Prime:11
Prime:13
Prime:17
Prime:19
Prime:23
Prime:29
Prime:31
Prime:37
Prime:41
Prime:43
Prime:47
Prime:53
Prime:59
Prime:61
Prime:67
Prime:71
Prime:73
Prime:79
Prime:83
Prime:89
Prime:97

Note you can check your results here:
http://www.utm.edu/research/primes/lists/small/1000.txt
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Jul 16th, 2005
0

Re: C++ prime number program help

It is printing the number multiple times, becuse of:
C++ Syntax (Toggle Plain Text)
  1. for ( int j = 2; j <= i/2; j++)
  2. {
  3. if ( i % j == 0 )
  4. {
  5. check_prime = 0;
  6. }
  7.  
  8. if ( check_prime != 0 )
  9. {
  10. cout << i << endl;
  11. }
  12. }
here you are printing out the number when check prime != 0.
but untill i%j == 0 the program are going to ouput that the number is prime, even when it aint.
this is bad design, and I think you need to rewrite your function.

check out: http://www.daniweb.com/techtalkforum...064-prime.html
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
zyruz is offline Offline
60 posts
since Jun 2005
Jul 16th, 2005
0

Re: C++ prime number program help

I see what you are saying but that still isn't right because 1 is not a prime number
Reputation Points: 14
Solved Threads: 0
Junior Poster in Training
djbsabkcb is offline Offline
92 posts
since Jun 2005
Jul 16th, 2005
0

Re: C++ prime number program help

Quote originally posted by winbatch ...
Here's what I went with. Note you want to do a break if you find out it's divisible as there is no point in checking all the numbers beyond it for divisibility..

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3. void prime_num(int);
  4. int main()
  5. {
  6. cout << " Enter a number and I will generate the prime numbers up to that number: ";
  7. int num = 0;
  8. cin >> num;
  9. prime_num(num);
  10. }
  11.  
  12. void prime_num( int num)
  13. {
  14. bool isPrime=true;
  15. for ( int i = 0; i <= num; i++)
  16. {
  17. for ( int j = 2; j <= num; j++)
  18. {
  19. if ( i!=j && i % j == 0 )
  20. {
  21. isPrime=false;
  22. break;
  23. }
  24. }
  25. if (isPrime)
  26. {
  27. cout <<"Prime:"<< i << endl;
  28. }
  29. isPrime=true;
  30. }
  31. }
Enter a number and I will generate the prime numbers up to that number: 100
Prime:1
Prime:2
Prime:3
Prime:5
Prime:7
Prime:11
Prime:13
Prime:17
Prime:19
Prime:23
Prime:29
Prime:31
Prime:37
Prime:41
Prime:43
Prime:47
Prime:53
Prime:59
Prime:61
Prime:67
Prime:71
Prime:73
Prime:79
Prime:83
Prime:89
Prime:97

Note you can check your results here:
http://www.utm.edu/research/primes/lists/small/1000.txt
I see what you are saying but that still isn't right because 1 is not a prime number
Reputation Points: 14
Solved Threads: 0
Junior Poster in Training
djbsabkcb is offline Offline
92 posts
since Jun 2005
Jul 16th, 2005
0

Re: C++ prime number program help

So Picky . As you wish:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3. void prime_num(int);
  4. int main()
  5. {
  6. cout << " Enter a number and I will generate the prime numbers up to that number: ";
  7. int num = 0;
  8. cin >> num;
  9. prime_num(num);
  10. }
  11.  
  12. void prime_num( int num)
  13. {
  14. bool isPrime=true;
  15. for ( int i = 2; i <= num; i++)
  16. {
  17. for ( int j = 2; j <i; j++)
  18. {
  19. if ( i % j == 0 )
  20. {
  21. isPrime=false;
  22. break;
  23. }
  24. }
  25. if (isPrime)
  26. {
  27. cout <<"Prime:"<< i << endl;
  28. }
  29. isPrime=true;
  30. }
  31. }
Enter a number and I will generate the prime numbers up to that number: 150
Prime:2
Prime:3
Prime:5
Prime:7
Prime:11
Prime:13
Prime:17
Prime:19
Prime:23
Prime:29
Prime:31
Prime:37
Prime:41
Prime:43
Prime:47
Prime:53
Prime:59
Prime:61
Prime:67
Prime:71
Prime:73
Prime:79
Prime:83
Prime:89
Prime:97
Prime:101
Prime:103
Prime:107
Prime:109
Prime:113
Prime:127
Prime:131
Prime:137
Prime:139
Prime:149
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Jul 16th, 2005
0

Re: C++ prime number program help

Quote originally posted by winbatch ...
So Picky . As you wish:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3. void prime_num(int);
  4. int main()
  5. {
  6. cout << " Enter a number and I will generate the prime numbers up to that number: ";
  7. int num = 0;
  8. cin >> num;
  9. prime_num(num);
  10. }
  11.  
  12. void prime_num( int num)
  13. {
  14. bool isPrime=true;
  15. for ( int i = 2; i <= num; i++)
  16. {
  17. for ( int j = 2; j <i; j++)
  18. {
  19. if ( i % j == 0 )
  20. {
  21. isPrime=false;
  22. break;
  23. }
  24. }
  25. if (isPrime)
  26. {
  27. cout <<"Prime:"<< i << endl;
  28. }
  29. isPrime=true;
  30. }
  31. }
Enter a number and I will generate the prime numbers up to that number: 150
Prime:2
Prime:3
Prime:5
Prime:7
Prime:11
Prime:13
Prime:17
Prime:19
Prime:23
Prime:29
Prime:31
Prime:37
Prime:41
Prime:43
Prime:47
Prime:53
Prime:59
Prime:61
Prime:67
Prime:71
Prime:73
Prime:79
Prime:83
Prime:89
Prime:97
Prime:101
Prime:103
Prime:107
Prime:109
Prime:113
Prime:127
Prime:131
Prime:137
Prime:139
Prime:149
thanks, that is correct.

its funny you can sit in front of this screen and try to debug for hours and once you figure it out it was something small to begin with. Very frustrating.
Reputation Points: 14
Solved Threads: 0
Junior Poster in Training
djbsabkcb is offline Offline
92 posts
since Jun 2005
Jul 17th, 2005
0

Re: C++ prime number program help

I know what you mean...

By the way, no need to quote the entire previous posting when putting a reply (only the relevant piece)..
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Sep 4th, 2005
0

Re: C++ prime number program help

Hello My friend, I do not know if my response will be in time for you but here is a master code for finding all prime numbers up to any number 'x' that you input...


C++ Syntax (Toggle Plain Text)
  1. # include <cmath> // This library enable the use of sqrt.
  2. # include <iostream>
  3. using namespace std;
  4.  
  5. void primenum(long double); // Prototype...
  6. int c = 0;
  7.  
  8. int main()
  9. {
  10. long double x = 0;
  11. cout<<"\n This program will generate all prime numbers up to the"
  12. <<"\n number you have entered below...\n";
  13. cout<<"\n Please enter a number: ";
  14. cin>> x;
  15.  
  16. cout<<"\n Here are all the prime numbers up to "<<x<<".\n";
  17. primenum(x); //function invocation...
  18. cout<<endl<<"\nThere are "<<c
  19. <<" prime numbers less than or equal to "<<x<<".\n\n";
  20.  
  21. return 0;
  22. }
  23.  
  24. // This function will determine the primenumbers up to num.
  25. void primenum(long double x)
  26. {
  27. bool prime = true; // Calculates the square-root of 'x'
  28. int number2;
  29. number2 =(int) floor (sqrt (x));
  30.  
  31. for (int i = 1; i <= x; i++){
  32. for ( int j = 2; j <= number2; j++){
  33. if ( i!=j && i % j == 0 ){
  34. prime = false;
  35. break;
  36. }
  37. }
  38. if (prime){
  39. cout <<" "<<i<<" ";
  40. c += 1;
  41. }
  42. prime = true;
  43. }
  44. }


// In you need any explanation for any parts of this program send me a message and i will reply as soon as i can...
Last edited by Dave Sinkula; Sep 4th, 2005 at 12:07 pm. Reason: Added [code][/code] tags.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Pcyther is offline Offline
1 posts
since Sep 2005
Mar 26th, 2007
-1

Re: C++ prime number program help

Click to Expand / Collapse  Quote originally posted by Pcyther ...
Hello My friend, I do not know if my response will be in time for you but here is a master code for finding all prime numbers up to any number 'x' that you input...


C++ Syntax (Toggle Plain Text)
  1. # include <cmath> // This library enable the use of sqrt.
  2. # include <iostream>
  3. using namespace std;
  4.  
  5. void primenum(long double); // Prototype...
  6. int c = 0;
  7.  
  8. int main()
  9. {
  10. long double x = 0;
  11. cout<<"\n This program will generate all prime numbers up to the"
  12. <<"\n number you have entered below...\n";
  13. cout<<"\n Please enter a number: ";
  14. cin>> x;
  15.  
  16. cout<<"\n Here are all the prime numbers up to "<<x<<".\n";
  17. primenum(x); //function invocation...
  18. cout<<endl<<"\nThere are "<<c
  19. <<" prime numbers less than or equal to "<<x<<".\n\n";
  20.  
  21. return 0;
  22. }
  23.  
  24. // This function will determine the primenumbers up to num.
  25. void primenum(long double x)
  26. {
  27. bool prime = true; // Calculates the square-root of 'x'
  28. int number2;
  29. number2 =(int) floor (sqrt (x));
  30.  
  31. for (int i = 1; i <= x; i++){
  32. for ( int j = 2; j <= number2; j++){
  33. if ( i!=j && i % j == 0 ){
  34. prime = false;
  35. break;
  36. }
  37. }
  38. if (prime){
  39. cout <<" "<<i<<" ";
  40. c += 1;
  41. }
  42. prime = true;
  43. }
  44. }


// In you need any explanation for any parts of this program send me a message and i will reply as soon as i can...



very very very nice programmmm......
and thanks for this .....
Reputation Points: 9
Solved Threads: 1
Newbie Poster
inoxmum is offline Offline
5 posts
since Mar 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in C++ Forum Timeline: Library management project for 12th class
Next Thread in C++ Forum Timeline: Something like typedef?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC