001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.util;
022:
023: import java.text.NumberFormat;
024:
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027:
028: /**
029: * <a href="MathUtil.java.html"><b><i>View Source</i></b></a>
030: *
031: * @author Brian Wing Shun Chan
032: *
033: */
034: public class MathUtil {
035:
036: public static int factorial(int x) {
037: if (x < 0) {
038: return 0;
039: }
040:
041: int factorial = 1;
042:
043: while (x > 1) {
044: factorial = factorial * x;
045: x = x - 1;
046: }
047:
048: return factorial;
049: }
050:
051: public static double format(double x, int max, int min) {
052: NumberFormat nf = NumberFormat.getInstance();
053:
054: nf.setMaximumFractionDigits(max);
055: nf.setMinimumFractionDigits(min);
056:
057: try {
058: Number number = nf.parse(nf.format(x));
059:
060: x = number.doubleValue();
061: } catch (Exception e) {
062: _log.error(e.getMessage());
063: }
064:
065: return x;
066: }
067:
068: public static boolean isEven(int x) {
069: if ((x % 2) == 0) {
070: return true;
071: }
072:
073: return false;
074: }
075:
076: public static boolean isOdd(int x) {
077: return !isEven(x);
078: }
079:
080: public static int[] generatePrimes(int max) {
081: if (max < 2) {
082: return new int[0];
083: } else {
084: boolean[] crossedOut = new boolean[max + 1];
085:
086: for (int i = 2; i < crossedOut.length; i++) {
087: crossedOut[i] = false;
088: }
089:
090: int limit = (int) Math.sqrt(crossedOut.length);
091:
092: for (int i = 2; i <= limit; i++) {
093: if (!crossedOut[i]) {
094: for (int multiple = 2 * i; multiple < crossedOut.length; multiple += i) {
095:
096: crossedOut[multiple] = true;
097: }
098: }
099: }
100:
101: int uncrossedCount = 0;
102:
103: for (int i = 2; i < crossedOut.length; i++) {
104: if (!crossedOut[i]) {
105: uncrossedCount++;
106: }
107: }
108:
109: int[] result = new int[uncrossedCount];
110:
111: for (int i = 2, j = 0; i < crossedOut.length; i++) {
112: if (!crossedOut[i]) {
113: result[j++] = i;
114: }
115: }
116:
117: return result;
118: }
119: }
120:
121: private static Log _log = LogFactory.getLog(MathUtil.class);
122:
123: }
|