version 1.1.1.2, 2000/12/01 05:45:11 |
version 1.1.1.3, 2003/08/25 16:06:33 |
|
|
/* mpz/lcm.c: Calculate the least common multiple of two integers. |
/* mpz_lcm -- mpz/mpz least common multiple. |
|
|
Copyright (C) 1996, 2000 Free Software Foundation, Inc. |
Copyright 1996, 2000, 2001 Free Software Foundation, Inc. |
|
|
This file is part of the GNU MP Library. |
This file is part of the GNU MP Library. |
|
|
Line 23 MA 02111-1307, USA. */ |
|
Line 23 MA 02111-1307, USA. */ |
|
#include "gmp-impl.h" |
#include "gmp-impl.h" |
#include "longlong.h" |
#include "longlong.h" |
|
|
void *_mpz_realloc (); |
|
|
|
void |
void |
#if __STDC__ |
|
mpz_lcm (mpz_ptr r, mpz_srcptr u, mpz_srcptr v) |
mpz_lcm (mpz_ptr r, mpz_srcptr u, mpz_srcptr v) |
#else |
|
mpz_lcm (r, u, v) |
|
mpz_ptr r; |
|
mpz_srcptr u; |
|
mpz_srcptr v; |
|
#endif |
|
{ |
{ |
mpz_t g; |
mpz_t g; |
mp_size_t usize, vsize, size; |
mp_size_t usize, vsize, size; |
TMP_DECL (marker); |
TMP_DECL (marker); |
|
|
TMP_MARK (marker); |
usize = SIZ (u); |
|
vsize = SIZ (v); |
usize = ABS (SIZ (u)); |
|
vsize = ABS (SIZ (v)); |
|
|
|
if (usize == 0 || vsize == 0) |
if (usize == 0 || vsize == 0) |
{ |
{ |
SIZ (r) = 0; |
SIZ (r) = 0; |
return; |
return; |
} |
} |
|
usize = ABS (usize); |
|
vsize = ABS (vsize); |
|
|
|
if (vsize == 1) |
|
{ |
|
mp_limb_t vl, gl, c; |
|
mp_srcptr up; |
|
mp_ptr rp; |
|
|
|
one: |
|
MPZ_REALLOC (r, usize+1); |
|
|
|
up = PTR(u); |
|
vl = PTR(v)[0]; |
|
gl = mpn_gcd_1 (up, usize, vl); |
|
vl /= gl; |
|
|
|
rp = PTR(r); |
|
c = mpn_mul_1 (rp, up, usize, vl); |
|
rp[usize] = c; |
|
usize += (c != 0); |
|
SIZ(r) = usize; |
|
return; |
|
} |
|
|
|
if (usize == 1) |
|
{ |
|
usize = vsize; |
|
MPZ_SRCPTR_SWAP (u, v); |
|
goto one; |
|
} |
|
|
|
TMP_MARK (marker); |
size = MAX (usize, vsize); |
size = MAX (usize, vsize); |
MPZ_TMP_INIT (g, size); |
MPZ_TMP_INIT (g, size); |
|
|
mpz_gcd (g, u, v); |
mpz_gcd (g, u, v); |
mpz_divexact (g, u, g); |
mpz_divexact (g, u, g); |
mpz_mul (r, g, v); |
mpz_mul (r, g, v); |
|
|
|
SIZ (r) = ABS (SIZ (r)); /* result always positive */ |
|
|
TMP_FREE (marker); |
TMP_FREE (marker); |
} |
} |