1 | /* Test mpz_perfect_square_p and mpz_perfect_power_p |
---|
2 | |
---|
3 | Copyright 2000, 2001 Free Software Foundation, Inc. |
---|
4 | |
---|
5 | This file is part of the GNU MP Library. |
---|
6 | |
---|
7 | The GNU MP Library is free software; you can redistribute it and/or modify |
---|
8 | it under the terms of the GNU Lesser General Public License as published by |
---|
9 | the Free Software Foundation; either version 2.1 of the License, or (at your |
---|
10 | option) any later version. |
---|
11 | |
---|
12 | The GNU MP Library is distributed in the hope that it will be useful, but |
---|
13 | WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
---|
14 | or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
---|
15 | License for more details. |
---|
16 | |
---|
17 | You should have received a copy of the GNU Lesser General Public License |
---|
18 | along with the GNU MP Library; see the file COPYING.LIB. If not, write to |
---|
19 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
---|
20 | MA 02111-1307, USA. */ |
---|
21 | |
---|
22 | #include <stdio.h> |
---|
23 | #include <stdlib.h> |
---|
24 | |
---|
25 | #include "gmp.h" |
---|
26 | #include "gmp-impl.h" |
---|
27 | #include "tests.h" |
---|
28 | |
---|
29 | void debug_mp _PROTO ((mpz_t, int)); |
---|
30 | |
---|
31 | int |
---|
32 | main (int argc, char **argv) |
---|
33 | { |
---|
34 | mpz_t x2, x2t, x; |
---|
35 | mp_size_t x2n; |
---|
36 | int res; |
---|
37 | int i; |
---|
38 | /* int cnt = 0; */ |
---|
39 | int reps = 100000; |
---|
40 | gmp_randstate_ptr rands; |
---|
41 | mpz_t bs; |
---|
42 | |
---|
43 | tests_start (); |
---|
44 | rands = RANDS; |
---|
45 | |
---|
46 | mpz_init (bs); |
---|
47 | |
---|
48 | if (argc == 2) |
---|
49 | reps = atoi (argv[1]); |
---|
50 | |
---|
51 | mpz_init (x2); |
---|
52 | mpz_init (x); |
---|
53 | mpz_init (x2t); |
---|
54 | |
---|
55 | for (i = 0; i < reps; i++) |
---|
56 | { |
---|
57 | mpz_urandomb (bs, rands, 9); |
---|
58 | x2n = mpz_get_ui (bs); |
---|
59 | mpz_rrandomb (x2, rands, x2n); |
---|
60 | /* mpz_out_str (stdout, -16, x2); puts (""); */ |
---|
61 | |
---|
62 | res = mpz_perfect_square_p (x2); |
---|
63 | mpz_sqrt (x, x2); |
---|
64 | mpz_mul (x2t, x, x); |
---|
65 | |
---|
66 | if (res != (mpz_cmp (x2, x2t) == 0)) |
---|
67 | abort (); |
---|
68 | |
---|
69 | /* cnt += res != 0; */ |
---|
70 | } |
---|
71 | /* printf ("%d/%d perfect squares\n", cnt, reps); */ |
---|
72 | |
---|
73 | mpz_clear (bs); |
---|
74 | mpz_clear (x2); |
---|
75 | mpz_clear (x); |
---|
76 | mpz_clear (x2t); |
---|
77 | |
---|
78 | tests_end (); |
---|
79 | exit (0); |
---|
80 | } |
---|
81 | |
---|
82 | void |
---|
83 | debug_mp (mpz_t x, int base) |
---|
84 | { |
---|
85 | mpz_out_str (stderr, base, x); fputc ('\n', stderr); |
---|
86 | } |
---|