usefulMath

Java program. A collection of math operations not found in the Math class.

/*
 * UsefulMath.java
 *
 * Version:
 *     $Id: UsefulMath.java,v 1.23 2008/03/31 05:11:23 mwn4938 Exp $
 *
 * Revisions:
 *     $Log: UsefulMath.java,v $
 *     Revision 1.23  2008/03/31 05:11:23  mwn4938
 *     Fixed typed in prime(double)
 *
 *     Revision 1.22  2008/03/20 05:00:08  mwn4938
 *     Removed base conversion. It turns out toString in Double and
 *     Integer already does it.
 *
 *     Revision 1.21  2008/02/19 02:52:55  mwn4938
 *     Fixed type problem in prime(double)
 *
 *     Revision 1.20  2007/10/22 07:29:42  mwn4938
 *     removed extra imports
 *
 *     Revision 1.19  2007/10/22 07:26:07  mwn4938
 *     reduced the number of compares in all prime methods.
 *
 *     Revision 1.18  2007/10/22 07:22:43  mwn4938
 *     added a check to frac for fractions reduced to whole numbers.
 *
 *     Revision 1.17  2007/09/22 23:47:50  mwn4938
 *     added a double primeFactor method and an int prime method.
 *
 *     Revision 1.16  2007/04/20 23:03:01  mwn4938
 *     changed base conversion so that it can handle bigger numbers.
 *
 *     Revision 1.15  2007/04/20 19:43:47  mwn4938
 *     added "out of range" errors to newBase
 *
 *     Revision 1.14  2007/04/12 04:31:14  mwn4938
 *     filled in more comments
 *
 *     Revision 1.13  2007/04/12 04:28:24  mwn4938
 *     finished base conversion
 *
 *     Revision 1.12  2007/04/11 05:15:43  mwn4938
 *     changed the order of if statements in the prime method to make sure that
 *     2 is considered prime.
 *
 *     Revision 1.11  2007/03/07 01:24:51  mwn4938
 *     sped up the prime methods by testing only odds.
 *
 *     Revision 1.10  2007/01/30 06:41:55  mwn4938
 *     added IllegalArgumentException to frac()'s when denom is 0.
 *
 *     Revision 1.9  2006/12/14 07:21:22  mwn4938
 *     changed prime methods to contain one less if statement.
 *
 *     Revision 1.8  2006/12/14 07:20:11  mwn4938
 *     added method comments and changed the prime methods so that they don't
 *     create a return variable.
 *
 *     Revision 1.7  2006/06/05 01:01:18  mwn4938
 *     made prime and gcf more efficient
 *
 *     Revision 1.6  2006/04/04 17:14:02  mwn4938
 *     got rid of recursion in intFact methods.
 *
 *     Revision 1.5  2006/03/15 22:06:42  mwn4938
 *     started work on base conversion
 *
 *     Revision 1.4  2006/03/15 03:59:53  mwn4938
 *     reverted to v1.2 last change unneeded
 *
 *     Revision 1.2  2006/03/15 02:07:20  mwn4938
 *     fixed gcf's so that they default to 1 instead of 0
 *
 *     Revision 1.1  2006/03/14 19:15:21  mwn4938
 *     initial shared make
 *
 *     Revision 1.1  2006/01/18 03:04:44  mwn4938
 *     sent to repository.
 *
 */

import java.util.LinkedList;

/**
 * Contains several maybe useful math operations not included in the Math class.
 *
 * @author Mike Neurohr (mwn4938@rit.edu)
 */
public class UsefulMath{
    /**
     * Will determine if a number is prime
     *
     * @param a determine the primality of this number
     * @return true if a is prime, false otherwise
     */
    public static boolean prime(double a){
        if(a == 2){
            return true;
        }else if(a <= 1 || a % 2 == 0){
            return false;
        }else{
            for(double n= 3; n <= (double)Math.sqrt(a); n+= 2){
                if(a % n == 0){ return false; }
            }
        }
        return true;
    }


    /**
     * Calculates the factorial (!) of a.
     *
     * @param a the number to factorial
     * @return a!
     * @throws IllegalArgumentException
     *             when a<0
     */
    public static double intFact(long a) throws IllegalArgumentException{
        if(a < 0){ throw new IllegalArgumentException(
                "Number must be positive."); }
        double retVal= 1;
        for(double i= a; i > 1; i--){
            retVal*= i;
        }
        return retVal;
    }

    /**
     * Reduces a fraction.
     *
     * @param numer the numerator
     * @param denom the denominator
     * @return a String in the form "numerator / denominator"
     * @throws IllegalArgumentException
     *             when denom is 0.
     */
    public static String frac(long numer, long denom)
            throws IllegalArgumentException{
        if(denom == 0)
            throw new IllegalArgumentException("Denominator cannot be 0.");
        String ret$= "";
        long factor= gcf(numer, denom);
        long newNumer= numer / factor;
        long newDenom= denom / factor;
        ret$= (newDenom != 1) ? (newNumer + " / " + newDenom) : newNumer + "";
        return ret$;
    }

    /**
     * Calculates the greatest common factor of two numbers.
     *
     * @param a the first number
     * @param b the second number
     * @return the gcf of a and b
     */
    public static int gcf(int a, int b){
        int retVal= 1;
        int factor= 0;
        factor= Math.max(a, b);
        for(int loop= factor; loop > 1; loop--){
            if(a % loop == 0 && b % loop == 0){
                retVal= loop;
                loop= 1;
            }
        }
        return retVal;
    }

    /**
     * Calculates the greatest common factor of two numbers.
     *
     * @param a the first number
     * @param b the second number
     * @return the gcf of a and b
     */
    public static long gcf(long a, long b){
        long retVal= 1;
        long factor= 0;
        factor= Math.max(a, b);
        for(long loop= factor; loop > 1; loop--){
            if(a % loop == 0 && b % loop == 0){
                retVal= loop;
                loop= 1;
            }
        }
        return retVal;
    }
    
    /**
     * Generates a list of the prime factors of a number.
     *
     * @param a the number to factorize
     * @return a LinkedList of the prime factors
     */
    public static LinkedList<Double> primeFactor(double a){
        LinkedList<Double> ans= new LinkedList<Double>();
        for(double i= 2; i <= a && a != 1; i++){
            if(prime(i) && a % i == 0){
                ans.add(i);
                a= a / i;
                i= 1;
            }
        }
        return ans;
    }

    /**
     * Generates a list of the prime factors of a number.
     *
     * @param a the number to factorize
     * @return a LinkedList of the prime factors
     */
    public static LinkedList<Integer> primeFactor(int a){
        LinkedList<Integer> ans= new LinkedList<Integer>();
        for(int i= 2; i <= a && a != 1; i++){
            if(prime(i) && a % i == 0){
                ans.add(i);
                a= a / i;
                i= 1;
            }
        }
        return ans;
    }
}