#!/usr/bin/ruby

# Translation of: https://rosettacode.org/wiki/9_billion_names_of_God_the_integer#Python

var cache = [[1]];

func cumu (n) {
    for l in range(cache.len, n) {
        var r = [0];
        for i in (1..l) {
            r.append(r[-1] + cache[l-i][[i, l-i].min])
        }
        cache.append(r);
    }

    return(cache[n]);
}

func row (n) {
    var r = cumu(n);
    return (n-1 -> range.map{|i| r[i+1] - r[i] });
}

"rows:".say;
for i in (1..10) {
    "%2d: %s\n".printf(i, row(i).dump);
}

"\nsums:".say;

[2, 5, 23, 32].each { |i|
    "%2d : %4d\n".printf(i, (cumu(i))[-1]);
}
