import java.util.Scanner; public class SimpRad2 { static long[] simplify( long square ) { double outside = 1; double inside = square; long[] simplified = { 1, square }; // Check if it's already a perfect square outside = Math.sqrt(square); if (outside == Math.floor(outside)) { simplified[0] = (long)outside; simplified[1] = 1; return simplified; } // Find all the squares that could be factors and see if they are long i, sqr; for (i = 2, sqr = 4; sqr <= (square / 2); i++, sqr = (i * i)) { // Is this square a factor? double in = (double)square / sqr; if (in == Math.floor(in)) { simplified[0] = i; simplified[1] = (long)in; } } // Otherwise, since it hasn't simplified, return the original radical return simplified; } public static void main( String[] args ) { String arg; Scanner user_input = new Scanner(System.in); long radicand = -1; final String SQUARE_ROOT_SYMBOL = "\u221A"; long[] rads; String i = ""; String negative = ""; try { arg = args[0]; radicand = Long.parseLong(arg); } catch( ArrayIndexOutOfBoundsException e ) { arg = ""; } catch( NumberFormatException e2 ) { arg = ""; } while (radicand < 0) { System.out.print("Please enter the current radicand: "); arg = user_input.next(); try { radicand = Long.parseLong(arg); } catch (NumberFormatException e) { System.out.print("That's not an integer! "); continue; } if (radicand < 0) { i = "i"; negative = "-"; radicand = -radicand; } } rads = simplify(radicand); // If it's already simplified if (rads[0] == 1) { System.out.println(SQUARE_ROOT_SYMBOL + negative + radicand + " = " + i + SQUARE_ROOT_SYMBOL + rads[1]); } // If it's a perfect square else if (rads[1] == 1) { System.out.println(SQUARE_ROOT_SYMBOL + negative + radicand + " = " + rads[0] + i); } else { System.out.println(SQUARE_ROOT_SYMBOL + negative + radicand + " = " + rads[0] + i + SQUARE_ROOT_SYMBOL + rads[1]); } } }