1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.util;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  
28  import java.text.NumberFormat;
29  
30  import java.util.HashMap;
31  import java.util.Map;
32  
33  /**
34   * <a href="MathUtil.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   */
38  public class MathUtil {
39  
40      public static int base2Log(long x) {
41          return _base2LogValues.get(x);
42      }
43  
44      public static long base2Pow(int x) {
45          if (x == 0) {
46              return 1;
47          }
48          else {
49              return 2L << (x - 1);
50          }
51      }
52  
53      public static int factorial(int x) {
54          if (x < 0) {
55              return 0;
56          }
57  
58          int factorial = 1;
59  
60          while (x > 1) {
61              factorial = factorial * x;
62              x = x - 1;
63          }
64  
65          return factorial;
66      }
67  
68      public static double format(double x, int max, int min) {
69          NumberFormat nf = NumberFormat.getInstance();
70  
71          nf.setMaximumFractionDigits(max);
72          nf.setMinimumFractionDigits(min);
73  
74          try {
75              Number number = nf.parse(nf.format(x));
76  
77              x = number.doubleValue();
78          }
79          catch (Exception e) {
80              _log.error(e.getMessage());
81          }
82  
83          return x;
84      }
85  
86      public static boolean isEven(int x) {
87          if ((x % 2) == 0) {
88              return true;
89          }
90  
91          return false;
92      }
93  
94      public static boolean isOdd(int x) {
95          return !isEven(x);
96      }
97  
98      public static int[] generatePrimes(int max) {
99          if (max < 2) {
100             return new int[0];
101         }
102         else {
103             boolean[] crossedOut = new boolean[max + 1];
104 
105             for (int i = 2; i < crossedOut.length; i++) {
106                 crossedOut[i] = false;
107             }
108 
109             int limit = (int)Math.sqrt(crossedOut.length);
110 
111             for (int i = 2; i <= limit; i++) {
112                 if (!crossedOut[i]) {
113                     for (int multiple = 2 * i; multiple < crossedOut.length;
114                             multiple += i) {
115 
116                         crossedOut[multiple] = true;
117                     }
118                 }
119             }
120 
121             int uncrossedCount = 0;
122 
123             for (int i = 2; i < crossedOut.length; i++) {
124                 if (!crossedOut[i]) {
125                     uncrossedCount++;
126                 }
127             }
128 
129             int[] result = new int[uncrossedCount];
130 
131             for (int i = 2, j = 0; i < crossedOut.length; i++) {
132                 if (!crossedOut[i]) {
133                     result[j++] = i;
134                 }
135             }
136 
137             return result;
138         }
139     }
140 
141     private static Log _log = LogFactoryUtil.getLog(MathUtil.class);
142 
143     private static Map<Long, Integer> _base2LogValues =
144         new HashMap<Long, Integer>();
145 
146     static {
147         _base2LogValues.put(0L, Integer.MIN_VALUE);
148 
149         for (int i = 0; i < 63; i++) {
150             _base2LogValues.put(base2Pow(i), i);
151         }
152     }
153 
154 }