Skip to content

Instantly share code, notes, and snippets.

@masak

masak/program.p6 Secret

Forked from smls/gist:83829c30fdc22acaffbb
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masak/70b1dbf77c88c43979cb to your computer and use it in GitHub Desktop.
Save masak/70b1dbf77c88c43979cb to your computer and use it in GitHub Desktop.
a slight modification of smls++'s program; finds all solutions
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