Jump to content

Metallic ratios: Difference between revisions

julia example
(julia example)
Line 331:
Value to 256 dp after 615 iterations: 1.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374847540880753868917521266338622235369317931800607667263544333890865959395829056383226613199282902678806752087668925017116962070322210432162695486262963136144
</pre>
 
=={{header|Julia}}==
<lang julia>using Formatting
import Base.iterate, Base.IteratorSize, Base.IteratorEltype, Base.Iterators.take
 
const metallicnames = ["Platinum", "Golden", "Silver", "Bronze", "Copper", "Nickel",
"Aluminium", "Iron", "Tin", "Lead"]
 
struct Lucas b::Int end
Base.IteratorSize(s::Lucas) = Base.IsInfinite()
Base.IteratorEltype(s::Lucas) = BigInt
Base.iterate(s::Lucas, (x1, x2) = (big"1", big"1")) = (t = x2 * s.b + x1; (x1, (x2, t)))
 
printlucas(b, len=15) = (for i in take(Lucas(b), len) print(i, ", ") end; println("..."))
 
function metallic(b, dplaces=32)
setprecision(dplaces * 5)
estimate, err, x1, iter = big"0.0", BigFloat(10)^(-dplaces), big"1", Lucas(b)
next = iterate(iter); (_, state) = next; next = iterate(iter, state)
for iterations in 1:dplaces*100
(x2, state) = next
newestimate = BigFloat(x2) / BigFloat(x1)
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
 
for (b, name) in enumerate(metallicnames)
println("The first 15 elements of the Lucas sequence named ",
metallicnames[b], " and b of $(b - 1) are:")
printlucas(b - 1)
metallic(b - 1)
end
println("Golden ratio to 256 decimal places:")
metallic(1, 256)
</lang>{{out}}
<pre>
The first 15 elements of the Lucas sequence named Platinum and b of 0 are:
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, ...
After 2 iterations, the value of 1.00000000000000000000000000000000 is stable to 32 decimal places.
 
The first 15 elements of the Lucas sequence named Golden and b of 1 are:
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, ...
After 79 iterations, the value of 1.61803398874989484820458683436564 is stable to 32 decimal places.
 
The first 15 elements of the Lucas sequence named Silver and b of 2 are:
1, 1, 3, 7, 17, 41, 99, 239, 577, 1393, 3363, 8119, 19601, 47321, 114243, ...
After 45 iterations, the value of 2.41421356237309504880168872420970 is stable to 32 decimal places.
 
The first 15 elements of the Lucas sequence named Bronze and b of 3 are:
1, 1, 4, 13, 43, 142, 469, 1549, 5116, 16897, 55807, 184318, 608761, 2010601, 6640564, ...
After 34 iterations, the value of 3.30277563773199464655961063373525 is stable to 32 decimal places.
 
The first 15 elements of the Lucas sequence named Copper and b of 4 are:
1, 1, 5, 21, 89, 377, 1597, 6765, 28657, 121393, 514229, 2178309, 9227465, 39088169, 165580141, ...
After 29 iterations, the value of 4.23606797749978969640917366873128 is stable to 32 decimal places.
 
The first 15 elements of the Lucas sequence named Nickel and b of 5 are:
1, 1, 6, 31, 161, 836, 4341, 22541, 117046, 607771, 3155901, 16387276, 85092281, 441848681, 2294335686, ...
After 26 iterations, the value of 5.19258240356725201562535524577016 is stable to 32 decimal places.
 
The first 15 elements of the Lucas sequence named Aluminium and b of 6 are:
1, 1, 7, 43, 265, 1633, 10063, 62011, 382129, 2354785, 14510839, 89419819, 551029753, 3395598337, 20924619775, ...
After 24 iterations, the value of 6.16227766016837933199889354443272 is stable to 32 decimal places.
 
The first 15 elements of the Lucas sequence named Iron and b of 7 are:
1, 1, 8, 57, 407, 2906, 20749, 148149, 1057792, 7552693, 53926643, 385039194, 2749201001, 19629446201, 140155324408, ...
After 22 iterations, the value of 7.14005494464025913554865124576352 is stable to 32 decimal places.
 
The first 15 elements of the Lucas sequence named Tin and b of 8 are:
1, 1, 9, 73, 593, 4817, 39129, 317849, 2581921, 20973217, 170367657, 1383914473, 11241683441, 91317382001, 741780739449, ...
After 21 iterations, the value of 8.12310562561766054982140985597408 is stable to 32 decimal places.
 
The first 15 elements of the Lucas sequence named Lead and b of 9 are:
1, 1, 10, 91, 829, 7552, 68797, 626725, 5709322, 52010623, 473804929, 4316254984, 39320099785, 358197153049, 3263094477226, ...
After 20 iterations, the value of 9.10977222864644365500113714088140 is stable to 32 decimal places.
 
Golden ratio to 256 decimal places:
After 615 iterations, the value of 1.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374847540880753868917521266338622235369317931800607667263544333890865959395829056383226613199282902678806752087668925017116962070322210432162695486262963136144 is stable to 256 decimal places.
</pre>
 
 
=={{header|Perl}}==
4,106

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.