1 //******************************************************************************
2 //
3 // File: Mathe.java
4 // Package: edu.rit.util
5 // Unit: Class edu.rit.util.Mathe
6 //
7 // This Java source file is copyright (C) 2010 by Alan Kaminsky. All rights
8 // reserved. For further information, contact the author, Alan Kaminsky, at
9 // ark@cs.rit.edu.
10 //
11 // This Java source file is part of the Parallel Java Library ("PJ"). PJ is free
12 // software; you can redistribute it and/or modify it under the terms of the GNU
13 // General Public License as published by the Free Software Foundation; either
14 // version 3 of the License, or (at your option) any later version.
15 //
16 // PJ is distributed in the hope that it will be useful, but WITHOUT ANY
17 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 // A PARTICULAR PURPOSE. See the GNU General Public License for more details.
19 //
20 // Linking this library statically or dynamically with other modules is making a
21 // combined work based on this library. Thus, the terms and conditions of the GNU
22 // General Public License cover the whole combination.
23 //
24 // As a special exception, the copyright holders of this library give you
25 // permission to link this library with independent modules to produce an
26 // executable, regardless of the license terms of these independent modules, and
27 // to copy and distribute the resulting executable under terms of your choice,
28 // provided that you also meet, for each linked independent module, the terms
29 // and conditions of the license of that module. An independent module is a module
30 // which is not derived from or based on this library. If you modify this library,
31 // you may extend this exception to your version of the library, but you are not
32 // obligated to do so. If you do not wish to do so, delete this exception
33 // statement from your version.
34 //
35 // A copy of the GNU General Public License is provided in the file gpl.txt. You
36 // may also obtain a copy of the GNU General Public License on the World Wide
37 // Web at http://www.gnu.org/licenses/gpl.html.
38 //
39 //******************************************************************************
40 package edu.rit.util;
41
42 /**
43 * Class Mathe provides useful mathematical operations. The class is named
44 * "Mathe" so the compiler won't confuse it with class
45 * org.apache.commons.math3.util.FastMath.
46 *
47 * @author Alan Kaminsky
48 * @version 11-Feb-2010
49 */
50 public class Mathe {
51
52 // Prevent construction.
53 private Mathe() {
54 }
55
56 // Exported operations.
57 /**
58 * Compute the integer square root of the integer <code>x</code>. The value
59 * floor(<code>x</code><SUP>1/2</SUP>) is returned. The answer is calculated
60 * using an exact integer algorithm taken from:
61 * <UL>
62 * <LI>
63 * J. Crenshaw. Integer square roots.
64 * <A HREF="http://www.embedded.com/98/9802fe2.htm"
65 * TARGET="_top">http://www.embedded.com/98/9802fe2.htm</A>
66 * </UL>
67 *
68 * @param x Input.
69 * @return Floor(<code>x</code><SUP>1/2</SUP>).
70 * @exception ArithmeticException (unchecked exception) Thrown if <code>x</code>
71 * < 0.
72 */
73 public static int sqrt(int x) {
74 if (x < 0) {
75 throw new ArithmeticException("Mathe.sqrt(): x < 0");
76 }
77 int rem = 0;
78 int root = 0;
79 for (int i = 0; i < 16; ++i) {
80 root <<= 1;
81 rem = (rem << 2) | (x >>> 30);
82 x <<= 2;
83 ++root;
84 if (root <= rem) {
85 rem -= root;
86 ++root;
87 } else {
88 --root;
89 }
90 }
91 return root >>> 1;
92 }
93
94 // Unit test main program.
95 // /**
96 // * Unit test main program.
97 // */
98 // public static void main
99 // (String[] args)
100 // {
101 // for (int x = 0; x <= 100000000; ++ x)
102 // {
103 // if ((x % 10000000) == 0) System.out.printf ("%d%n", x);
104 // int y = Mathe.sqrt(x);
105 // if (y*y > x || x >= (y + 1)*(y + 1))
106 // {
107 // System.out.printf ("x = %d, sqrt(x) = %d, fail%n", x, y);
108 // }
109 // }
110 // }
111 }