Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Challenge: Number of unique ways to reach target sum

by ambrus (Abbot)
on Feb 15, 2006 at 14:18 UTC ( [id://530383]=note: print w/replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
    ruby -we '@a = (0 .. 10).map { (0 .. 667).map { (0 .. 101).map { (); }
    +; }; }; def res c = 10, t = 667, m = 101; c * m - t < t and t = c * m
    + - t; if t < 0; 0; else @a[c][t][m] ||= if 0 == c && 0 == t; 1; elsif
    + 0 == c; 0; elsif 0 == t; 0; else (1 ... m).inject(0) {|r, x| r + res
    +(c - 1, t - x, x); } end; end; end; (1 .. 10).each {|c| res c; warn "
    +[c=#{c}]"; }; p res;'
    
  2. or download this
    ruby -we '@a = (0 .. 10).map { (0 .. 667).map { (0 .. 101).map { (); }
    +; }; }; def res c = 10, t = 667, m = 101; c * m - t < t and t = c * m
    + - t; if t < 0; 0; else @a[c][t][m] ||= if 0 == c && 0 == t; 1; elsif
    + 0 == c; 0; elsif 0 == t; 0; else (1 ... m).inject(0) {|r, x| r + res
    +(c - 1, t - x, x); } end; end; end; p res;'
    
  3. or download this
    ruby -we '@tri = (0 .. 10).map {|c| c * (c + 1) / 2; }; @a = [[Array.n
    +ew(101, 1)] + [Array.new(101, 0)] * 400] + (1 .. 10).map { (0 .. 400)
    +.map { Array.new(101); }; }; def res c = 10, t = 667, m = 101; (t2 = 
    +c * m - t) < t and t = t2; if t < @tri[c]; 0; else @a[c][t][m] ||= (1
    + ... m).inject(0) {|r, x| r + res(c - 1, t - x, x); } end; end; puts 
    +r = res; r == 14479062752 or fail;'
    
  4. or download this
    ruby -we '@tri = (0 .. 10).map {|c| c * (c + 1) / 2; }; @a = [[Array.n
    +ew(101, 1)] + [Array.new(101, 0)] * 400] + (1 .. 10).map { (0 .. 400)
    +.map { Array.new(101); }; }; def res c = 10, t = 667, m = 101; (t2 = 
    +c * m - t) < t and t = t2; if t < @tri[c]; 0; else @a[c][t][m] ||= (g
    + = @tri[c - 1]; ((t + g)/c .. [m - 1, t - g].min)).inject(0) {|r, x| 
    +r + res(c - 1, t - x, x); } end; end; puts r = res; r == 14479062752 
    +or fail;'
    
  5. or download this
    ruby -we '@tri = (0 .. 10).map {|c| c * (c + 1) / 2; }; @a = (0 .. 10)
    +.map { (0 .. 400).map { Array.new(101); }; }; def res c = 10, t = 667
    +, m = 101; (t2 = c * m - t) < t and t = t2; if t <= 0; if t == 0 && c
    + == 0; 1 else 0 end; else @a[c][t][m] ||= (g = @tri[c - 1]; ((t + g)/
    +c .. [m - 1, t - g].min)).inject(0) {|r, x| r + res(c - 1, t - x, x);
    + } end; end; puts r = res; r == 14479062752 or fail;'
    

Log In?
Username:
Password:

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

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

    No recent polls found