-
-
Save masak/70b1dbf77c88c43979cb to your computer and use it in GitHub Desktop.
a slight modification of smls++'s program; finds all solutions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
my $sum = 283; | |
my @terms = 23, 33, 44, 62; | |
my $solution-found = False; | |
my @factors = 0 xx +@terms; # First set of factors is all zeroes | |
loop (my $i = 0; $i < +@factors;) { | |
@factors[$i]++; | |
given [+] @factors Z* @terms { | |
when * > $sum { @factors[$i] = 0; $i++ } | |
when * < $sum { $i = 0 } | |
default { | |
say "Found solution:\n $sum = ", | |
(@terms Z=> @factors).Bag.kv.map(* ~ "×" ~ *).join(" + "); | |
$solution-found = True; | |
} | |
} | |
} | |
say "No solution found." | |
unless $solution-found; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment