(Noob) need some help w/ Prime Numbers

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2006
Posts: 3
Reputation: coopedw is an unknown quantity at this point 
Solved Threads: 0
coopedw coopedw is offline Offline
Newbie Poster

(Noob) need some help w/ Prime Numbers

 
0
  #1
Mar 1st, 2006
I know there are tons of posts on this, I think I'm on the right track with this, but I can't quite get it to work. I can't figure out how to get the range of primes to print. I'd appreciate any input I can get, sorry about the indenting.

main() keeps doing the following, until user enters a 0 or a negative number:
• Ask user to enter a number;
• If it is positive, print all the prime numbers between 1 and it; when there’s no prime number, print “none�; ( A prime number is a whole number that can only be divided by 1 and itself, i.e., 2, 3, 5, 7, 11, 13, 17, 23, ….)
• Print the max of these prime numbers; or print “none� when there’s no prime.

The calling relation should be: main() calls printPrimes() and getMaxPrime();
printPrimes() and getMaxPrime() calls isPrime() .

My Code:
  1. #include <iostream>
  2. #include <math.h>
  3. using namespace std;
  4.  
  5.  
  6. /* function isPrime (int num) finds out whether the input integer num is a prime number or not
  7. */
  8. bool isPrime (int num)
  9. {
  10. for (int fac = 2; fac <= num; fac++)
  11. if (num % fac == 0) return 0;
  12. return 1;
  13. }
  14.  
  15.  
  16.  
  17.  
  18. /* function printPrimes (int num) prints all the prime numbers between 1 and num
  19. */
  20. void printPrimes (int num)
  21. {
  22. int number = 1;
  23. while(number <= num)
  24. {
  25. ++number;
  26. if (isPrime(number))
  27. cout << number<<" ";
  28. }
  29. }
  30.  
  31.  
  32.  
  33.  
  34. /* function getMaxPrime(int num) returns the greatest prime number between 1 and num. if there is no prime number between 1 and num, getMaxPrime() should return a 0.
  35. */
  36. int getMaxPrime (int num)
  37. {
  38. int maxCount = 0;
  39. for (int count = 1; count <= num; count++)
  40. {
  41. if (isPrime(count))
  42. maxCount = count;
  43. }
  44. return int (maxCount);
  45. }
  46.  
  47.  
  48.  
  49.  
  50. int main ()
  51. {
  52. int number;
  53.  
  54. do
  55. {
  56. cout <<endl<<endl
  57. <<"Please enter a number, 0 or negative to stop :";
  58. cin >> number;
  59. if (number > 0)
  60. {
  61. cout << "All the prime numbers between 1 and "<< number<<" are: "<< endl;
  62. printPrimes(number);
  63. cout << "The max prime is :" ;
  64. int maxPrime = getMaxPrime(number);
  65. if (maxPrime==0) cout <<"none.";
  66. else cout << maxPrime;
  67. }
  68. } while (number>0);
  69. return 0;
  70. }
Attached Files
File Type: cpp lab3work.cpp (1.5 KB, 4 views)
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: (Noob) need some help w/ Prime Numbers

 
0
  #2
Mar 1st, 2006
i'm not sure if I understood the problem but try to replace
  1. if (num % fac == 0)
with
  1. if ((num % fac == 0) && (num != fac))

and use this

  1. void printPrimes (int num)
  2. {
  3. int number = 2;
  4. while(number <= num)
  5. {
  6.  
  7. if (isPrime(number))
  8. cout << number<<" ";
  9. ++number;
  10. }
  11. }
instead your printPrimes
I didn't had the time to test it
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: (Noob) need some help w/ Prime Numbers

 
0
  #3
