1 /* Test mpz_lcm and mpz_lcm_ui.
3 Copyright 2001 Free Software Foundation, Inc.
5 This file is part of the GNU MP Library.
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 3 of the License, or (at your
10 option) any later version.
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.
17 You should have received a copy of the GNU Lesser General Public License
18 along with the GNU MP Library. If not, see http://www.gnu.org/licenses/. */
31 check_all (mpz_ptr want, mpz_srcptr x_orig, mpz_srcptr y_orig)
34 int negx, negy, swap, inplace;
37 mpz_init_set (x, x_orig);
38 mpz_init_set (y, y_orig);
40 for (swap = 0; swap < 2; swap++)
44 for (negx = 0; negx < 2; negx++)
48 for (negy = 0; negy < 2; negy++)
52 for (inplace = 0; inplace <= 1; inplace++)
55 { mpz_set (got, x); mpz_lcm (got, got, y); }
58 MPZ_CHECK_FORMAT (got);
60 if (mpz_cmp (got, want) != 0)
62 printf ("mpz_lcm wrong, inplace=%d\n", inplace);
66 mpz_trace ("got", got);
67 mpz_trace ("want", want);
71 if (mpz_fits_ulong_p (y))
73 unsigned long yu = mpz_get_ui (y);
75 { mpz_set (got, x); mpz_lcm_ui (got, got, yu); }
77 mpz_lcm_ui (got, x, yu);
79 if (mpz_cmp (got, want) != 0)
81 printf ("mpz_lcm_ui wrong, inplace=%d\n", inplace);
82 printf ("yu=%lu\n", yu);
100 static unsigned long prime[] = {
101 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,
102 101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,
103 191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,
104 281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,
105 389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,
114 /* New prime each time. */
115 mpz_set_ui (want, 1L);
116 for (i = 0; i < numberof (prime); i++)
119 mpz_set_ui (y, prime[i]);
120 mpz_mul_ui (want, want, prime[i]);
121 check_all (want, x, y);
124 /* Old prime each time. */
126 for (i = 0; i < numberof (prime); i++)
128 mpz_set_ui (y, prime[i]);
129 check_all (want, x, y);
132 /* One old, one new each time. */
133 mpz_set_ui (want, prime[0]);
134 for (i = 1; i < numberof (prime); i++)
137 mpz_set_ui (y, prime[i] * prime[i-1]);
138 mpz_mul_ui (want, want, prime[i]);
139 check_all (want, x, y);
142 /* Triplets with A,B in x and B,C in y. */
143 mpz_set_ui (want, 1L);
146 for (i = 0; i+2 < numberof (prime); i += 3)
148 mpz_mul_ui (want, want, prime[i]);
149 mpz_mul_ui (want, want, prime[i+1]);
150 mpz_mul_ui (want, want, prime[i+2]);
152 mpz_mul_ui (x, x, prime[i]);
153 mpz_mul_ui (x, x, prime[i+1]);
155 mpz_mul_ui (y, y, prime[i+1]);
156 mpz_mul_ui (y, y, prime[i+2]);
158 check_all (want, x, y);
170 main (int argc, char *argv[])