brainfrz icon

Untitled

brainfrz | PRO | 10/03/15 03:11:58 PM UTC | 0 ⭐ | 809 👁️ | Never ⏰ | []
Java |

3.21 KB

|

None

|

0 👍

/

0 👎

import java.util.Scanner;
 
public class SimpRad4
{
    public static final String SQUARE_ROOT_SYMBOL = "\u221A";
    public static final int INPUT_FAILURE = -1;
 
 
    static long[] simplify( long square )
    {
        long outside = 1;
        long inside = square;
        long[] simplified = { 1, square };
 
        // Check if it's already a perfect square
        outside = (long)Math.sqrt(square);
        if ((outside * outside) == square)
        {
            simplified[0] = outside;
            simplified[1] = 1;
            return simplified;
        }
 
        // Find all the squares that could be factors and see if they are
        for (long factor = 2, sqr = 4; sqr <= (square / 2); factor++, sqr = (factor * factor))
        {
            // Is this square a factor?
            if ((square % sqr) == 0)
            {
                simplified[0] = factor;
                simplified[1] = square / sqr;
            }
        }
 
        // Otherwise, since it hasn't simplified, return the original radical
        return simplified;
    }
 
    public static String display( long radicand, long multiplier, long newRad, boolean negative )
    {
        String i = negative ? "i" : "";
        String negativeStr = negative ? "-" : "";
 
 
        // If it's already simplified
        if (multiplier == 1)
        {
            return (SQUARE_ROOT_SYMBOL + negativeStr + radicand + " = " + i
                        + SQUARE_ROOT_SYMBOL + newRad);
        }
        // If it's a perfect square
        else if (newRad == 1)
        {
            return (SQUARE_ROOT_SYMBOL + negativeStr + radicand + " = " + multiplier + i);
        }
        else
        {
            return (SQUARE_ROOT_SYMBOL + negativeStr + radicand + " = " + multiplier + i
                        + SQUARE_ROOT_SYMBOL + newRad);
        }
    }
 
 
 
    public static void main( String[] args )
    {
        Scanner user_input = new Scanner(System.in);
        long radicand = INPUT_FAILURE;
        long[] rads;
        boolean isNegative = false;
        String arg;
        
        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: ");
 
            try {
                radicand = Long.parseLong(user_input.next());
            } catch (NumberFormatException e) {
                System.out.print("That's not an integer! ");
                radicand = INPUT_FAILURE;
                continue;
            }
            
            if (radicand < 0)
            {
                radicand = -radicand;
                isNegative = true;
            }
 
        }
 
        // Timing for optimal testing purposes
        long startTime = System.nanoTime();
 
        rads = simplify(radicand);
        System.out.println(display(radicand, rads[0], rads[1], isNegative));
 
        long endTime = System.nanoTime();
        System.out.println("This took " + (endTime - startTime) + "ns.");
    }
}

Comments