Mar 1st, 2006
if you want between 1 and num (num not included) than
  1. /* lab3.cpp Working with Functions
  2. */
  3.  
  4. #include <iostream>
  5. #include <math.h>
  6. using namespace std;
  7.  
  8.  
  9. /* function isPrime (int num) finds out whether the input integer num is a prime number or not.
  10. If num is a prime, return true; else return false;
  11.   A prime number is a number whose only factors are 1 and itself.
  12. Note: 0 and 1 are not prime numbers. The minimal prime number is 2.
  13.  
  14. */
  15. bool isPrime (int num)
  16. {
  17. for (int fac = 2; fac <= num; fac++)
  18. if ((num % fac == 0) && (num != fac))
  19. return 0;
  20. return 1;
  21. }
  22.  
  23.  
  24.  
  25.  
  26. /* function printPrimes (int num) prints all the prime numbers between 1 and num
  27. */
  28. void printPrimes (int num)
  29. {
  30. int number = 2;
  31. while(number < num)
  32. {
  33.  
  34. if (isPrime(number))
  35. cout << number<<" ";
  36. ++number;
  37. }
  38. }
  39.  
  40.  
  41.  
  42.  
  43. /* function getMaxPrime(int num) returns the greatest prime number between 1 and num.
  44. if there is no prime number between 1 and num, getMaxPrime() should return a 0.
  45. */
  46. int getMaxPrime (int num)
  47. {
  48. int maxCount = 0;
  49. for (int count = 1; count < num; count++)
  50. {
  51. if (isPrime(count))
  52. maxCount = count;
  53. }
  54. return int (maxCount);
  55. }
  56.  
  57.  
  58.  
  59.  
  60. int main ()
  61. {
  62. int number;
  63.  
  64. do
  65. {
  66. cout <<endl<<endl
  67. <<"Please enter a number, 0 or negative to stop :";
  68. cin >> number;
  69. if (number > 0)
  70. {
  71. cout << "All the prime numbers between 1 and "<< number<<" are: "<< endl;
  72. printPrimes(number);
  73. cout << "The max prime is :" ;
  74. int maxPrime = getMaxPrime(number);
  75. if (maxPrime==0) cout <<"none.";
  76. else cout << maxPrime;
  77. }
  78. } while (number>0);
  79. return 0;
  80. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 3
Reputation: coopedw is an unknown quantity at this point 
Solved Threads: 0
coopedw coopedw is offline Offline
Newbie Poster

Re: (Noob) need some help w/ Prime Numbers

 
0
  #4
Mar 1st, 2006
I still can't get the printPrimes function to work. It doesn't output anything. and the getMaxPrime function returns 1. does anybody have any suggestions for this?
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 3
Reputation: coopedw is an unknown quantity at this point 
Solved Threads: 0
coopedw coopedw is offline Offline
Newbie Poster

Re: (Noob) need some help w/ Prime Numbers

 
0
  #5
Mar 2nd, 2006
Finally got it working. I thought I'd post the code in case someone else has the same problem
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. /* function isPrime (int num) finds out whether the input integer num is a prime number or not.
  7. If num is a prime, return true; else return false;
  8.   A prime number is a number whose only factors are 1 and itself.
  9. Note: 0 and 1 are not prime numbers. The minimal prime number is 2.
  10.  
  11. */
  12. bool isPrime (int num)
  13. {
  14. bool primeCheck = true;
  15. for (int fac = 2; fac < num; fac++ )//runs through factors between 2 and num
  16. {
  17. if (num % fac == 0) //checks to see if a number is divisible
  18. primeCheck = false;
  19. }
  20. return (primeCheck); //returns true or false to calling functions printPrimes and getMaxPrime.
  21. }
  22.  
  23. /* function printPrimes (int num) prints all the prime numbers between 1 and num
  24. */
  25. void printPrimes (int num)
  26. {
  27. bool print;
  28. if (num <= 1)
  29. cout << "none!"; // Determines if their are any primes in the range.
  30.  
  31. for (int count = 2; count <= num; count++ )
  32. {
  33. print = isPrime(count); //Creates "print" function to determine if a number is prime.
  34. if (print == true)
  35. {
  36. cout << count << ", "; // outputs prime number
  37. }
  38. }
  39. }
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47. /* function getMaxPrime(int num) returns the greatest prime number between 1 and num.
  48. if there is no prime number between 1 and num, getMaxPrime() should return a 0.
  49. */
  50. int getMaxPrime (int num)
  51. {
  52. int maxPrime;
  53. int prime = 2;
  54. if (num <= 1) //sets maxPrime to "0" (none)
  55. {
  56. maxPrime = 0;
  57. prime = 0;
  58. }
  59.  
  60. do //checks if a number is prime, sets maxPrime equal to found primes.
  61. {
  62. bool check = isPrime(prime);
  63. if (check == true)
  64. maxPrime = prime; //sets maxPrime to last prime number found in range.
  65. prime ++;
  66.  
  67. }while (prime <= num);
  68. return (maxPrime); // returns the value of maxPrime to main function
  69. }
  70.  
  71.  
  72.  
  73.  
  74. int main ()
  75. {
  76. int number;
  77.  
  78. do
  79. {
  80. cout <<endl<<endl
  81. <<"Please enter a number, 0 or negative to stop :";
  82. cin >> number;
  83. if (number > 0)
  84. {
  85. cout << "All the prime numbers between 1 and "<< number<<" are:"<< endl;
  86. printPrimes(number);
  87. cout << endl;
  88. cout << "The max prime is :" ;
  89. int maxPrime = getMaxPrime( number );
  90. if (maxPrime<=1) cout <<"none.";
  91. else cout << maxPrime;
  92. }
  93. } while (number>0);
  94. return 0;
  95. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 18
Reputation: brahle is an unknown quantity at this point 
Solved Threads: 0
brahle's Avatar
brahle brahle is offline Offline
Newbie Poster

Re: (Noob) need some help w/ Prime Numbers

 
0
  #6
Mar 3rd, 2006
Do you know what is the definition of the prime number? It is a number that has no other diveders other than one and itself. You tested the number to itself (every number devides it self, doesn't it?) so that is why the function always return false.
If the number isn't prime it must have deviders to (inclusively) its square root. Think about it, and say why.
Revenage is a dish best served cold.
50|2|2Y 4 |34|) 3|\|6|_|5|-|
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 138
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

Re: (Noob) need some help w/ Prime Numbers

 
0
  #7
Mar 4th, 2006
If you exclude number 2, and start you for loop with 3, you need to check only odd numbers, increment you for loop by 2.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 3256 | Replies: 6
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC