Advertisement
Shaun_B

Recursive list of prime numbers to between 1 and 1000 (WIP)

Oct 24th, 2012
414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.32 KB | None | 0 0
  1. /**
  2.  * This demonstration will take any entered number between 1 and 1000
  3.  * and tell you if it is a prime number or not. The work is done by the
  4.  * primeNumber function/method/sub-routine, which accepts three inputs:
  5.  * the first integer x will allow you to output all of the prime numbers
  6.  * from 2 upwards to the number that you have entered and is dependent
  7.  * on the third, which is a boolean true or false. If the boolean list
  8.  * is true then it will recursively call itself and work out all of the
  9.  * prime numbers from 2 to the number that is entered, which is the
  10.  * integer y.
  11.  * Entering 1001 or more will exit the demonstration, -1 will 'list' the
  12.  * prime numbers from 2 to the number entered, 0 will switch off list
  13.  * mode and any whole between 2 and 1000 will be determined by the
  14.  * primeNumber function mentioned above.
  15.  * There is a try catch thing in as there's a problem with the Scanner
  16.  * in that not entering a number and a character instead will cause a
  17.  * Exception or something. A safer way to do this would be to accept
  18.  * the input as a string and determine its' numberic value. I seem to
  19.  * remember in Java called ParseInt or something like that if you want
  20.  * to investigate that.
  21.  *
  22.  * @Author:     Shaun B
  23.  * @Version:    1.0.2b
  24.  * @Date:       2012-10-26
  25.  * @Todo:       Work out a 'safer' way to accept the user inputs,
  26.  *              build this into something more presentable, like an
  27.  *              Applet or something ;-)
  28.  **/
  29. import java.util.Scanner;
  30. import java.lang.Math;
  31. public class Numbers
  32. {
  33.     private static boolean run = true;
  34.     private static boolean listMode = false;
  35.     private static int readNumber = 0;
  36.     public static void main (String a [])
  37.     {
  38.         System.out.println("Here is a prime number checker with imporved checking.");
  39.         System.out.println("This will now correctly varify all prime numbers upto 1000.");
  40.         System.out.println("If you want to list all of the prime numbers to the number");
  41.         System.out.println("that you enter then type -1 and press return. If you don't");
  42.         System.out.println("require this, type 0. To exit the demonstration, type 1001");
  43.         System.out.println("or more and it will exit for you.");
  44.         Scanner keyboard = new Scanner (System.in);
  45.         while (run==true)
  46.         {
  47.             System.out.println("Please enter a whole (integer) number");
  48.             try
  49.             {
  50.                 readNumber = keyboard.nextInt();
  51.             }
  52.             catch (Exception e)
  53.             {
  54.                 System.err.println("Illegal keyboard input in main class.");
  55.                 System.exit(0);
  56.             }
  57.             if (readNumber==0)
  58.             {
  59.                 listMode = false;
  60.             }
  61.             else if (readNumber==-1)
  62.             {
  63.                 listMode = true;
  64.             }
  65.             else if (readNumber>=1001)
  66.             {
  67.                 run = false;
  68.                 break;
  69.             }
  70.             else if (readNumber>1)
  71.             {
  72.                 primeNumber(2, readNumber, listMode);
  73.             }
  74.         }
  75.         System.exit(0);
  76.     }
  77.     public static void primeNumber (int x, int y, boolean list)
  78.     {
  79.         if(!list)
  80.         {
  81.             x=y;
  82.         }
  83.     // Here are some improvements from the original version, it checks
  84.     // to see if the entered number has a square root and if this is equal
  85.     // to the integer value of the square root then it can't be a prime
  86.     // number (see boolean prime below):
  87.         double squareRoot = Math.sqrt(x);
  88.         boolean dividesIntoInt = false;
  89.         int i=5;
  90.         // This will check to see if the number enetered (or currently passed to the
  91.         // sub-routine) has any divisors above 5 that produces a whole number
  92.         // other than itself, of course:
  93.         while(i<y)
  94.         {
  95.             // See http://floating-point-gui.de/ for a guide on using floating point
  96.             // numbers (might not be relevant to Java, but it's good to know):
  97.             float z=(float)x/i;
  98.             if((int)z==z && x!=i)
  99.             {
  100.                 dividesIntoInt = true;
  101.                 i=y;
  102.             }
  103.             // Increase index by two (so keeping each number odd):
  104.             ++i;
  105.             ++i;
  106.         }
  107.         // Here's the condition to test for prime numbers. Most prime numbers have
  108.         // will have a remainder if divided by 3. This now checks if number entered
  109.         // has a square root that is a whole number as well:
  110.         boolean prime= (x==2 || x==3 || x==5) ||
  111.                        (x%2!=0 && x%3!=0 && x%5!=0 && (int)squareRoot!=squareRoot) &&
  112.                        !dividesIntoInt;
  113.         if(prime)
  114.         {
  115.             System.out.format("%d ",x);
  116.         }
  117.         if(list && x<y)
  118.         {
  119.             // Here is the recursive call - in other words, this function
  120.             // is calling itself, increasing x by one as it does. Remember, to
  121.             // understand recursive programming, you must first understand
  122.             // recursive programming ;-)
  123.             primeNumber(x+1, y, list);
  124.         }
  125.         if(list && x==y)
  126.         {
  127.             System.out.println("are all prime numbers.");
  128.         }
  129.         if(!list && prime)
  130.         {
  131.             System.out.println("is a prime number");
  132.             return;
  133.         }
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement