Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Show Off Your Open Source Projects (http://www.programmingforums.org/forum52.html)
-   -   Calculate prime numbers (http://www.programmingforums.org/showthread.php?t=11224)

peaceofpi Aug 30th, 2006 8:34 PM

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

Jimbo Aug 30th, 2006 8:52 PM

Try using 121 for your input ;)

The Dark Aug 30th, 2006 8:54 PM

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

Jimbo Aug 30th, 2006 9:08 PM

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
:

  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. }


Kaja Fumei Aug 30th, 2006 10:25 PM

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:
:

  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. }


titaniumdecoy Aug 30th, 2006 10:33 PM

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."

glimmy Aug 30th, 2006 10:48 PM

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:
:

  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.

peaceofpi Aug 30th, 2006 10:55 PM

._.

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)

glimmy Aug 30th, 2006 11:12 PM

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...

titaniumdecoy Aug 30th, 2006 11:13 PM

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.

:

  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. }



All times are GMT -5. The time now is 1:17 AM.

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