brainfrz

Simplifying Radicals in Java

Sep 12th, 2015
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.35 KB | None | 0 0
  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.  
  52.         try {
  53.             arg = args[0];
  54.             radicand = Integer.parseInt(arg);
  55.         } catch( ArrayIndexOutOfBoundsException e ) {
  56.             arg = "";
  57.         } catch( NumberFormatException e2 ) {
  58.             arg = "";
  59.         }
  60.  
  61.         while( radicand < 0 )
  62.         {
  63.             System.out.print("Please enter the current radicand:  ");
  64.             arg = user_input.next();
  65.  
  66.             try {
  67.                 radicand = Integer.parseInt(arg);
  68.             } catch( NumberFormatException e ) {
  69.                 System.out.print("That's not an integer! ");
  70.                 continue;
  71.             }
  72.  
  73.             if( radicand < 0 )
  74.             {
  75.                 System.out.print("That's a negative. ");
  76.             }
  77.         }
  78.  
  79.         rads = simplify(radicand);
  80.         System.out.println(SQUARE_ROOT_SYMBOL + radicand + " = " + rads[0] + SQUARE_ROOT_SYMBOL +
  81.                                 rads[1]);
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment