use strict; use Algorithm::Combinatorics qw(variations); use feature ':5.10'; my @operators = qw(+ - * /); my @numbers = qw(9 -4 8 6 10); my $total = 7; #required result my $iter = variations(\@operators, 4); #get combinations for operators + without rep. my $iter2 = variations(\@numbers, 5); #get combinations for numbers w +ithout rep. while (my $p = $iter->next){ while (my $p2 = $iter2->next){ #escaping precedence my $eval = eval ("$p2->[0] $p->[0] $p2->[1]"); $eval = eval ("$eval $p->[1] $p2->[2]"); $eval = eval ("$eval $p->[2] $p2->[3]"); $eval = eval ("$eval $p->[3] $p2->[4]"); if ($eval == $total){ say "$p2->[0] ($p->[0]) $p2->[1] ($p->[1]) $p2->[2] ($p->[2]) $p2- +>[3] ($p->[3]) $p2->[4] = $total"; } } $iter2 = variations(\@numbers, 5); #Resets the iterator }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Automating solving missing Arithmetic operators
by wind (Priest) on Apr 07, 2011 at 06:33 UTC | |
by Swalif (Scribe) on Apr 07, 2011 at 14:21 UTC | |
|
Re: Automating solving missing Arithmetic operators
by ikegami (Patriarch) on Apr 07, 2011 at 05:34 UTC | |
by Swalif (Scribe) on Apr 07, 2011 at 14:16 UTC |