View Full Version: Prime Number Tester

C++ Learning Community > C++ Creations > Prime Number Tester


Title: Prime Number Tester
Description: useful (sort of) and simple


tubapro12 - March 11, 2005 03:31 AM (GMT)
This is one of my earlier functions. The main "showcase" here is the prime number function (int IntPrime(int)).

(I just had to change part of it due to the shocking discovery that Borland C++Builder's includes don't have float sqrtf(float) in <cmath> although Dev-C++'s does!)

CODE
// Tubapro's prime tester with a main()
#include <iostream>
#include <ctype>
#include <cmath>
using namespace std;

int IntPrime(int a)
{
   if(a < 0)
     a /= -1;
   int con;
   int i = 3;
   if(a == 1)
       return (1);
   if((a == 2)||(a == 3))
       return (0);
   else
   {
       const double root = sqrt( (double)a ) + 1;
       con = a % 2;
       if(con == 0) { return (2); }
       else
       {
           while(i < root)
           {
               con = a % i;
               if(con == 0)
                   return (i);
               i += 2;     // no need to try dividing by evens if not divisble by 2
           }
       }
   }
   return (0);
}

int main() {
 int input;
 int out;
 cout << "Type a number (you must type a digit):  ";
 cin >> input;
 cout << "\n\n";
 out = IntPrime(input);
 if(out == 0) {
   cout << input << " is a prime number." << endl;
 } else if (out == 1) {
   cout << "One is never a prime number." << endl;
 } else {
   cout << input << " is divisible by " << out << "." << endl;
 }
 cin.sync();
 cin.get();
 return 0;
}


comments and suggestions are welcome of course.

JoeImp - March 11, 2005 05:49 AM (GMT)
Pretty nice. Possibly make it take bigger numbers so that when I see how long it takes to process 3213132313321 it doesnt give me:

-858993460 is divisble by 2 :lol:

Maybe if you make support for huge numbers, give an estimated loops/time left?

Good work

Imp

myork - March 11, 2005 03:07 PM (GMT)
Comments.



CODE
int IntPrime(int a)
{
  // Very interesting way of doing abs.
  // Note that the '/' operator is probably the slowest mathimatical operation
  // Why not just use  a = 0-a or a = abs(a) or a = a*-1
  if(a < 0)
    a /= -1;


  // Dont decalre variables before ou need them
  // When i got to the while loop it took me a while to find the declaration of 'i'
  int con;
  int i = 3;

  // What about if a == 0?
  if(a == 1)
      return (1);
  if((a == 2)||(a == 3))
      return (0);

// Dont need this else
// The previous two if statment have already returned.
  else
  {

      // Dont use C cast.
      // Use the C++ static_cast<double>()
      // In this case the cast is not event required.
      const double root = sqrt( (double)a ) + 1;
      con = a % 2;
      if(con == 0) { return (2); }

// Dont need else because of return
      else
      {
// The compiler will have to covert i to a double before each comparison.
// Why not make root an integer to avoid this problem.
          while(i < root)
          {
              con = a % i;
              if(con == 0)
                  return (i);
              i += 2;     // no need to try dividing by evens if not divisble by 2
          }
      }
  }
  return (0);
}




CODE

int IntPrime(int a)
{
  a  = abs(a);

  if (a == 0)              {return(XXX);} // Not sure what to return.
  if (a == 1)              {return (1);}
  if((a == 2) || (a == 3)) {return (0);}

  if ((a %2) == 0)         {return (2);}

  const int root = static_cast<int>(sqrt(a) + 1);

  for(int i = 3;i < root;i+=2)// no need to try dividing by evens if not divisble by 2
  {
     if ((a % i) == 0)
     {
        return (i);
     }
  }
  return (0);
}

tubapro12 - March 12, 2005 04:03 AM (GMT)
Thanks for the suggestions...


@ JoeImp: I'll upgrade it to using long ints. if you know of any larger numeric data types (i don't) i try to write it for one of them.

@ myork: I never even thought about zero, thanks for pointing that out. i was using double (actually, it was originally a float) to make sure that it didn't round down, but there were other ways around that too, which i know now. i haven't revised it in a long time-i got lazy and assumed it was good enough :unsure:

DeAs91 - March 12, 2005 09:21 AM (GMT)
I couldn't compile it in Dev-C++ (the newest version), nor in VC++, because none of them could find "ctype".

C-Man - March 12, 2005 09:25 AM (GMT)
because it's cctype/ctype.h not ctype
and btw you can remove that include cause none of the functions are used

DeAs91 - March 12, 2005 09:30 AM (GMT)
QUOTE (C-Man @ Mar 12 2005, 09:25 AM)
because it's cctype/ctype.h not ctype
and btw you can remove that include cause none of the functions are used

Okay, I tried and it worked :)

btw, nice program.

tubapro12 - March 13, 2005 07:34 PM (GMT)
upgraded it with the suggestions, based on the code myork rewrote. killed two birds with one stone: upped its maximum input to 4,294,967,295 and got rid of the use of abs() by changing its return type to unsigned long int.

CODE
#include <iostream>
#include <cmath>
using namespace std;

unsigned long LongPrime(unsigned long a)
{
 if (a == 0) {
   return -1;
 } else if (a == 1) {
   return 1;
 } else if((a == 2) || (a == 3)) {
   return 0;
 }

 if ((a % 2) == 0) {
   return 2;
 }

 const long root = static_cast<unsigned long>(sqrt((static_cast<double>(a)) + 1));

 for(int i = 3; i < root; i += 2) {
    if ((a % i) == 0) {
       return i;
    }
 }
 return 0;
}

int main() {
 bool run = true;
 while(run) {
   unsigned long input;
   short out;
   cout << "Type a number to test if it is a prime \nnumber [you must type digit(s), 0 to quit]:  ";
   cin >> input;
   if(input == 0) {
     run = false;
     break;
   }
   cout << "\n\n";
   out = LongPrime(input);
   switch(out) {
     case 0:
       cout << input << " is a prime number." << endl;
       break;
     case 1:
       cout << "One is never a prime number." << endl;
       break;
     case 2:
       cout << "Even numbers are never prime, except two." << endl;
       break;
     default:
       cout << input << " is divisible by " << out << "." << endl;
   }
 }
 cin.sync();
 cin.get();
 return 0;
}


just in case you're wondering 4,294,967,291 is the highest prime it can find. i remembered why it has no output for zero, in the original program it continues to run until zero is input, and i changed this copy to do so too. and i believe someone suggested that static_cast isn't even needed, i tried taking it out and compiler gave me an error, so its still in lol.

Brando123 - March 21, 2005 07:24 AM (GMT)
:o Ohh no!!! Im turning into a nerd too!!! I used input as a variable in my program :o




* Hosted for free by InvisionFree