]> code.delx.au - gnu-emacs-elpa/blob - packages/realgud/test/gcd.pl
df476a891d40b5b48dc9fb1494327602277ec126
[gnu-emacs-elpa] / packages / realgud / test / gcd.pl
1 #!/usr/bin/env perl
2 use strict;
3 use warnings;
4
5 # GCD. We assume positive numbers
6 sub gcd($$);
7 sub gcd($$)
8 {
9 my ($a, $b) = @_;
10 # Make: a <= b
11 ($a, $b) = ($b, $a) if ($a > $b);
12
13 return undef if $a <= 0;
14 return $a if ($a == 1) or ($b-$a == 0);
15 return gcd($b-$a, $a);
16 }
17
18 die sprintf "Need two integer arguments, got %d", scalar(@ARGV) unless
19 @ARGV == 2;
20 my ($a, $b) = @ARGV[0,1];
21 printf "The GCD of %d and %d is %d\n", $a, $b, gcd($a, $b);