Cool little script. I've used Algorithm::Combinatorics as well recently and found it to be a very helpful module.
Here's your script streamlined just a little bit. As the documentation points out, variations(\@data, scalar @data) is equivalent to permutations(\@data).
use Algorithm::Combinatorics qw(permutations); use strict; my @numbers = qw(9 -4 8 6 10); my @operators = qw(+ - * /); my $total = 7; #required result my $num_iter = permutations(\@numbers); while (my $nums = $num_iter->next){ my $op_iter = permutations(\@operators); while (my $ops = $op_iter->next) { my $str = $nums->[0]; $str = "($str $ops->[$_] " . $nums->[$_+1] . ")" for (0..3); if ($total == eval $str) { print "$str = $total\n"; } } }
In reply to Re: Automating solving missing Arithmetic operators
by wind
in thread Automating solving missing Arithmetic operators
by Swalif
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |