3 * Compute integer square roots
5 * (c) 2000 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
32 /*----- Main code ---------------------------------------------------------*/
34 /* --- @mp_sqrt@ --- *
36 * Arguments: @mp *d@ = pointer to destination integer
37 * @mp *a@ = (nonnegative) integer to take square root of
39 * Returns: The largest integer %$x$% such that %$x^2 \le a$%.
41 * Use: Computes integer square roots.
43 * The current implementation isn't very good: it uses the
44 * Newton-Raphson method to find an approximation to %$a$%. If
45 * there's any demand for a better version, I'll write one.
48 mp *mp_sqrt(mp *d, mp *a)
51 mp *q = MP_NEW, *r = MP_NEW;
53 /* --- Sanity preservation --- */
57 /* --- Deal with trivial cases --- */
65 /* --- Find an initial guess of about the right size --- */
72 /* --- Main approximation --- *
74 * The Newton--Raphson method finds approximate zeroes of a function by
75 * starting with a guess and repeatedly refining the guess by approximating
76 * the function near the guess by its tangent at the guess
77 * %$x$%-coordinate, using where the tangent cuts the %$x$%-axis as the new
80 * Given a function %$f(x)$% and a guess %$x_i$%, the tangent has the
81 * equation %$y = f(x_i) + f'(x_i) (x - x_i)$%, which is zero when
83 * %$\displaystyle x = x_i - \frac{f(x_i)}{f'(x_i)}
85 * We set %$f(x) = x^2 - a$%, so our recurrence will be
87 * %$\displaystyle x_{i+1} = x_i - \frac{x_i^2 - a}{2 x_i}$%
89 * It's possible to simplify this, but it's useful to see %$q = x_i^2 - a$%
90 * so that we know when to stop. We want the largest integer not larger
91 * than the true square root. If %$q > 0$% then %$x_i$% is definitely too
92 * large, and we should decrease it by at least one even if the adjustment
93 * term %$(x_i^2 - a)/2 x$% is less than one.
95 * Suppose, then, that %$q \le 0$%. Then %$(x_i + 1)^2 - a = {}$%
96 * $%x_i^2 + 2 x_i + 1 - a = q + 2 x_i + 1$%. Hence, if %$q \ge -2 x_i$%
97 * then %$x_i + 1$% is an overestimate and we should settle where we are.
98 * Otherwise, %$x_i + 1$% is an underestimate -- but, in this case the
99 * adjustment will always be at least one.
110 if (MP_CMP(q, >=, r))
113 mp_div(&r, &q, q, d);
116 d = mp_sub(d, d, MP_ONE);
121 /* --- Finished, at last --- */
129 /* --- @mp_squarep@ --- *
131 * Arguments: @mp *n@ = an integer
133 * Returns: Nonzero if and only if @n@ is a perfect square, i.e.,
134 * %$n = a^2$% for some rational integer %$a$%.
137 int mp_squarep(mp *n)
142 if (MP_NEGP(n)) return (0);
143 t = mp_sqrt(t, n); t = mp_sqr(t, t);
144 rc = MP_EQ(t, n); mp_drop(t); return (rc);
147 /*----- Test rig ----------------------------------------------------------*/
151 #include <mLib/testrig.h>
153 static int verify(dstr *v)
155 mp *a = *(mp **)v[0].buf;
156 mp *qq = *(mp **)v[1].buf;
157 mp *q = mp_sqrt(MP_NEW, a);
162 fputs("\n*** sqrt failed", stderr);
163 fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 10);
164 fputs("\n*** result = ", stderr); mp_writefile(q, stderr, 10);
165 fputs("\n*** expect = ", stderr); mp_writefile(qq, stderr, 10);
172 assert(mparena_count(MPARENA_GLOBAL) == 0);
177 static test_chunk tests[] = {
178 { "sqrt", verify, { &type_mp, &type_mp, 0 } },
182 int main(int argc, char *argv[])
185 test_run(argc, argv, tests, SRCDIR "/t/mp");
191 /*----- That's all, folks -------------------------------------------------*/