1 /*
2     Copyright (C) 2009 William Hart
3 
4     This file is part of FLINT.
5 
6     FLINT is free software: you can redistribute it and/or modify it under
7     the terms of the GNU Lesser General Public License (LGPL) as published
8     by the Free Software Foundation; either version 2.1 of the License, or
9     (at your option) any later version.  See <http://www.gnu.org/licenses/>.
10 */
11 
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <gmp.h>
15 #include "flint.h"
16 #include "ulong_extras.h"
17 
main(void)18 int main(void)
19 {
20    int i, result;
21    FLINT_TEST_INIT(state);
22 
23    flint_printf("remove....");
24    fflush(stdout);
25 
26 
27 
28    for (i = 0; i < 10000 * flint_test_multiplier(); i++) /* Test random numbers */
29    {
30       mp_limb_t n1, n2, orig_n;
31       mpz_t d_n2, d_n1, d_p;
32       int exp1, exp2;
33       ulong j;
34 
35       mpz_init(d_n1);
36       mpz_init(d_n2);
37       mpz_init(d_p);
38 
39       n1 = n_randtest_not_zero(state);
40       orig_n = n1;
41 
42       for (j = 0; j < FLINT_NUM_PRIMES_SMALL/10; j++)
43       {
44          flint_mpz_set_ui(d_n1, n1);
45          flint_mpz_set_ui(d_p, flint_primes_small[j]);
46          exp1 = n_remove(&n1, flint_primes_small[j]);
47          exp2 = mpz_remove(d_n2, d_n1, d_p);
48          n2 = flint_mpz_get_ui(d_n2);
49 
50          result = ((exp1 == exp2) && (n1 == n2));
51          if (!result)
52          {
53             flint_printf("FAIL:\n");
54             flint_printf("n = %wu, exp1 = %d, exp2 = %d, n1 = %wu, n2 = %wu, p = %d\n", orig_n, exp1, exp2, n1, n2, flint_primes_small[j]);
55             abort();
56          }
57       }
58 
59       mpz_clear(d_n1);
60       mpz_clear(d_n2);
61       mpz_clear(d_p);
62    }
63 
64    for (i = 0; i < 10000 * flint_test_multiplier(); i++) /* Test perfect powers */
65    {
66       mp_limb_t n1, n2, orig_n, base;
67       mpz_t d_n2, d_n1, d_p;
68       int exp1, exp2, exp;
69       ulong j;
70 
71       mpz_init(d_n1);
72       mpz_init(d_n2);
73       mpz_init(d_p);
74 
75       base = n_randtest_not_zero(state);
76       n1 = base;
77       exp = n_randint(state, FLINT_BITS/FLINT_BIT_COUNT(n1)) + 1;
78       n1 = n_pow(base, exp);
79 
80       orig_n = n1;
81 
82       for (j = 0; j < FLINT_NUM_PRIMES_SMALL/10; j++)
83       {
84          flint_mpz_set_ui(d_n1, n1);
85          flint_mpz_set_ui(d_p, flint_primes_small[j]);
86          exp1 = n_remove(&n1, flint_primes_small[j]);
87          exp2 = mpz_remove(d_n2, d_n1, d_p);
88          n2 = flint_mpz_get_ui(d_n2);
89 
90          result = ((exp1 == exp2) && (n1 == n2));
91          if (!result)
92          {
93             flint_printf("FAIL\n");
94             flint_printf("n = %wu, exp1 = %d, exp2 = %d, n1 = %wu, n2 = %wu, p = %d\n", orig_n, exp1, exp2, n1, n2, flint_primes_small[j]);
95             abort();
96          }
97       }
98 
99       mpz_clear(d_n1);
100       mpz_clear(d_n2);
101       mpz_clear(d_p);
102    }
103 
104    FLINT_TEST_CLEANUP(state);
105 
106    flint_printf("PASS\n");
107    return 0;
108 }
109