Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * This demonstration will take any entered number between 1 and 1000
- * and tell you if it is a prime number or not. The work is done by the
- * primeNumber function/method/sub-routine, which accepts three inputs:
- * the first integer x will allow you to output all of the prime numbers
- * from 2 upwards to the number that you have entered and is dependent
- * on the third, which is a boolean true or false. If the boolean list
- * is true then it will recursively call itself and work out all of the
- * prime numbers from 2 to the number that is entered, which is the
- * integer y.
- * Entering 1001 or more will exit the demonstration, -1 will 'list' the
- * prime numbers from 2 to the number entered, 0 will switch off list
- * mode and any whole between 2 and 1000 will be determined by the
- * primeNumber function mentioned above.
- * There is a try catch thing in as there's a problem with the Scanner
- * in that not entering a number and a character instead will cause a
- * Exception or something. A safer way to do this would be to accept
- * the input as a string and determine its' numberic value. I seem to
- * remember in Java called ParseInt or something like that if you want
- * to investigate that.
- *
- * @Author: Shaun B
- * @Version: 1.0.2b
- * @Date: 2012-10-26
- * @Todo: Work out a 'safer' way to accept the user inputs,
- * build this into something more presentable, like an
- * Applet or something ;-)
- **/
- import java.util.Scanner;
- import java.lang.Math;
- public class Numbers
- {
- private static boolean run = true;
- private static boolean listMode = false;
- private static int readNumber = 0;
- public static void main (String a [])
- {
- System.out.println("Here is a prime number checker with imporved checking.");
- System.out.println("This will now correctly varify all prime numbers upto 1000.");
- System.out.println("If you want to list all of the prime numbers to the number");
- System.out.println("that you enter then type -1 and press return. If you don't");
- System.out.println("require this, type 0. To exit the demonstration, type 1001");
- System.out.println("or more and it will exit for you.");
- Scanner keyboard = new Scanner (System.in);
- while (run==true)
- {
- System.out.println("Please enter a whole (integer) number");
- try
- {
- readNumber = keyboard.nextInt();
- }
- catch (Exception e)
- {
- System.err.println("Illegal keyboard input in main class.");
- System.exit(0);
- }
- if (readNumber==0)
- {
- listMode = false;
- }
- else if (readNumber==-1)
- {
- listMode = true;
- }
- else if (readNumber>=1001)
- {
- run = false;
- break;
- }
- else if (readNumber>1)
- {
- primeNumber(2, readNumber, listMode);
- }
- }
- System.exit(0);
- }
- public static void primeNumber (int x, int y, boolean list)
- {
- if(!list)
- {
- x=y;
- }
- // Here are some improvements from the original version, it checks
- // to see if the entered number has a square root and if this is equal
- // to the integer value of the square root then it can't be a prime
- // number (see boolean prime below):
- double squareRoot = Math.sqrt(x);
- boolean dividesIntoInt = false;
- int i=5;
- // This will check to see if the number enetered (or currently passed to the
- // sub-routine) has any divisors above 5 that produces a whole number
- // other than itself, of course:
- while(i<y)
- {
- // See http://floating-point-gui.de/ for a guide on using floating point
- // numbers (might not be relevant to Java, but it's good to know):
- float z=(float)x/i;
- if((int)z==z && x!=i)
- {
- dividesIntoInt = true;
- i=y;
- }
- // Increase index by two (so keeping each number odd):
- ++i;
- ++i;
- }
- // Here's the condition to test for prime numbers. Most prime numbers have
- // will have a remainder if divided by 3. This now checks if number entered
- // has a square root that is a whole number as well:
- boolean prime= (x==2 || x==3 || x==5) ||
- (x%2!=0 && x%3!=0 && x%5!=0 && (int)squareRoot!=squareRoot) &&
- !dividesIntoInt;
- if(prime)
- {
- System.out.format("%d ",x);
- }
- if(list && x<y)
- {
- // Here is the recursive call - in other words, this function
- // is calling itself, increasing x by one as it does. Remember, to
- // understand recursive programming, you must first understand
- // recursive programming ;-)
- primeNumber(x+1, y, list);
- }
- if(list && x==y)
- {
- System.out.println("are all prime numbers.");
- }
- if(!list && prime)
- {
- System.out.println("is a prime number");
- return;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement