brainfrz

Simplifying Radicals in Java

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