#!/usr/bin/ruby

func simple_moving_average(period) {

    var list = [];
    var sum = 0;

    func (number) {
        list.append(number);
        sum += number;
        list.len > period && (
            sum -= list.shift;
        );
        return (sum / list.length);
    }
}

var ma3 = simple_moving_average(3);
var ma5 = simple_moving_average(5);

for num in (1..^6, 6^..1) {
  printf("Next number = %d, SMA_3 = %.3f, SMA_5 = %.1f\n",
    num, ma3.call(num), ma5.call(num));
}
