001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    
020    import java.text.NumberFormat;
021    
022    import java.util.HashMap;
023    import java.util.Map;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     */
028    public class MathUtil {
029    
030            public static int base2Log(long x) {
031                    return _base2LogValues.get(x);
032            }
033    
034            public static long base2Pow(int x) {
035                    if (x == 0) {
036                            return 1;
037                    }
038                    else {
039                            return 2L << (x - 1);
040                    }
041            }
042    
043            public static int factorial(int x) {
044                    if (x < 0) {
045                            return 0;
046                    }
047    
048                    int factorial = 1;
049    
050                    while (x > 1) {
051                            factorial = factorial * x;
052                            x = x - 1;
053                    }
054    
055                    return factorial;
056            }
057    
058            public static double format(double x, int max, int min) {
059                    NumberFormat nf = NumberFormat.getInstance();
060    
061                    nf.setMaximumFractionDigits(max);
062                    nf.setMinimumFractionDigits(min);
063    
064                    try {
065                            Number number = nf.parse(nf.format(x));
066    
067                            x = number.doubleValue();
068                    }
069                    catch (Exception e) {
070                            _log.error(e.getMessage());
071                    }
072    
073                    return x;
074            }
075    
076            public static boolean isEven(int x) {
077                    if ((x % 2) == 0) {
078                            return true;
079                    }
080    
081                    return false;
082            }
083    
084            public static boolean isOdd(int x) {
085                    return !isEven(x);
086            }
087    
088            public static int[] generatePrimes(int max) {
089                    if (max < 2) {
090                            return new int[0];
091                    }
092                    else {
093                            boolean[] crossedOut = new boolean[max + 1];
094    
095                            for (int i = 2; i < crossedOut.length; i++) {
096                                    crossedOut[i] = false;
097                            }
098    
099                            int limit = (int)Math.sqrt(crossedOut.length);
100    
101                            for (int i = 2; i <= limit; i++) {
102                                    if (!crossedOut[i]) {
103                                            for (int multiple = 2 * i; multiple < crossedOut.length;
104                                                            multiple += i) {
105    
106                                                    crossedOut[multiple] = true;
107                                            }
108                                    }
109                            }
110    
111                            int uncrossedCount = 0;
112    
113                            for (int i = 2; i < crossedOut.length; i++) {
114                                    if (!crossedOut[i]) {
115                                            uncrossedCount++;
116                                    }
117                            }
118    
119                            int[] result = new int[uncrossedCount];
120    
121                            for (int i = 2, j = 0; i < crossedOut.length; i++) {
122                                    if (!crossedOut[i]) {
123                                            result[j++] = i;
124                                    }
125                            }
126    
127                            return result;
128                    }
129            }
130    
131            private static Log _log = LogFactoryUtil.getLog(MathUtil.class);
132    
133            private static Map<Long, Integer> _base2LogValues =
134                    new HashMap<Long, Integer>();
135    
136            static {
137                    _base2LogValues.put(0L, Integer.MIN_VALUE);
138    
139                    for (int i = 0; i < 63; i++) {
140                            _base2LogValues.put(base2Pow(i), i);
141                    }
142            }
143    
144    }