#!/usr/bin/ruby

# Translation of: http://rosettacode.org/wiki/Benford's_law#Perl

var (actuals, expected) = ([], [])
var fibonacci = 1000.of {|i| fib(i).digit(-1) }

for i in (1..9) {
    var num = fibonacci.count { |j| j == i }
    actuals.append(num / 1000)
    expected.append(1 + (1/i) -> log10)
}

"%17s%17s\n".printf("Observed","Expected")

for i in (1..9) {
    "%d : %11s %%%15s %%\n".printf(
            i, "%.2f".sprintf(100 *  actuals[i-1]),
               "%.2f".sprintf(100 * expected[i-1]),
    )
}
