001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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 int[] generatePrimes(int max) {
077                    if (max < 2) {
078                            return new int[0];
079                    }
080                    else {
081                            boolean[] crossedOut = new boolean[max + 1];
082    
083                            for (int i = 2; i < crossedOut.length; i++) {
084                                    crossedOut[i] = false;
085                            }
086    
087                            int limit = (int)Math.sqrt(crossedOut.length);
088    
089                            for (int i = 2; i <= limit; i++) {
090                                    if (!crossedOut[i]) {
091                                            for (int multiple = 2 * i; multiple < crossedOut.length;
092                                                            multiple += i) {
093    
094                                                    crossedOut[multiple] = true;
095                                            }
096                                    }
097                            }
098    
099                            int uncrossedCount = 0;
100    
101                            for (int i = 2; i < crossedOut.length; i++) {
102                                    if (!crossedOut[i]) {
103                                            uncrossedCount++;
104                                    }
105                            }
106    
107                            int[] result = new int[uncrossedCount];
108    
109                            for (int i = 2, j = 0; i < crossedOut.length; i++) {
110                                    if (!crossedOut[i]) {
111                                            result[j++] = i;
112                                    }
113                            }
114    
115                            return result;
116                    }
117            }
118    
119            public static boolean isEven(int x) {
120                    if ((x % 2) == 0) {
121                            return true;
122                    }
123    
124                    return false;
125            }
126    
127            public static boolean isOdd(int x) {
128                    return !isEven(x);
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    }