in reply to Automating solving missing Arithmetic operators

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"; } } }

Replies are listed 'Best First'.
Re^2: Automating solving missing Arithmetic operators
by Swalif (Scribe) on Apr 07, 2011 at 14:21 UTC
    Great enhancements! now it's more elegant with calculation's part. Thanks