1  /*
     2   * Copyright the original author or authors.
     3   * 
     4   * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   * 
     8   *      http://www.mozilla.org/MPL/MPL-1.1.html
     9   * 
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  import org.as2lib.core.BasicClass;
    18  import org.as2lib.data.type.*;
    19  
    20  /**
    21   * {@code MathUtil} contains fundamental math operations.
    22   *
    23   * @author Christophe Herreman
    24   * @author Martin Heidegger
    25   * @author Simon Wacker
    26   */
    27  class org.as2lib.util.MathUtil extends BasicClass {
    28  	
    29  	/**
    30  	 * Checks if the passed-in integer {@code n} is odd.
    31  	 * 
    32  	 * @param n the integer to check
    33  	 * @return {@code true} if {@code n} is odd else {@code false}
    34  	 */
    35  	public static function isOdd(n:Integer):Boolean {
    36  		return Boolean(n%2);
    37  	}
    38  	
    39  	/**
    40  	 * Checks if the passed-in integer {@code n} is even.
    41  	 *
    42  	 * @param n the integer to check
    43  	 * @return {@code true} if {@code n} is even else {@code false}
    44  	 */	
    45  	public static function isEven(n:Integer):Boolean {
    46  		return (n%2 == 0);
    47  	}
    48  	
    49  	/**
    50  	 * Checks if the passed-in number {@code n} is an integer.
    51  	 *
    52  	 * @param n the number to check
    53  	 * @return {@code true} if {@code n} is an integer else {@code false}
    54  	 */	
    55  	public static function isInteger(n:Number):Boolean {
    56  		return (n%1 == 0);
    57  	}
    58  
    59  	/**
    60  	 * Checks if the passed-in number {@code n} is natural.
    61  	 *
    62  	 * @param n the number to check
    63  	 * @return {@code true} if {@code n} is natural else {@code false}
    64  	 */	
    65  	public static function isNatural(n:Number):Boolean {
    66  		return (n >= 0 && n%1 == 0);
    67  	} 
    68  	
    69  	/**
    70  	 * Checks if the passed-in number {@code n} is a prime.
    71  	 * 
    72  	 * <p>A prime number is a positive integer that has no positive integer divisors
    73  	 * other than 1 and itself.
    74  	 * 
    75  	 * @param n the number to check
    76  	 * @return {@code true} if {@code n} is a prime else {@code false}
    77  	 */	
    78  	public static function isPrime(n:NaturalNumber):Boolean {
    79  		if (n == 1) return false;
    80  		if (n == 2) return true;
    81  		if (n % 2 == 0) return false;
    82  		for (var i:Number = 3, e:Number = Math.sqrt(n); i <= e; i += 2) {
    83  			if (n % i == 0){
    84  				return false;
    85  			}
    86  		}
    87  		return true;
    88  	}
    89  		
    90  	/**
    91  	 * Calculates the factorial of the passed-in number {@code n}.
    92  	 *
    93  	 * @param n the number to calculate the factorial of
    94  	 * @return the factorial of {@code n}
    95  	 */
    96  	public static function factorial(n:NaturalNumberIncludingZero):Number {
    97  		if (n == 0) {
    98  			return 1;
    99  		}
   100  		var d:Number = n.valueOf(); // Performance Speed up (this way the instance will not be used anymore
   101  		var i:Number = d-1;
   102  		while (i) {
   103  			d = d*i;
   104  			i--;
   105  		}
   106  		return d;
   107  	}
   108  		
   109  	/**
   110  	 * Returns an array with all divisors of the passed-in number {@code n}
   111  	 * 
   112  	 * @param n the number to return the divisors of
   113  	 * @return an array that contains the divisors of {@code n}
   114  	 */
   115  	public static function getDivisors(n:NaturalNumberIncludingZero):Array {
   116  		var r:Array = new Array();
   117  		for (var i:Number = 1, e:Number = n/2; i <= e; i++) {
   118  			if (n % i == 0){
   119  				r.push(i);
   120  			}
   121  		}
   122  		if (n != 0) r.push(n.valueOf());
   123  		return r;
   124  	}
   125  	
   126  	/**
   127  	 * Rounds the passed-in number {@code n} to the nearest value.
   128  	 * 
   129  	 * <p>It works basically the same as the {@code Math.round} method, but it adds a
   130  	 * new argument to specify the number of decimal spaces.
   131  	 * 
   132  	 * @param n the number to round
   133  	 * @param c the number of decimal spaces
   134  	 * @returns the rounded number
   135  	 */
   136  	public static function round(n:Number, c:Number):Number {
   137  		var r:Number = Math.pow(10,c);
   138  		return Math.round(n*r)/r;
   139  	}
   140  	
   141  	/**
   142  	 * Floors the passed-in number {@code n}.
   143  	 * 
   144  	 * <p>It works basically the same as the {@code Math.floor} method, but it adds a
   145  	 * new argument to specify the number of decimal spaces.
   146  	 * 
   147  	 * @param n the number to round
   148  	 * @param c the number of decimal spaces
   149  	 * @returns the rounded number
   150  	 */
   151  	public static function floor(n:Number, c:Number):Number {
   152  		var r:Number = Math.pow(10,c);
   153  		return Math.floor(n*r)/r;
   154  	}  
   155  	
   156  	/**
   157  	 * Private constructor.
   158  	 */
   159  	private function MathUtil() {
   160  	}
   161  	
   162  }