brainfrz

Untitled

Oct 3rd, 2015
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.21 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class SimpRad4
  4. {
  5.     public static final String SQUARE_ROOT_SYMBOL = "\u221A";
  6.     public static final int INPUT_FAILURE = -1;
  7.  
  8.  
  9.     static long[] simplify( long square )
  10.     {
  11.         long outside = 1;
  12.         long inside = square;
  13.         long[] simplified = { 1, square };
  14.  
  15.         // Check if it's already a perfect square
  16.         outside = (long)Math.sqrt(square);
  17.         if ((outside * outside) == square)
  18.         {
  19.             simplified[0] = outside;
  20.             simplified[1] = 1;
  21.             return simplified;
  22.         }
  23.  
  24.         // Find all the squares that could be factors and see if they are
  25.         for (long factor = 2, sqr = 4; sqr <= (square / 2); factor++, sqr = (factor * factor))
  26.         {
  27.             // Is this square a factor?
  28.             if ((square % sqr) == 0)
  29.             {
  30.                 simplified[0] = factor;
  31.                 simplified[1] = square / sqr;
  32.             }
  33.         }
  34.  
  35.         // Otherwise, since it hasn't simplified, return the original radical
  36.         return simplified;
  37.     }
  38.  
  39.     public static String display( long radicand, long multiplier, long newRad, boolean negative )
  40.     {
  41.         String i = negative ? "i" : "";
  42.         String negativeStr = negative ? "-" : "";
  43.  
  44.  
  45.         // If it's already simplified
  46.         if (multiplier == 1)
  47.         {
  48.             return (SQUARE_ROOT_SYMBOL + negativeStr + radicand + " = " + i
  49.                         + SQUARE_ROOT_SYMBOL + newRad);
  50.         }
  51.         // If it's a perfect square
  52.         else if (newRad == 1)
  53.         {
  54.             return (SQUARE_ROOT_SYMBOL + negativeStr + radicand + " = " + multiplier + i);
  55.         }
  56.         else
  57.         {
  58.             return (SQUARE_ROOT_SYMBOL + negativeStr + radicand + " = " + multiplier + i
  59.                         + SQUARE_ROOT_SYMBOL + newRad);
  60.         }
  61.     }
  62.  
  63.  
  64.  
  65.     public static void main( String[] args )
  66.     {
  67.         Scanner user_input = new Scanner(System.in);
  68.         long radicand = INPUT_FAILURE;
  69.         long[] rads;
  70.         boolean isNegative = false;
  71.         String arg;
  72.        
  73.         try {
  74.             arg = args[0];
  75.             radicand = Long.parseLong(arg);
  76.         } catch( ArrayIndexOutOfBoundsException e ) {
  77.             arg = "";
  78.         } catch( NumberFormatException e2 ) {
  79.             arg = "";
  80.         }
  81.  
  82.  
  83.         while (radicand < 0)
  84.         {
  85.             System.out.print("Please enter the current radicand: ");
  86.  
  87.             try {
  88.                 radicand = Long.parseLong(user_input.next());
  89.             } catch (NumberFormatException e) {
  90.                 System.out.print("That's not an integer! ");
  91.                 radicand = INPUT_FAILURE;
  92.                 continue;
  93.             }
  94.            
  95.             if (radicand < 0)
  96.             {
  97.                 radicand = -radicand;
  98.                 isNegative = true;
  99.             }
  100.  
  101.         }
  102.  
  103.         // Timing for optimal testing purposes
  104.         long startTime = System.nanoTime();
  105.  
  106.         rads = simplify(radicand);
  107.         System.out.println(display(radicand, rads[0], rads[1], isNegative));
  108.  
  109.         long endTime = System.nanoTime();
  110.         System.out.println("This took " + (endTime - startTime) + "ns.");
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment