brainfrz

Simplified? Simplifying Radicals

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