Prime Number

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2008
Posts: 3,810
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Prime Number

 
0
  #11
Oct 21st, 2008
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main ()
  5.  
  6. {
  7. int q;
  8. int j;
  9. int sum = 0;
  10. int p;
  11. int num;
  12. cout << " Please enter an even integer greater than 2: ";
  13. cin >> num;
  14.  
  15. for ( int i = 2; i < num; i++ )
  16.  
  17. {
  18. if ( ( i == 2 ) or ( i % 2 != 0 ) )
  19.  
  20. p = i;
  21.  
  22. for ( int j = 1; j <= p; j++ )
  23.  
  24. {
  25. if ( p % j == 0 )
  26.  
  27. {
  28. sum += j;
  29.  
  30. }
  31.  
  32. }
  33.  
  34. {
  35. if ( sum == p + 1 )
  36.  
  37. q = p;
  38.  
  39. }
  40. cout << q << endl;
  41.  
  42. }
  43.  
  44. return 0;
  45. }

The brackets on lines 34 and 39 have no effect. The spacing makes it very hard to read. Indent consistently, get rid of the meaningless brackets and the blank lines. Here's your code with better spacing:

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main ()
  5.  
  6. {
  7. int q;
  8. int j;
  9. int sum = 0;
  10. int p;
  11. int num;
  12. cout << " Please enter an even integer greater than 2: ";
  13. cin >> num;
  14.  
  15. for ( int i = 2; i < num; i++)
  16. {
  17. if ( ( i == 2 ) or ( i % 2 != 0 ) )
  18. p = i;
  19.  
  20. for ( int j = 1; j <= p; j++ )
  21. {
  22. if ( p % j == 0 )
  23. sum += j;
  24.  
  25. if ( sum == p + 1 )
  26. q = p;
  27. }
  28.  
  29. cout << q << endl;
  30. }
  31.  
  32. return 0;
  33. }

Reread stilllearning's earlier post about line 17. I was surprised, but this code compiled on Dev C++. I figured "or" would be an error, but apparently Dev C++ didn't mind. Still, best to use || instead of "or".

I don't understand what this alrgorithm has to do with prime numbers. For example, what does sum represent?
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Prime Number

 
1
  #12
Oct 21st, 2008
> I was surprised, but this code compiled on Dev C++. I figured "or" would be an error
http://david.tribble.com/text/cdiffs.htm#C99-alt-tok

It's one of the less travelled paths through the C++ standard
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: Prime Number

 
0
  #13
Oct 22nd, 2008
Very interesting . I had no idea about this. Thanks Salem.
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC