Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 30th, 2006, 7:34 PM   #1
peaceofpi
hi: for(;;) goto hi;
 
peaceofpi's Avatar
 
Join Date: Jun 2006
Posts: 94
Rep Power: 3 peaceofpi is on a distinguished road
Send a message via AIM to peaceofpi Send a message via MSN to peaceofpi
Calculate prime numbers

Hope it hasn't been done before D:
But anyway, I did it all by myself. Took about two to three days of on and off thinking to figure out how to get it working. It seems to work correctly, comments and criticism of my probably horrible code are welcome.

http://pi.monkeh.net/prime.cpp
__________________
How do you play Religious Roulette?
Stand around in a circle and blaspheme till someone gets struck by lightning.
peaceofpi is offline   Reply With Quote
Old Aug 30th, 2006, 7:52 PM   #2
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3 Jimbo is on a distinguished road
Try using 121 for your input
Jimbo is offline   Reply With Quote
Old Aug 30th, 2006, 7:54 PM   #3
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 852
Rep Power: 4 The Dark is on a distinguished road
121 doesn't work

Also, this code seems odd:
        while (n%2==0)            // tests for even number. if it's even, it's not going to be prime.
                                  // 2 is indeed prime, but n starts at 10 n_n
        {
           cout << n << " is even" << endl;
           break;
        }
Why not just use an if statement?

Edit: D'oh should have refreshed before I typed
The Dark is offline   Reply With Quote
Old Aug 30th, 2006, 8:08 PM   #4
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 763
Rep Power: 3 Jimbo is on a distinguished road
Ok, I looked through the code this time...
1) As The Dark mentioned, using a while when you always break is kinda silly. That's what if statements are for.
2) Don't call main() from your own program.
3) Your if statement is incorrect. It's also pretty redundant (you already know n%2 is 0, so n%4, n%6, n%8, n%10 is pointless)

I took your code, sliced a lot out, and wrote my own version, if you'd like to compare. (I took out the system("pause") because it doesn't work on my computer :p) I didn't really comment it, but if you have questions, just ask
cpp Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. bool isPrime(unsigned int n)
  5. {
  6. if(!(n%2) || n < 9) // cover any n < 9; assumes 1 is prime
  7. return false;
  8. for(int i = 3; i*i <= n; i+=2)
  9. {
  10. if(!(n%i))
  11. return false;
  12. }
  13. return true;
  14. }
  15.  
  16. void prime(int input)
  17. {
  18. for (int n=10; n<=input; n++)
  19. {
  20. if(!(n%2))
  21. {
  22. cout << n << " is even" << endl;
  23. continue;
  24. }
  25. cout << n << " is odd ";
  26. if(isPrime(n))
  27. cout << "and prime";
  28. cout << endl;
  29. }
  30. }
  31.  
  32. int main()
  33. {
  34. int input = 0;
  35. while(true)
  36. {
  37. cout << "Enter number: ";
  38. cin >> input;
  39. if(input < 10)
  40. cout << endl << "Higher than 10 please..." << endl;
  41. else
  42. break;
  43. }
  44. prime(input);
  45. }
Jimbo is offline   Reply With Quote
Old Aug 30th, 2006, 9:25 PM   #5
Kaja Fumei
Hobbyist Programmer
 
Join Date: Oct 2005
Posts: 134
Rep Power: 3 Kaja Fumei is on a distinguished road
Couple of comments on Jimbo's isPrime():
1. It assumes the input is an odd number. While it always true in this context when prime() calls it, a seperate call to isPrime(14) will return true instead of false because it is not checked for being a multiple of 2, and the loop will break before it reaches the other factor of 7.
2. In the n < 9 case, I think you meant to use "&&" instead of "||". And there needs to be a special case for when n is 2, because 2 is both even and prime.

While the isPrime() works perfectly when called by the current prime() function because the input is never even or less than 9, it would yield incorrect results if called by another function that didn't filter anything out first.

Here's an isPrime() that should work from any context:
cpp Syntax (Toggle Plain Text)
  1. bool isPrime(unsigned int n)
  2. {
  3. if(n < 9) // cover any n < 9; assumes 1 is prime
  4. return n % 2 == 1 || n == 2; // true if odd or 2
  5. else if(n % 2 == 0) // quick check for evens
  6. return false;
  7.  
  8. for(int i = 3; i*i <= n; i+=2)
  9. {
  10. if(!(n%i))
  11. return false;
  12. }
  13. return true;
  14. }
Kaja Fumei is offline   Reply With Quote
Old Aug 30th, 2006, 9:33 PM   #6
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 856
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
I found this on some random site and it made me laugh:

Quote:
A mathematician, scientist, and engineer all set out to prove that all numbers are prime. The mathematician says "one is prime, two is prime, three is prime, they're all prime." The scientist says "one is prime, two is prime, three is prime, four is experimental error, five is prime, they're all prime." The engineer says "one is prime, two is prime, three is prime, four is prime, five is prime, they're all prime."
titaniumdecoy is offline   Reply With Quote
Old Aug 30th, 2006, 9:48 PM   #7
glimmy
Programmer
 
glimmy's Avatar
 
Join Date: May 2005
Location: Minnesota
Posts: 42
Rep Power: 0 glimmy is on a distinguished road
Send a message via AIM to glimmy
Its funny you should bring this up. Earlier today I made a similar program, though mine just goes through all numbers upto 1,000,000,000:
cpp Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6. int ind = 0;
  7. int prime;
  8. int max;
  9.  
  10. int list[10000000] = {2};
  11.  
  12. for (int i = 3;i < 1000000000;i++) {
  13.  
  14. prime = 1;
  15.  
  16. for (int j = 0; j < ind; j++) {
  17. if ((i%list[j]) == 0) {
  18. prime = 0;
  19. break;
  20. }
  21. }
  22.  
  23. if (prime == 1) {
  24. ind++;
  25. list[ind] = i;
  26. cout << i << "\n";
  27. }
  28. }
  29. return 0;
  30. }

I had an earlier version that brute forced every number, but I found that only using primes works much faster.
glimmy is offline   Reply With Quote
Old Aug 30th, 2006, 9:55 PM   #8
peaceofpi
hi: for(;;) goto hi;
 
peaceofpi's Avatar
 
Join Date: Jun 2006
Posts: 94
Rep Power: 3 peaceofpi is on a distinguished road
Send a message via AIM to peaceofpi Send a message via MSN to peaceofpi
._.

Guess I better hit the tutorial again.

edit:
Quote:
Originally Posted by Jimbo
2) Don't call main() from your own program.
I know that's probably not a good thing to do, but what else can I do? (Drawing a blank)
__________________
How do you play Religious Roulette?
Stand around in a circle and blaspheme till someone gets struck by lightning.
peaceofpi is offline   Reply With Quote
Old Aug 30th, 2006, 10:12 PM   #9
glimmy
Programmer
 
glimmy's Avatar
 
Join Date: May 2005
Location: Minnesota
Posts: 42
Rep Power: 0 glimmy is on a distinguished road
Send a message via AIM to glimmy
Quote:
Originally Posted by peaceofpi
._.
I know that's probably not a good thing to do, but what else can I do? (Drawing a blank)
A loop might be good...
glimmy is offline   Reply With Quote
Old Aug 30th, 2006, 10:13 PM   #10
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 856
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
I wrote this method for determining whether a number is prime a while ago. It is almost identical to the code posted by Kaja Fumei. I'm not sure if it completely correct though; if not I would appreciate any pointers.

cpp Syntax (Toggle Plain Text)
  1. bool is_prime(int x) {
  2. if (x % 2 == 0 || x == 1)
  3. return false;
  4. for (int i = 3; i <= sqrt(x); i += 2)
  5. if (x % i == 0)
  6. return false;
  7. return true;
  8. }
titaniumdecoy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Median/Mode in arrays? {Need help} Java|Tera Java 27 Nov 29th, 2005 10:50 AM
Prime Numbers Konnor C++ 6 Sep 12th, 2005 6:46 PM
Generating prime and composite numbers saadmir C 5 Sep 9th, 2005 4:08 PM
Prime numbers thomzor C++ 24 May 20th, 2005 2:02 PM
Help with a couple prime numbers programs!! BehemothPhoenix C++ 3 Mar 20th, 2005 2:06 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:02 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC