#!/usr/bin/ruby

#
## http://rosettacode.org/wiki/Left_factorials#Sidef
#

func left_fact(n) {
    static cached    = 0;
    static factorial = 1;
    static leftfact  = 0;

    if (n < cached) {
        cached    = 0;
        factorial = 1;
        leftfact  = 0;
    }

    while (n > cached) {
        leftfact  += factorial;
        factorial *= ++cached;
    }

    leftfact;
}

[range(0, 10), range(20, 50).by(10)].each { |r|
    r.each { |i|
        printf("!%d  = %s\n", i, left_fact(i));
    }
}
