3 * $Id: mp-sqrt.c,v 1.2 2000/10/08 12:02:21 mdw Exp $
5 * Compute integer square roots
7 * (c) 2000 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.2 2000/10/08 12:02:21 mdw
34 * Use @MP_EQ@ instead of @MP_CMP@.
36 * Revision 1.1 2000/06/22 19:01:44 mdw
37 * Compute (approximations to) integer square roots.
41 /*----- Header files ------------------------------------------------------*/
45 /*----- Main code ---------------------------------------------------------*/
47 /* --- @mp_sqrt@ --- *
49 * Arguments: @mp *d@ = pointer to destination integer
50 * @mp *a@ = (nonnegative) integer to take square root of
52 * Returns: The largest integer %$x$% such that %$x^2 \le a$%.
54 * Use: Computes integer square roots.
56 * The current implementation isn't very good: it uses the
57 * Newton-Raphson method to find an approximation to %$a$%. If
58 * there's any demand for a better version, I'll write one.
61 mp
*mp_sqrt(mp
*d
, mp
*a
)
64 mp
*q
= MP_NEW
, *r
= MP_NEW
;
66 /* --- Sanity preservation --- */
68 assert(((void)"imaginary root in mp_sqrt", !(a
->f
& MP_NEG
)));
70 /* --- Deal with trivial cases --- */
79 /* --- Find an initial guess of about the right size --- */
87 /* --- Main approximation --- *
89 * We use the Newton-Raphson recurrence relation
91 * %$x_{i+1} = x_i - \frac{x_i^2 - a}{2 x_i}$%
93 * We inspect the term %$q = x^2 - a$% to see when to stop. Increasing
94 * %$x$% is pointless when %$-q < 2 x + 1$%.
105 if (MP_CMP(q
, <=, r
))
108 mp_div(&r
, &q
, q
, d
);
111 d
= mp_sub(d
, d
, MP_ONE
);
116 /* --- Finished, at last --- */
124 /*----- Test rig ----------------------------------------------------------*/
128 #include <mLib/testrig.h>
130 static int verify(dstr
*v
)
132 mp
*a
= *(mp
**)v
[0].buf
;
133 mp
*qq
= *(mp
**)v
[1].buf
;
134 mp
*q
= mp_sqrt(MP_NEW
, a
);
139 fputs("\n*** sqrt failed", stderr
);
140 fputs("\n*** a = ", stderr
); mp_writefile(a
, stderr
, 10);
141 fputs("\n*** result = ", stderr
); mp_writefile(q
, stderr
, 10);
142 fputs("\n*** expect = ", stderr
); mp_writefile(qq
, stderr
, 10);
149 assert(mparena_count(MPARENA_GLOBAL
) == 0);
154 static test_chunk tests
[] = {
155 { "sqrt", verify
, { &type_mp
, &type_mp
, 0 } },
159 int main(int argc
, char *argv
[])
162 test_run(argc
, argv
, tests
, SRCDIR
"/tests/mp");
168 /*----- That's all, folks -------------------------------------------------*/