Loading...
Logo
Processing Forum
 it will return true if the input is prime and false otherwise 

Replies(4)

There are several methods, one of the most well known (perhaps because it is one of the simplest) is the Sieve of Eratosthenes. Slow of big numbers, enough for simple cases.
It is not a matter of a simple function, it needs to be an iterative, quite long method.
That's a coincidence, I've just made this function to find that. Actually it was my Dad, a math lover, who made the logic, i just translated to code. He has coded that a long time ago in his Hp calculator... It looks like it is working :) I have printed all prime numbers from 5 to 1,000,000 with it. And is in portuguese... Well I'll translate the messages here. Also there is no check for negative numbers. Now I will look to the Sieve of Eratosthenes to see how it compares :)


Copy code
  1. boolean testaPrimo(double in)
  2. {
  3.   double i = 3; // double needed for big numbers
  4.  if (in == 3 || in == 2)
  5.   {
  6.     println(in + " Is prime");
  7.     return true;
  8.   } 
  9.   if(in % 2 != 0) // if even return quickly
  10.   {
  11.   while(in % i != 0 )
  12.   {
  13.     if(i >= Math.sqrt(in))
  14.     {
  15.     println(in + " It is prime because the last  divisor tested " + i + " is bigger than the square root  " + (Math.sqrt(in)) + " of the number tested.\n"); 
  16.       return true;
  17.   }else{
  18.     i+=2;
  19.   }    
  20.   }
  21.   println((int)in +" Not prime as is divisible by " + i + "\n");
  22.   return false;
  23.   
  24.   }else
  25.  {
  26.    println(in + " Not prime cause " + in + " is even." + "\n");
  27.    return false;
  28.  }
  29. }
Nice 1, v.k.!
... there is no check for negative numbers.
Fixed that!!! Also, changed double to long type, since everything is integer-based anywayz:

Copy code
  1. /*
  2. TestaPrimo
  3.  by v.k. (2012/Oct)
  4.  //
  5.  http://forum.processing.org/topic/how-would-i-write-a-function-to-determine-whether-a-number-is-prime-s
  6.  */
  7. final void setup() {
  8.     noLoop();
  9.     for ( long primo=-5; primo<30 ; primo+=2 )
  10.         testaPrimo(primo);
  11. }
  12. //
  13. private static final boolean testaPrimo(long num) {
  14.     //
  15.     final long in = num < 0 ? -num : num;
  16.     long i = 3;
  17.     //
  18.     if (in == 3 || in == 2) {
  19.         println(num + " ->\tis a prime");
  20.         return true;
  21.     }
  22.     else if (in == 1) {
  23.         println(num + " ->\tlacks some properties to be considered a true prime!!!");
  24.         return false;
  25.     }
  26.     else if (in % 2 != 0) {
  27.         //
  28.         while (in % i != 0 )
  29.             //
  30.             if (i >= sqrt(in)) {
  31.                 print(num + " ->\tis a prime for its square root {" + sqrt(in));
  32.                 println("} is lesser than the last checked divisor {" + i + "}.");
  33.                 return true;
  34.             }
  35.             else    i+=2;
  36.         //
  37.         println(num +" ->\tisn't a prime as it is divisible by " + i);
  38.         return false;
  39.     }
  40.     //
  41.     else {
  42.         //
  43.         println(num + " ->\tisn't a prime for " + num + " is even.");
  44.         return false;
  45.     }
  46.     //
  47. }
thanks GoToLoop, compliments to my father :) Thanks for the fixes.