Fixed the bug of not outputing the correct least common multiple of given numbers

master
shjnyr 2015-10-18 01:13:30 +00:00
parent f0f7e84118
commit 19da7511b4
2 changed files with 5 additions and 9 deletions

View File

@ -29,14 +29,10 @@ handle remainder => sub {
my $formatted_numbers = join(', ', @numbers);
$formatted_numbers =~ s/, ([^,]*)$/ and $1/;
my $gcd = $numbers[0];
my $lcm = 1;
foreach (@numbers) {
$gcd = gcf($gcd, $_);
}
my $lcm = shift @numbers;
foreach (@numbers) {
$lcm *= $_ / $gcd;
$lcm = $lcm * $_ / gcf($lcm, $_);
}
return "Least common multiple of $formatted_numbers is $lcm.",

View File

@ -63,7 +63,7 @@ ddg_goodie_test(
structured_answer => {
input => ['12, 18 and 24'],
operation => 'Least common multiple',
result => 144
result => 72
}
),
'lcm 6, 9, ,,,, 12 15' => test_zci(
@ -71,7 +71,7 @@ ddg_goodie_test(
structured_answer => {
input => ['6, 9, 12 and 15'],
operation => 'Least common multiple',
result => 360
result => 180
}
),
);