Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add gcd routine which calls into BigDigits for efficiency.

  • Loading branch information...
commit 5affaa7c003d18b4bfdcdd0c18012269cdb65258 1 parent 270d80c
@colomon authored
Showing with 13 additions and 0 deletions.
  1. +7 −0 lib/Math/BigInt.pm
  2. +6 −0 t/02-arith.t
View
7 lib/Math/BigInt.pm
@@ -10,6 +10,7 @@ sub bdSubtract(OpaquePointer $w, OpaquePointer $u, OpaquePointer $v) returns Int
sub bdMultiply(OpaquePointer $w, OpaquePointer $u, OpaquePointer $v) returns Int is native("libbd") { ... }
sub bdDivide(OpaquePointer $q, OpaquePointer $r, OpaquePointer $u, OpaquePointer $v) returns Int is native("libbd") { ... }
sub bdModulo(OpaquePointer $w, OpaquePointer $u, OpaquePointer $v) returns Int is native("libbd") { ... }
+sub bdGcd(OpaquePointer $g, OpaquePointer $x, OpaquePointer $y) returns Int is native("libbd") { ... }
sub bdConvToDecimal(OpaquePointer $bd, OpaquePointer $s, Int $smax) returns Int is native("libbd") { ... }
sub bdIsEqual(OpaquePointer $a, OpaquePointer $b) returns Int is native("libbd") { ... }
sub bdCompare(OpaquePointer $a, OpaquePointer $b) returns Int is native("libbd") { ... }
@@ -251,4 +252,10 @@ class Math::BigInt does Real {
bdMultiply($result.bd, Math::BigInt.new($a).bd, Math::BigInt.new($b).bd);
$result;
}
+
+ our multi sub gcd(Math::BigInt $a, Math::BigInt $b) is export(:DEFAULT) {
+ my $result = Math::BigInt.new("1");
+ bdGcd($result.bd, $a.bd, $b.bd);
+ $result;
+ }
}
View
6 t/02-arith.t
@@ -132,6 +132,12 @@ plan *;
is (2L)**80, "1208925819614629174706176", "2**80 is 1208925819614629174706176";
}
+# gcd!
+{
+ is gcd(25L, 24L), 1, "gcd of 25 and 24 is 1";
+ is gcd(25L, 5L), 5, "gcd of 25 and 5 is 5";
+ is gcd(25L, 30L), 5, "gcd of 25 and 30 is 5";
+}
Please sign in to comment.
Something went wrong with that request. Please try again.