WeBWorK Problems

Error traced to gcd( ) using more than 2 arguments?

Re: Error traced to gcd( ) using more than 2 arguments?

by Alex Jordan -
Number of replies: 0
If I recall, the gcd() that you have access to assumes two inputs, and it only returns the gcd of the first two arguments.

Maybe there is already a simpler way to handle this, but I think in the past I just wrote a loop. If @numbers = (14,14,7,14), then make something like GCD() be a recursive subroutine. Haven't tested, but maybe like:

sub GCD {
my $a = shift;
my $b = shift;
my @leftovers = @_;
my $g = gcd($a,$b);
return (@leftovers == 0) ? $g : GCD($g,@leftovers);
}

While I may not be providing valid perl code, the last line is meant to read "if @leftovers is empty, then return $g; otherwise, return GCD($g,@leftovers)."