Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^4: Finding Sum of Consecutive Numerical Difference in Set of Numbers

by ambrus (Abbot)
on Oct 29, 2005 at 10:53 UTC ( [id://503846]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Finding Sum of Consecutive Numerical Difference in Set of Numbers
in thread Finding Sum of Consecutive Numerical Difference in Set of Numbers

I have a dusty ruby script somewhere to solve problems like this... yes, here's it.

def mask \ s, m; (0 ... s.size).find_all {|k| 0 != m[k] }.map {|k| s[k] } end; def poss2 \ v1, v2, s1, s2; 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; NUMS = [2, 4, 5, 7]; TARGET = -10; main; __END__

Update: output is

-10 = (4 - (2 + (5 + 7))) -10 = (4 - (2 + (7 + 5))) -10 = (4 - (5 + (2 + 7))) -10 = (4 - (5 + (7 + 2))) -10 = (4 - ((2 + 5) + 7)) -10 = (4 - ((5 + 2) + 7)) -10 = (4 - (7 + (2 + 5))) -10 = (4 - (7 + (5 + 2))) -10 = (4 - ((2 + 7) + 5)) -10 = (4 - ((7 + 2) + 5)) -10 = (4 - ((5 + 7) + 2)) -10 = (4 - ((7 + 5) + 2)) -10 = ((4 - 2) - (5 + 7)) -10 = ((4 - 2) - (7 + 5)) -10.0 = ((4 / 2) - (5 + 7)) -10.0 = ((4 / 2) - (7 + 5)) -10 = (5 - ((2 * 4) + 7)) -10 = (5 - ((4 * 2) + 7)) -10 = (5 - (7 + (2 * 4))) -10 = (5 - (7 + (4 * 2))) -10.0 = (5 / ((7 / 2) - 4)) -10 = ((4 - 5) - (2 + 7)) -10 = ((4 - 5) - (7 + 2)) -10 = ((4 - (2 + 5)) - 7) -10 = ((4 - (5 + 2)) - 7) -10 = (((4 - 2) - 5) - 7) -10.0 = (((4 / 2) - 5) - 7) -10 = ((5 - (2 * 4)) - 7) -10 = ((5 - (4 * 2)) - 7) -10 = (((4 - 5) - 2) - 7) -10 = ((4 - 7) - (2 + 5)) -10 = ((4 - 7) - (5 + 2)) -10 = ((4 - (2 + 7)) - 5) -10 = ((4 - (7 + 2)) - 5) -10 = (((4 - 2) - 7) - 5) -10.0 = (((4 / 2) - 7) - 5) -10 = (((4 - 7) - 2) - 5) -10 = ((5 - 7) - (2 * 4)) -10 = ((5 - 7) - (4 * 2)) -10 = ((4 - (5 + 7)) - 2) -10 = ((4 * (5 - 7)) - 2) -10 = ((4 - (7 + 5)) - 2) -10 = (((4 - 5) - 7) - 2) -10 = (((4 - 7) - 5) - 2) -10 = (((5 - 7) * 4) - 2)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://503846]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-24 19:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found