Metallic ratios

func seqRatio(f, places = 32) {
    1..Inf -> reduce {|t,n|
        var r = (f(n+1)/f(n)).round(-places)
        return(n, r.as_dec(places + r.abs.int.len)) if (r == t)
        r
    }
}

for k,v in (%w(Platinum Golden Silver Bronze Copper Nickel Aluminum Iron Tin Lead).kv) {
    next if (k == 0)   # undefined ratio
    say "Lucas sequence U_n(#{k},-1) for #{v} ratio"
    var f = {|n| lucasu(k, -1, n) }
    say ("First 15 elements: ", 15.of(f).join(', '))
    var (n, r) = seqRatio(f)
    say "Approximated value: #{r} reached after #{n} iterations"
    say ''
}

with (seqRatio({|n| fib(n) }, 256)) {|n,v|
    say "Golden ratio to 256 decimal places:"
    say "Approximated value: #{v}"
    say "Reached after #{n} iterations"
}

Output:

Last updated

Was this helpful?