| 1 |
module etc.bigint.gcd; |
|---|
| 2 |
import etc.bigint.bigint_int; |
|---|
| 3 |
|
|---|
| 4 |
/* |
|---|
| 5 |
|
|---|
| 6 |
Copyright (c) 2004, Arcane Jill |
|---|
| 7 |
|
|---|
| 8 |
All rights reserved. Intellectual Property Me Arse! |
|---|
| 9 |
|
|---|
| 10 |
Redistribution and use in source and binary forms, with or without modification, are permitted |
|---|
| 11 |
provided that the following conditions are met: |
|---|
| 12 |
|
|---|
| 13 |
* Redistributions of source code must retain the above copyright notice, the phrase |
|---|
| 14 |
"Intellectual Property Me Arse!", this list of conditions, and the following disclaimer. |
|---|
| 15 |
* Redistributions in binary form must reproduce the above copyright notice, the phrase |
|---|
| 16 |
"Intellectual Property Me Arse!", this list of conditions and the following disclaimer |
|---|
| 17 |
in the documentation and/or other materials provided with the distribution. |
|---|
| 18 |
* The name Arcane Jill may not be used to endorse or promote products derived from this |
|---|
| 19 |
software without specific prior written permission. |
|---|
| 20 |
|
|---|
| 21 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS |
|---|
| 22 |
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY |
|---|
| 23 |
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER, |
|---|
| 24 |
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|---|
| 25 |
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
|---|
| 26 |
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED, AND ON ANY |
|---|
| 27 |
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|---|
| 28 |
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY |
|---|
| 29 |
OF SUCH DAMAGE. |
|---|
| 30 |
|
|---|
| 31 |
*/ |
|---|
| 32 |
|
|---|
| 33 |
/* ------------------------------------------------------------------------------------ |
|---|
| 34 |
Returns the lowest common multiple of a and b |
|---|
| 35 |
*/ |
|---|
| 36 |
Int lcm(Int a, Int b) |
|---|
| 37 |
{ |
|---|
| 38 |
return (a * b) / gcd(a,b); |
|---|
| 39 |
} |
|---|
| 40 |
|
|---|
| 41 |
/* ------------------------------------------------------------------------------------ |
|---|
| 42 |
Returns the greatest common divisor of a and b |
|---|
| 43 |
*/ |
|---|
| 44 |
Int gcd(Int a, Int b) |
|---|
| 45 |
in |
|---|
| 46 |
{ |
|---|
| 47 |
assert(a.positive); |
|---|
| 48 |
assert(b.positive); |
|---|
| 49 |
} |
|---|
| 50 |
out(d) |
|---|
| 51 |
{ |
|---|
| 52 |
Int x; |
|---|
| 53 |
Int y; |
|---|
| 54 |
assert(d == gcd(a, b, x, y)); |
|---|
| 55 |
} |
|---|
| 56 |
body |
|---|
| 57 |
{ |
|---|
| 58 |
uint i = ge2(a); |
|---|
| 59 |
uint j = ge2(b); |
|---|
| 60 |
if (i > j) i = j; |
|---|
| 61 |
Int u = a >> i; |
|---|
| 62 |
Int v = b >> i; |
|---|
| 63 |
while (!u.equalsZero()) |
|---|
| 64 |
{ |
|---|
| 65 |
u = u >> ge2(u); |
|---|
| 66 |
v = v >> ge2(v); |
|---|
| 67 |
if (u >= v) u = u - v; |
|---|
| 68 |
else v = v - u; |
|---|
| 69 |
} |
|---|
| 70 |
return v << i; |
|---|
| 71 |
} |
|---|
| 72 |
|
|---|
| 73 |
/* ------------------------------------------------------------------------------------ |
|---|
| 74 |
Returns the greatest common divisor, d, of a and b |
|---|
| 75 |
and also integers x and y such that a*x + b*y == d |
|---|
| 76 |
|
|---|
| 77 |
Note. As currently implemented, this is MUCH slower than gcd() |
|---|
| 78 |
because of the expensive call to divMod. |
|---|
| 79 |
*/ |
|---|
| 80 |
|
|---|
| 81 |
Int gcd(Int a, Int b, out Int x, out Int y) |
|---|
| 82 |
in |
|---|
| 83 |
{ |
|---|
| 84 |
assert(a > 0); |
|---|
| 85 |
assert(b > 0); |
|---|
| 86 |
} |
|---|
| 87 |
out(d) |
|---|
| 88 |
{ |
|---|
| 89 |
assert(a*x + b*y == d); |
|---|
| 90 |
} |
|---|
| 91 |
body |
|---|
| 92 |
{ |
|---|
| 93 |
if (a > b) return gcd(b, a, y, x); |
|---|
| 94 |
Int u = a; |
|---|
| 95 |
Int v = b; |
|---|
| 96 |
Int x1 = Int.ONE; |
|---|
| 97 |
Int y1 = Int.ZERO; |
|---|
| 98 |
Int x2 = Int.ZERO; |
|---|
| 99 |
Int y2 = Int.ONE; |
|---|
| 100 |
while (!u.equalsZero) |
|---|
| 101 |
{ |
|---|
| 102 |
Int r; |
|---|
| 103 |
Int q = divMod(v, u, r); |
|---|
| 104 |
x = x2 - q * x1; |
|---|
| 105 |
y = y2 - q * y1; |
|---|
| 106 |
v = u; |
|---|
| 107 |
u = r; |
|---|
| 108 |
x2 = x1; |
|---|
| 109 |
x1 = x; |
|---|
| 110 |
y2 = y1; |
|---|
| 111 |
y1 = y; |
|---|
| 112 |
} |
|---|
| 113 |
x = x2; |
|---|
| 114 |
y = y2; |
|---|
| 115 |
return v; |
|---|
| 116 |
} |
|---|