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):


In reply to Number from given digits puzzle by ambrus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.