#! /usr/bin/perl -w use strict; print largest(@ARGV)."\n"; sub largest { # Everything works mod $least my $least = min(@_); # We start off not knowing how to do anything. my @first = map {undef} 1..$least; # For technical reasons this needs to be 0 now, # and $least later. $first[0] = 0; for my $num (@_) { my @in_process = grep {defined($first[$_])} 0..($least - 1); while (@in_process) { my $to_process = shift @in_process; my $value = $first[$to_process] + $num; my $mod = $value % $least; if (not defined($first[$mod]) or $value < $first[$mod]) { $first[$mod] = $value; print " $value = $mod (mod $least)\n"; push @in_process, $mod; } } } $first[0] = $least; if (grep {not defined($_)} @first) { # We didn't reach one... return undef; } else { my $worst = max(@first); return $worst - $least; } } sub min { my $min = shift; for (@_) { $min = $_ if $_ < $min; } return $min; } sub max { my $max = shift; for (@_) { $max = $_ if $_ > $max; } return $max; }