Metallic ratios: Difference between revisions

Content added Content deleted
Line 345: Line 345:


printlucas(b, len=15) = (for i in take(Lucas(b), len) print(i, ", ") end; println("..."))
printlucas(b, len=15) = (for i in take(Lucas(b), len) print(i, ", ") end; println("..."))

function lucasratios(b, len)
iter = BigFloat.(collect(take(Lucas(b), len + 1)))
return map(i -> iter[i + 1] / iter[i], 1:length(iter)-1)
end


function metallic(b, dplaces=32)
function metallic(b, dplaces=32)
setprecision(dplaces * 5)
setprecision(dplaces * 5)
estimate, err, x1, iter = big"0.0", BigFloat(10)^(-dplaces), big"1", Lucas(b)
ratios, err = lucasratios(b, dplaces * 50), BigFloat(10)^(-dplaces)
errors = map(i -> abs(ratios[i + 1] - ratios[i]), 1:length(ratios)-1)
next = iterate(iter); (_, state) = next; next = iterate(iter, state) # iterate past first 1 in sequence
iternum = findfirst(x -> x < err, errors)
for iterations in 1:dplaces*100
println("After $(iternum + 1) iterations, the value of ",
(x2, state) = next
format(ratios[iternum + 1], precision=dplaces),
newestimate = BigFloat(x2) / BigFloat(x1)
" is stable to $dplaces decimal places.\n")
if abs(newestimate - estimate) < err
println("After $iterations iterations, the value of ",
format(newestimate, precision=dplaces),
" is stable to $dplaces decimal places.\n")
break
end
x1, estimate, next = x2, newestimate, iterate(iter, state)
end
end
end