ambrus has asked for the wisdom of the Perl Monks concerning the following question:
You probably know puzzles like this: use arithmetic operators and parenthesis to generate the number 24 using exactly the following four digits in any order: (1, 2, 4, 6) 1, 3, 4, 6.
I've written a ruby program to solve puzzles like this. The program works fine and gives the solution. However, when I translated this program to perl, I couldn't get it work. I've now even reformatted the two programs to be the same line by line (lay then side by side if that helps), but I still can't find out the problem.
I'd be grateful if you could look through the program (witha fresh mind) in any way and find the error. Thank you.
When I run the perl program, I get two deep recursion warnings, and the program eats lots of memory, and doesn't give a result in some time (the ruby one completes within a second).
Here's the ruby program:
#!ruby -w # alapm.rb -- megold egy számrejtvényt # Állítsuk elő a 24-et alapműveletekkel az 1, 3, 4, 6 számjegyek ponto +san # egyszeri felhasználásával. A sorrend tetsz. def mask \ s, m; (0 ... s.size).find_all {|k| 0 != m[k] }.map {|k| s[k] } end; def poss2 \ v1, v2, s1, s2; program to solve puzzles like this. The program w +orks fine yield v1 + v2, "(" + s1 + " + " + s2 + ")"; yield v1 - v2, "(" + s1 + " - " + s2 + ")"; yield v1 * v2, "(" + s1 + " * " + s2 + ")"; 1e-8 < v2.abs and yield Float(v1) / v2, "(" + s1 + " / " + s2 + ")"; s1 =~ /^\d+$/ && s2 =~ /^\d$/ and yield v1 + 10*v2, s1 + s2; end; def poss \ v, s; if 1 == v.size; yield v.first, s.first; else (1 ... (1<<v.size) - 1).each{|m| vv2 = []; ss2 = []; poss(mask(v, ~m), mask(s, ~m)) { |v2, s2| vv2.push v2; ss2.push s2; }; poss(mask(v, m), mask(s, m)) { |v1, s1| (0 ... vv2.size).each {|k| poss2(v1, vv2[k], s1, ss2[k]) { |v3, s3| yield v3, s3; }; }; }; }; end; end; # def main; poss(NUMS, NUMS.map{|x| x.to_s}) { |r, s| (r - TARGET).abs < 1e-6 and print r, " = ", s, "\n"; }; end; NUMS = [1, 3, 4, 6]; TARGET = 24; main; __END__
And the perl one:
#!perl # alapm.pl -- megold egy számrejtvényt # Rubyból fordítottam perlbe kábé szó szerint, tehát rosszabbnak kell # lennie, mintha eleve perlben írnám. Látszik rajta, callbackeket # használok úgy, ahogy perl-ben nem tenném. # Állítsuk elő a 24-et alapműveletekkel az 1, 3, 4, 6 számjegyek ponto +san # egyszeri felhasználásával. A sorrend tetsz. use warnings; use strict; use Carp "cluck"; sub mask { my($s, $m) = @_; [map { $$s[$_] } grep { 0 != ($m & (1<<$_)) } 0 .. @$s - 1]; } sub poss2 { my($v1, $v2, $s1, $s2, $f) = @_; &$f($v1 + $v2, "(" . $s1 . " + " . $s2 . ")"); &$f($v1 - $v2, "(" . $s1 . " - " . $s2 . ")"); &$f($v1 * $v2, "(" . $s1 . " * " . $s2 . ")"); 1e-8 < abs($v2) and &$f($v1 / $v2, "(" . $s1 . " / " . $s2 . ")"); $s1 =~ /^\d+$/ && $s2 =~ /^\d$/ and &$f($v1 + 10 * $v2, $s1 + $s2); } sub poss { my($v, $s, $f) = @_; if (1 == @$v) { &$f($$v[0], $$s[0]); } else { for my $m (1 .. (1<<@$v) - 2) { my(@vv2, @ss2); poss(mask($v, ~$m), mask($s, ~$m), sub { my($v2, $s2) = @_; push @vv2, $v2; push @ss2, $s2; }); poss(mask($v, $m), mask($s, $m), sub { my($v1, $s1) = @_; for my $k (0 .. @vv2 - 1) { poss2($v1, $vv2[$k], $s1, $ss2[$k], sub { my($v3, $s3) = @_; &$f($v3, $s3); }); } }); } } } my(@NUMS, $TARGET); sub main { poss([@NUMS], [@NUMS], sub { my($r, $s) = @_; abs($r - $TARGET) < 1e-6 and print $r, " = ", $s, "\n"; }); } @NUMS = (1, 3, 4, 6); $TARGET = 24; main(); __END__
Update: as talexb notes, the description in the first paragraph has different digits then the code. I've corrected that.
The desired output of the code (and the real output of the ruby code) follows (I should have included this):
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Number from given digits puzzle
by Anno (Deacon) on Mar 30, 2007 at 23:47 UTC | |
by Anonymous Monk on Mar 05, 2009 at 03:54 UTC | |
by Anno (Deacon) on Mar 09, 2009 at 16:20 UTC | |
|
Re: Number from given digits puzzle
by talexb (Chancellor) on Mar 30, 2007 at 20:45 UTC | |
|
Re: Number from given digits puzzle
by pKai (Priest) on Mar 30, 2007 at 21:06 UTC | |
by ambrus (Abbot) on Mar 31, 2007 at 11:36 UTC | |
by Krambambuli (Curate) on Mar 31, 2007 at 12:36 UTC | |
by ambrus (Abbot) on Mar 31, 2007 at 12:54 UTC | |
|
Re: Number from given digits puzzle
by ambrus (Abbot) on Apr 01, 2007 at 19:09 UTC |