Metallic ratios: Difference between revisions

m
→‎{{header|RPL}}: improved comment
m (→‎{{header|RPL}}: improved comment)
 
(35 intermediate revisions by 17 users not shown)
Line 1:
{{draft task}}
 
Many people have heard of the '''Golden ratio''', phi ('''φ'''). Phi is just one of a series
Line 13:
'''Metallic ratios''' are the real roots of the general form equation:
 
<big> x²<sup>2</sup> - bx - 1 = 0 </big>
 
where the integer '''b''' determines which specific one it is.
Line 19:
Using the quadratic equation:
 
<big> ( -b ± √(b²<sup>2</sup> - 4ac) ) / 2a = x </big>
 
Substitute in (from the top equation) '''1''' for '''a''', '''-1''' for '''c''', and recognising that -b is negated we get:
 
<big> ( b ± √(b²<sup>2</sup> + 4) ) ) / 2 = x </big>
 
We only want the real root:
 
<big> ( b + √(b²<sup>2</sup> + 4) ) ) / 2 = x </big>
 
When we set '''b''' to '''1''', we get an irrational number: the '''Golden ratio'''.
 
<big> ( 1 + √(1²<sup>2</sup> + 4) ) / 2 = (1 + √5) / 2 = ~1.618033989... </big>
 
With '''b''' set to '''2''', we get a different irrational number: the '''Silver ratio'''.
 
<big> ( 2 + √(2²<sup>2</sup> + 4) ) / 2 = (2 + √8) / 2 = ~2.414213562... </big>
 
When the ratio '''b''' is '''3''', it is commonly referred to as the '''Bronze''' ratio, '''4''' and '''5'''
Line 49:
'''Metallic ratios''' where '''b''' > '''0''' are also defined by the irrational continued fractions:
 
<big> [b;b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,b...] </big>
 
 
So, The first 10ten '''Metallic ratios''' are:
 
:::::: {| class="wikitable" style="text-align: center;"
|+ Metallic ratios
!Name!!'''b'''!!Equation!!Value!!Continued fraction!!OEIS link
Line 87:
A traditional '''Lucas sequence''' is of the form:
 
xₙ<big>x<sub>''n''</sub> = P * xₙ₋₁x<sub>''n-1''</sub> - Q * xₙ₋₂.x<sub>''n-2''</sub></big>
 
and starts with the first 2 values '''0, 1'''.
Line 93:
For our purposes in this task, to find the metallic ratios we'll use the form:
 
xₙ<big>x<sub>''n''</sub> = b * xₙ₋₁x<sub>''n-1''</sub> + xₙ₋₂x<sub>''n-2''</sub></big>
 
( '''P''' is set to '''b''' and '''Q''' is set to '''-1'''. ) To avoid "divide by zero" issues we'll start the sequence with the first two terms '''1, 1'''. The initial starting value has very little effect on the final ratio or convergence rate. ''Perhaps it would be more accurate to call it a Lucas-like sequence.''
Line 99:
At any rate, when '''b = 1''' we get:
 
<big>x<sub>''n''</sub> = x<sub>''n-1''</sub> + x<sub>''n-2''</sub></big>
xₙ = 1 * xₙ₋₁ + xₙ₋₂.
 
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144...
Line 107:
When '''b = 2''':
 
<big>x<sub>''n''</sub> = 2 * x<sub>''n-1''</sub> + x<sub>''n-2''</sub></big>
xₙ = 2 * xₙ₋₁ + xₙ₋₂.
 
1, 1, 3, 7, 17, 41, 99, 239, 577, 1393...
Line 154:
* [[wp:Lucas_sequence|Wikipedia: Lucas sequence]]
 
=={{header|C#|csharp}}==
{{libheader|System.Numerics}}Since the task description outlines how the results can be calculated using square roots for each B, this program not only calculates the results by iteration (as specified), it also checks each result with an independent result calculated by the indicated square root (for each B).
<syntaxhighlight lang="csharp">using static System.Math;
using static System.Console;
using BI = System.Numerics.BigInteger;
class Program {
static BI IntSqRoot(BI v, BI res) { // res is the initial guess
BI term = 0, d = 0, dl = 1; while (dl != d) { term = v / res; res = (res + term) >> 1;
dl = d; d = term - res; } return term; }
static string doOne(int b, int digs) { // calculates result via square root, not iterations
int s = b * b + 4; BI g = (BI)(Sqrt((double)s) * Pow(10, ++digs)),
bs = IntSqRoot(s * BI.Parse('1' + new string('0', digs << 1)), g);
bs += b * BI.Parse('1' + new string('0', digs));
bs >>= 1; bs += 4; string st = bs.ToString();
return string.Format("{0}.{1}", st[0], st.Substring(1, --digs)); }
static string divIt(BI a, BI b, int digs) { // performs division
int al = a.ToString().Length, bl = b.ToString().Length;
a *= BI.Pow(10, ++digs << 1); b *= BI.Pow(10, digs);
string s = (a / b + 5).ToString(); return s[0] + "." + s.Substring(1, --digs); }
// custom formating
static string joined(BI[] x) { int[] wids = {1, 1, 2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
string res = ""; for (int i = 0; i < x.Length; i++) res +=
string.Format("{0," + (-wids[i]).ToString() + "} ", x[i]); return res; }
static void Main(string[] args) { // calculates and checks each "metal"
WriteLine("Metal B Sq.Rt Iters /---- 32 decimal place value ----\\ Matches Sq.Rt Calc");
int k; string lt, t = ""; BI n, nm1, on; for (int b = 0; b < 10; b++) {
BI[] lst = new BI[15]; lst[0] = lst[1] = 1;
for (int i = 2; i < 15; i++) lst[i] = b * lst[i - 1] + lst[i - 2];
// since all the iterations (except Pt) are > 15, continue iterating from the end of the list of 15
n = lst[14]; nm1 = lst[13]; k = 0; for (int j = 13; k == 0; j++) {
lt = t; if (lt == (t = divIt(n, nm1, 32))) k = b == 0 ? 1 : j;
on = n; n = b * n + nm1; nm1 = on; }
WriteLine("{0,4} {1} {2,2} {3, 2} {4} {5}\n{6,19} {7}", "Pt Au Ag CuSn Cu Ni Al Fe Sn Pb"
.Split(' ')[b], b, b * b + 4, k, t, t == doOne(b, 32), "", joined(lst)); }
// now calculate and check big one
n = nm1 =1; k = 0; for (int j = 1; k == 0; j++) {
lt = t; if (lt == (t = divIt(n, nm1, 256))) k = j;
on = n; n += nm1; nm1 = on; }
WriteLine("\nAu to 256 digits:"); WriteLine(t);
WriteLine("Iteration count: {0} Matched Sq.Rt Calc: {1}", k, t == doOne(1, 256)); }
}</syntaxhighlight>
{{out}}
<pre style="white-space:pre-wrap;">Metal B Sq.Rt Iters /---- 32 decimal place value ----\ Matches Sq.Rt Calc
Pt 0 4 1 1.00000000000000000000000000000000 True
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Au 1 5 78 1.61803398874989484820458683436564 True
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610
Ag 2 8 44 2.41421356237309504880168872420970 True
1 1 3 7 17 41 99 239 577 1393 3363 8119 19601 47321 114243
CuSn 3 13 34 3.30277563773199464655961063373525 True
1 1 4 13 43 142 469 1549 5116 16897 55807 184318 608761 2010601 6640564
Cu 4 20 28 4.23606797749978969640917366873128 True
1 1 5 21 89 377 1597 6765 28657 121393 514229 2178309 9227465 39088169 165580141
Ni 5 29 25 5.19258240356725201562535524577016 True
1 1 6 31 161 836 4341 22541 117046 607771 3155901 16387276 85092281 441848681 2294335686
Al 6 40 23 6.16227766016837933199889354443272 True
1 1 7 43 265 1633 10063 62011 382129 2354785 14510839 89419819 551029753 3395598337 20924619775
Fe 7 53 22 7.14005494464025913554865124576352 True
1 1 8 57 407 2906 20749 148149 1057792 7552693 53926643 385039194 2749201001 19629446201 140155324408
Sn 8 68 20 8.12310562561766054982140985597408 True
1 1 9 73 593 4817 39129 317849 2581921 20973217 170367657 1383914473 11241683441 91317382001 741780739449
Pb 9 85 20 9.10977222864644365500113714088140 True
1 1 10 91 829 7552 68797 626725 5709322 52010623 473804929 4316254984 39320099785 358197153049 3263094477226
 
Au to 256 digits:
1.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374847540880753868917521266338622235369317931800607667263544333890865959395829056383226613199282902678806752087668925017116962070322210432162695486262963136144
Iteration count: 616 Matched Sq.Rt Calc: True</pre>Note: All the metals (except bronze) in this task are elements, so those symbols are used. Bronze is usually approx. 88% '''Cu''' and 12 % '''Sn''', and so it is labeled '''CuSn''' here.
 
=={{header|C++|C++}}==
{{trans|Go}}
{{Works with|C++11}}
{{libheader|Boost}}
<syntaxhighlight lang="cpp">#include <boost/multiprecision/cpp_dec_float.hpp>
#include <iostream>
 
const char* names[] = { "Platinum", "Golden", "Silver", "Bronze", "Copper", "Nickel", "Aluminium", "Iron", "Tin", "Lead" };
 
template<const uint N>
void lucas(ulong b) {
std::cout << "Lucas sequence for " << names[b] << " ratio, where b = " << b << ":\nFirst " << N << " elements: ";
auto x0 = 1L, x1 = 1L;
std::cout << x0 << ", " << x1;
for (auto i = 1u; i <= N - 1 - 1; i++) {
auto x2 = b * x1 + x0;
std::cout << ", " << x2;
x0 = x1;
x1 = x2;
}
std::cout << std::endl;
}
 
template<const ushort P>
void metallic(ulong b) {
using namespace boost::multiprecision;
using bfloat = number<cpp_dec_float<P+1>>;
bfloat x0(1), x1(1);
auto prev = bfloat(1).str(P+1);
for (auto i = 0u;;) {
i++;
bfloat x2(b * x1 + x0);
auto thiz = bfloat(x2 / x1).str(P+1);
if (prev == thiz) {
std::cout << "Value after " << i << " iteration" << (i == 1 ? ": " : "s: ") << thiz << std::endl << std::endl;
break;
}
prev = thiz;
x0 = x1;
x1 = x2;
}
}
 
int main() {
for (auto b = 0L; b < 10L; b++) {
lucas<15>(b);
metallic<32>(b);
}
std::cout << "Golden ratio, where b = 1:" << std::endl;
metallic<256>(1);
 
return 0;
}</syntaxhighlight>
{{Out}}
<pre>Lucas sequence for Platinum ratio, where b = 0:
First 15 elements: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
Value after 1 iteration: 1
 
Lucas sequence for Golden ratio, where b = 1:
First 15 elements: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610
Value after 78 iterations: 1.61803398874989484820458683436564
 
Lucas sequence for Silver ratio, where b = 2:
First 15 elements: 1, 1, 3, 7, 17, 41, 99, 239, 577, 1393, 3363, 8119, 19601, 47321, 114243
Value after 44 iterations: 2.4142135623730950488016887242097
 
Lucas sequence for Bronze ratio, where b = 3:
First 15 elements: 1, 1, 4, 13, 43, 142, 469, 1549, 5116, 16897, 55807, 184318, 608761, 2010601, 6640564
Value after 34 iterations: 3.30277563773199464655961063373525
 
Lucas sequence for Copper ratio, where b = 4:
First 15 elements: 1, 1, 5, 21, 89, 377, 1597, 6765, 28657, 121393, 514229, 2178309, 9227465, 39088169, 165580141
Value after 28 iterations: 4.23606797749978969640917366873128
 
Lucas sequence for Nickel ratio, where b = 5:
First 15 elements: 1, 1, 6, 31, 161, 836, 4341, 22541, 117046, 607771, 3155901, 16387276, 85092281, 441848681, 2294335686
Value after 25 iterations: 5.19258240356725201562535524577016
 
Lucas sequence for Aluminium ratio, where b = 6:
First 15 elements: 1, 1, 7, 43, 265, 1633, 10063, 62011, 382129, 2354785, 14510839, 89419819, 551029753, 3395598337, 20924619775
Value after 23 iterations: 6.16227766016837933199889354443272
 
Lucas sequence for Iron ratio, where b = 7:
First 15 elements: 1, 1, 8, 57, 407, 2906, 20749, 148149, 1057792, 7552693, 53926643, 385039194, 2749201001, 19629446201, 140155324408
Value after 22 iterations: 7.14005494464025913554865124576352
 
Lucas sequence for Tin ratio, where b = 8:
First 15 elements: 1, 1, 9, 73, 593, 4817, 39129, 317849, 2581921, 20973217, 170367657, 1383914473, 11241683441, 91317382001, 741780739449
Value after 20 iterations: 8.12310562561766054982140985597408
 
Lucas sequence for Lead ratio, where b = 9:
First 15 elements: 1, 1, 10, 91, 829, 7552, 68797, 626725, 5709322, 52010623, 473804929, 4316254984, 39320099785, 358197153049, 3263094477226
Value after 20 iterations: 9.1097722286464436550011371408814
 
Golden ratio, where b = 1:
Value after 615 iterations: 1.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374847540880753868917521266338622235369317931800607667263544333890865959395829056383226613199282902678806752087668925017116962070322210432162695486262963136144</pre>
 
=={{header|F_Sharp|F#}}==
<syntaxhighlight lang="fsharp">
// Metallic Ration. Nigel Galloway: September 16th., 2020
let rec fN i g (e,l)=match i with 0->g |_->fN (i-1) (int(l/e)::g) (e,(l%e)*10I)
let fI(P:int)=Seq.unfold(fun(n,g)->Some(g,((bigint P)*n+g,n)))(1I,1I)
let fG fI fN=let _,(n,g)=fI|>Seq.pairwise|>Seq.mapi(fun n g->(n,fN g))|>Seq.pairwise|>Seq.find(fun((_,n),(_,g))->n=g) in (n,List.rev g)
let mR n g=printf "First 15 elements when P = %d -> " n; fI n|>Seq.take 15|>Seq.iter(printf "%A "); printf "\n%d decimal places " g
let Σ,n=fG(fI n)(fN (g+1) []) in printf "required %d iterations -> %d." Σ n.Head; List.iter(printf "%d")n.Tail ;printfn ""
[0..9]|>Seq.iter(fun n->mR n 32; printfn ""); mR 1 256
</syntaxhighlight>
{{out}}
<pre>
First 15 elements when P = 0 -> 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
32 decimal places required 1 iterations -> 1.00000000000000000000000000000000
 
First 15 elements when P = 1 -> 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610
32 decimal places required 79 iterations -> 1.61803398874989484820458683436563
 
First 15 elements when P = 2 -> 1 1 3 7 17 41 99 239 577 1393 3363 8119 19601 47321 114243
32 decimal places required 45 iterations -> 2.41421356237309504880168872420969
 
First 15 elements when P = 3 -> 1 1 4 13 43 142 469 1549 5116 16897 55807 184318 608761 2010601 6640564
32 decimal places required 33 iterations -> 3.30277563773199464655961063373524
 
First 15 elements when P = 4 -> 1 1 5 21 89 377 1597 6765 28657 121393 514229 2178309 9227465 39088169 165580141
32 decimal places required 28 iterations -> 4.23606797749978969640917366873127
 
First 15 elements when P = 5 -> 1 1 6 31 161 836 4341 22541 117046 607771 3155901 16387276 85092281 441848681 2294335686
32 decimal places required 25 iterations -> 5.19258240356725201562535524577016
 
First 15 elements when P = 6 -> 1 1 7 43 265 1633 10063 62011 382129 2354785 14510839 89419819 551029753 3395598337 20924619775
32 decimal places required 23 iterations -> 6.16227766016837933199889354443271
 
First 15 elements when P = 7 -> 1 1 8 57 407 2906 20749 148149 1057792 7552693 53926643 385039194 2749201001 19629446201 140155324408
32 decimal places required 21 iterations -> 7.14005494464025913554865124576351
 
First 15 elements when P = 8 -> 1 1 9 73 593 4817 39129 317849 2581921 20973217 170367657 1383914473 11241683441 91317382001 741780739449
32 decimal places required 20 iterations -> 8.12310562561766054982140985597407
 
First 15 elements when P = 9 -> 1 1 10 91 829 7552 68797 626725 5709322 52010623 473804929 4316254984 39320099785 358197153049 3263094477226
32 decimal places required 19 iterations -> 9.10977222864644365500113714088139
 
First 15 elements when P = 1 -> 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610
256 decimal places required 614 iterations -> 1.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374847540880753868917521266338622235369317931800607667263544333890865959395829056383226613199282902678806752087668925017116962070322210432162695486262963136144
</pre>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: combinators decimals formatting generalizations io kernel
math prettyprint qw sequences ;
IN: rosetta-code.metallic-ratios
Line 185 ⟶ 400:
[ "- reached after %d iteration(s)\n\n" printf ]
} spread
] each-index</langsyntaxhighlight>
{{out}}
<pre style="height:45ex">
Line 230 ⟶ 445:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 284 ⟶ 499:
fmt.Println("Golden ratio, where b = 1:")
metallic(1, 256)
}</langsyntaxhighlight>
 
{{out}}
Line 330 ⟶ 545:
Golden ratio, where b = 1:
Value to 256 dp after 615 iterations: 1.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374847540880753868917521266338622235369317931800607667263544333890865959395829056383226613199282902678806752087668925017116962070322210432162695486262963136144
</pre>
 
=={{header|Groovy}}==
{{trans|Kotlin}}
<syntaxhighlight lang="groovy">class MetallicRatios {
private static List<String> names = new ArrayList<>()
static {
names.add("Platinum")
names.add("Golden")
names.add("Silver")
names.add("Bronze")
names.add("Copper")
names.add("Nickel")
names.add("Aluminum")
names.add("Iron")
names.add("Tin")
names.add("Lead")
}
 
private static void lucas(long b) {
printf("Lucas sequence for %s ratio, where b = %d\n", names[b], b)
print("First 15 elements: ")
long x0 = 1
long x1 = 1
printf("%d, %d", x0, x1)
for (int i = 1; i < 13; ++i) {
long x2 = b * x1 + x0
printf(", %d", x2)
x0 = x1
x1 = x2
}
println()
}
 
private static void metallic(long b, int dp) {
BigInteger x0 = BigInteger.ONE
BigInteger x1 = BigInteger.ONE
BigInteger x2
BigInteger bb = BigInteger.valueOf(b)
BigDecimal ratio = BigDecimal.ONE.setScale(dp)
int iters = 0
String prev = ratio.toString()
while (true) {
iters++
x2 = bb * x1 + x0
String thiz = (x2.toBigDecimal().setScale(dp) / x1.toBigDecimal().setScale(dp)).toString()
if (prev == thiz) {
String plural = "s"
if (iters == 1) {
plural = ""
}
printf("Value after %d iteration%s: %s\n\n", iters, plural, thiz)
return
}
prev = thiz
x0 = x1
x1 = x2
}
}
 
static void main(String[] args) {
for (int b = 0; b < 10; ++b) {
lucas(b)
metallic(b, 32)
}
println("Golden ratio, where b = 1:")
metallic(1, 256)
}
}</syntaxhighlight>
{{out}}
<pre>Lucas sequence for Platinum ratio, where b = 0
First 15 elements: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
Value after 2 iterations: 1
 
Lucas sequence for Golden ratio, where b = 1
First 15 elements: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377
Value after 78 iterations: 1.61803398874989484820458683436564
 
Lucas sequence for Silver ratio, where b = 2
First 15 elements: 1, 1, 3, 7, 17, 41, 99, 239, 577, 1393, 3363, 8119, 19601, 47321
Value after 44 iterations: 2.41421356237309504880168872420970
 
Lucas sequence for Bronze ratio, where b = 3
First 15 elements: 1, 1, 4, 13, 43, 142, 469, 1549, 5116, 16897, 55807, 184318, 608761, 2010601
Value after 34 iterations: 3.30277563773199464655961063373525
 
Lucas sequence for Copper ratio, where b = 4
First 15 elements: 1, 1, 5, 21, 89, 377, 1597, 6765, 28657, 121393, 514229, 2178309, 9227465, 39088169
Value after 28 iterations: 4.23606797749978969640917366873128
 
Lucas sequence for Nickel ratio, where b = 5
First 15 elements: 1, 1, 6, 31, 161, 836, 4341, 22541, 117046, 607771, 3155901, 16387276, 85092281, 441848681
Value after 25 iterations: 5.19258240356725201562535524577016
 
Lucas sequence for Aluminum ratio, where b = 6
First 15 elements: 1, 1, 7, 43, 265, 1633, 10063, 62011, 382129, 2354785, 14510839, 89419819, 551029753, 3395598337
Value after 23 iterations: 6.16227766016837933199889354443272
 
Lucas sequence for Iron ratio, where b = 7
First 15 elements: 1, 1, 8, 57, 407, 2906, 20749, 148149, 1057792, 7552693, 53926643, 385039194, 2749201001, 19629446201
Value after 22 iterations: 7.14005494464025913554865124576352
 
Lucas sequence for Tin ratio, where b = 8
First 15 elements: 1, 1, 9, 73, 593, 4817, 39129, 317849, 2581921, 20973217, 170367657, 1383914473, 11241683441, 91317382001
Value after 20 iterations: 8.12310562561766054982140985597408
 
Lucas sequence for Lead ratio, where b = 9
First 15 elements: 1, 1, 10, 91, 829, 7552, 68797, 626725, 5709322, 52010623, 473804929, 4316254984, 39320099785, 358197153049
Value after 20 iterations: 9.10977222864644365500113714088140
 
Golden ratio, where b = 1:
Value after 615 iterations: 1.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374847540880753868917521266338622235369317931800607667263544333890865959395829056383226613199282902678806752087668925017116962070322210432162695486262963136144</pre>
 
=={{header|J}}==
 
It's perhaps worth noting that we can compute the fibonacci ratio to 256 digits in ten iterations, if we use an appropriate form of iteration:
 
<syntaxhighlight lang="j">
258j256":%~/+/+/ .*~^:10 x:*i.2 2
1.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374847540880753868917521266338622235369317931800607667263544333890865959395829056383226613199282902678806752087668925017116962070322210432162695486262963136144
</syntaxhighlight>
 
However, the task implies we should use a different form of iteration, so we should probably stick to that here.
 
<syntaxhighlight lang="j">
task=:{{
32 task y
:
echo 'b=',":y
echo 'seq=',":(,(1,y) X _2{.])^:15]1 1x
k=. 0
prev=.val=. ''
rat=. 1 1x
whilst.-.prev-:val do.
k=. k+1
prev=. val
rat=. }.rat,rat X 1,y
val=. (j./2 0+x)":%~/rat
end.
echo (":k),' iterations: ',val
}}
 
task 0
b=0
seq=1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
2 iterations: 1.00000000000000000000000000000000
task 1
b=1
seq=1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
78 iterations: 1.61803398874989484820458683436564
task 2
b=2
seq=1 1 3 7 17 41 99 239 577 1393 3363 8119 19601 47321 114243 275807 665857
44 iterations: 2.41421356237309504880168872420970
task 3
b=3
seq=1 1 4 13 43 142 469 1549 5116 16897 55807 184318 608761 2010601 6640564 21932293 72437443
34 iterations: 3.30277563773199464655961063373525
task 4
b=4
seq=1 1 5 21 89 377 1597 6765 28657 121393 514229 2178309 9227465 39088169 165580141 701408733 2971215073
28 iterations: 4.23606797749978969640917366873128
task 5
b=5
seq=1 1 6 31 161 836 4341 22541 117046 607771 3155901 16387276 85092281 441848681 2294335686 11913527111 61861971241
25 iterations: 5.19258240356725201562535524577016
task 6
b=6
seq=1 1 7 43 265 1633 10063 62011 382129 2354785 14510839 89419819 551029753 3395598337 20924619775 128943316987 794584521697
23 iterations: 6.16227766016837933199889354443272
task 7
b=7
seq=1 1 8 57 407 2906 20749 148149 1057792 7552693 53926643 385039194 2749201001 19629446201 140155324408 1000716717057 7145172343807
22 iterations: 7.14005494464025913554865124576352
task 8
b=8
seq=1 1 9 73 593 4817 39129 317849 2581921 20973217 170367657 1383914473 11241683441 91317382001 741780739449 6025563297593 48946287120193
20 iterations: 8.12310562561766054982140985597408
task 9
b=9
seq=1 1 10 91 829 7552 68797 626725 5709322 52010623 473804929 4316254984 39320099785 358197153049 3263094477226 29726047448083 270797521509973
20 iterations: 9.10977222864644365500113714088140
 
256 task 1
b=1
seq=1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
615 iterations: 1.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374847540880753868917521266338622235369317931800607667263544333890865959395829056383226613199282902678806752087668925017116962070322210432162695486262963136144
</syntaxhighlight>
 
=={{header|Java}}==
<syntaxhighlight lang="java">
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
import java.util.ArrayList;
import java.util.List;
 
public class MetallicRatios {
 
private static String[] ratioDescription = new String[] {"Platinum", "Golden", "Silver", "Bronze", "Copper", "Nickel", "Aluminum", "Iron", "Tin", "Lead"};
public static void main(String[] args) {
int elements = 15;
for ( int b = 0 ; b < 10 ; b++ ) {
System.out.printf("Lucas sequence for %s ratio, where b = %d:%n", ratioDescription[b], b);
System.out.printf("First %d elements: %s%n", elements, lucasSequence(1, 1, b, elements));
int decimalPlaces = 32;
BigDecimal[] ratio = lucasSequenceRatio(1, 1, b, decimalPlaces+1);
System.out.printf("Value to %d decimal places after %s iterations : %s%n", decimalPlaces, ratio[1], ratio[0]);
System.out.printf("%n");
}
int b = 1;
int decimalPlaces = 256;
System.out.printf("%s ratio, where b = %d:%n", ratioDescription[b], b);
BigDecimal[] ratio = lucasSequenceRatio(1, 1, b, decimalPlaces+1);
System.out.printf("Value to %d decimal places after %s iterations : %s%n", decimalPlaces, ratio[1], ratio[0]);
}
private static BigDecimal[] lucasSequenceRatio(int x0, int x1, int b, int digits) {
BigDecimal x0Bi = BigDecimal.valueOf(x0);
BigDecimal x1Bi = BigDecimal.valueOf(x1);
BigDecimal bBi = BigDecimal.valueOf(b);
MathContext mc = new MathContext(digits);
BigDecimal fractionPrior = x1Bi.divide(x0Bi, mc);
int iterations = 0;
while ( true ) {
iterations++;
BigDecimal x = bBi.multiply(x1Bi).add(x0Bi);
BigDecimal fractionCurrent = x.divide(x1Bi, mc);
if ( fractionCurrent.compareTo(fractionPrior) == 0 ) {
break;
}
x0Bi = x1Bi;
x1Bi = x;
fractionPrior = fractionCurrent;
}
return new BigDecimal[] {fractionPrior, BigDecimal.valueOf(iterations)};
}
 
private static List<BigInteger> lucasSequence(int x0, int x1, int b, int n) {
List<BigInteger> list = new ArrayList<>();
BigInteger x0Bi = BigInteger.valueOf(x0);
BigInteger x1Bi = BigInteger.valueOf(x1);
BigInteger bBi = BigInteger.valueOf(b);
if ( n > 0 ) {
list.add(x0Bi);
}
if ( n > 1 ) {
list.add(x1Bi);
}
while ( n > 2 ) {
BigInteger x = bBi.multiply(x1Bi).add(x0Bi);
list.add(x);
n--;
x0Bi = x1Bi;
x1Bi = x;
}
return list;
}
}
</syntaxhighlight>
{{out}}
<pre>
Lucas sequence for Platinum ratio, where b = 0:
First 15 elements: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
Value to 32 decimal places after 1 iterations : 1
 
Lucas sequence for Golden ratio, where b = 1:
First 15 elements: [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610]
Value to 32 decimal places after 78 iterations : 1.61803398874989484820458683436564
 
Lucas sequence for Silver ratio, where b = 2:
First 15 elements: [1, 1, 3, 7, 17, 41, 99, 239, 577, 1393, 3363, 8119, 19601, 47321, 114243]
Value to 32 decimal places after 44 iterations : 2.41421356237309504880168872420970
 
Lucas sequence for Bronze ratio, where b = 3:
First 15 elements: [1, 1, 4, 13, 43, 142, 469, 1549, 5116, 16897, 55807, 184318, 608761, 2010601, 6640564]
Value to 32 decimal places after 34 iterations : 3.30277563773199464655961063373525
 
Lucas sequence for Copper ratio, where b = 4:
First 15 elements: [1, 1, 5, 21, 89, 377, 1597, 6765, 28657, 121393, 514229, 2178309, 9227465, 39088169, 165580141]
Value to 32 decimal places after 28 iterations : 4.23606797749978969640917366873128
 
Lucas sequence for Nickel ratio, where b = 5:
First 15 elements: [1, 1, 6, 31, 161, 836, 4341, 22541, 117046, 607771, 3155901, 16387276, 85092281, 441848681, 2294335686]
Value to 32 decimal places after 25 iterations : 5.19258240356725201562535524577016
 
Lucas sequence for Aluminum ratio, where b = 6:
First 15 elements: [1, 1, 7, 43, 265, 1633, 10063, 62011, 382129, 2354785, 14510839, 89419819, 551029753, 3395598337, 20924619775]
Value to 32 decimal places after 23 iterations : 6.16227766016837933199889354443272
 
Lucas sequence for Iron ratio, where b = 7:
First 15 elements: [1, 1, 8, 57, 407, 2906, 20749, 148149, 1057792, 7552693, 53926643, 385039194, 2749201001, 19629446201, 140155324408]
Value to 32 decimal places after 22 iterations : 7.14005494464025913554865124576352
 
Lucas sequence for Tin ratio, where b = 8:
First 15 elements: [1, 1, 9, 73, 593, 4817, 39129, 317849, 2581921, 20973217, 170367657, 1383914473, 11241683441, 91317382001, 741780739449]
Value to 32 decimal places after 20 iterations : 8.12310562561766054982140985597408
 
Lucas sequence for Lead ratio, where b = 9:
First 15 elements: [1, 1, 10, 91, 829, 7552, 68797, 626725, 5709322, 52010623, 473804929, 4316254984, 39320099785, 358197153049, 3263094477226]
Value to 32 decimal places after 20 iterations : 9.10977222864644365500113714088140
 
Golden ratio, where b = 1:
Value to 256 decimal places after 615 iterations : 1.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374847540880753868917521266338622235369317931800607667263544333890865959395829056383226613199282902678806752087668925017116962070322210432162695486262963136144
</pre>
 
=={{header|jq}}==
'''Works with gojq, the Go implementation of jq'''
{{trans|Wren}}
The "Rational" module used here is available at [[Arithmetic/Rational#jq]].
Note that in the version of this module used to produce the output shown below,
the function `r_to_decimal/1` does not perform rounding of the last digit, and
so these results should be correspondingly interpreted.
 
The accuracy of the following program in some cases depends on unbounded-precision integer
arithmetic, which the C implementation of jq does not support.
<syntaxhighlight lang="jq">
include "rational" {search: "."}; the defs at [[Arithmetic/Rational#jq]]
 
def names:
["Platinum", "Golden", "Silver", "Bronze", "Copper","Nickel", "Aluminium", "Iron", "Tin", "Lead"];
 
def lucas($b):
[1,1] | recurse( [last, first + $b*last] ) | first;
 
def lucas($b; $n):
"Lucas sequence for \(names[$b]) ratio, where b = \($b):",
"First \(n) elements: ",
[limit($n; lucas($b))];
 
# dp = integer (degrees of precision)
def metallic(b; dp):
{ x0: 1,
x1: 1,
x2: 0,
ratio: r(1; 1),
iters: 0 }
| .prev = (.ratio|r_to_decimal(dp)) # a string
| until(.emit;
.iters += 1
| .x2 = .b * .x1 + .x0
| .ratio = r(.x2; .x1)
| .curr = (.ratio|r_to_decimal(dp)) # a string
| if .prev == .curr
then (if (.iters == 1) then " " else "s" end) as $plural
| .emit = "Value to \(dp) dp after \(.iters) iteration\($plural): \(.curr)\n"
else .prev = .curr
| .x0 = .x1
| .x1 = .x2
end )
| .emit;
 
(range( 0;10) | lucas(.; 15), metallic(.; 32)),
 
"Golden ratio, where b = 1:", metallic(1; 256)
</syntaxhighlight>
Invocation: gojq -nrcf metallic-ratios.jq
{{output}}
<pre>
Lucas sequence for Platinum ratio, where b = 0:
First 15 elements:
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
Value to 32 dp after 1 iteration : 1
 
Lucas sequence for Golden ratio, where b = 1:
First 15 elements:
[1,1,2,3,5,8,13,21,34,55,89,144,233,377,610]
Value to 32 dp after 79 iterations: 1.61803398874989484820458683436563
 
Lucas sequence for Silver ratio, where b = 2:
First 15 elements:
[1,1,3,7,17,41,99,239,577,1393,3363,8119,19601,47321,114243]
Value to 32 dp after 45 iterations: 2.41421356237309504880168872420969
 
Lucas sequence for Bronze ratio, where b = 3:
First 15 elements:
[1,1,4,13,43,142,469,1549,5116,16897,55807,184318,608761,2010601,6640564]
Value to 32 dp after 33 iterations: 3.30277563773199464655961063373524
 
Lucas sequence for Copper ratio, where b = 4:
First 15 elements:
[1,1,5,21,89,377,1597,6765,28657,121393,514229,2178309,9227465,39088169,165580141]
Value to 32 dp after 28 iterations: 4.23606797749978969640917366873127
 
Lucas sequence for Nickel ratio, where b = 5:
First 15 elements:
[1,1,6,31,161,836,4341,22541,117046,607771,3155901,16387276,85092281,441848681,2294335686]
Value to 32 dp after 25 iterations: 5.19258240356725201562535524577016
 
Lucas sequence for Aluminium ratio, where b = 6:
First 15 elements:
[1,1,7,43,265,1633,10063,62011,382129,2354785,14510839,89419819,551029753,3395598337,20924619775]
Value to 32 dp after 23 iterations: 6.16227766016837933199889354443271
 
Lucas sequence for Iron ratio, where b = 7:
First 15 elements:
[1,1,8,57,407,2906,20749,148149,1057792,7552693,53926643,385039194,2749201001,19629446201,140155324408]
Value to 32 dp after 21 iterations: 7.14005494464025913554865124576351
 
Lucas sequence for Tin ratio, where b = 8:
First 15 elements:
[1,1,9,73,593,4817,39129,317849,2581921,20973217,170367657,1383914473,11241683441,91317382001,741780739449]
Value to 32 dp after 20 iterations: 8.12310562561766054982140985597407
 
Lucas sequence for Lead ratio, where b = 9:
First 15 elements:
[1,1,10,91,829,7552,68797,626725,5709322,52010623,473804929,4316254984,39320099785,358197153049,3263094477226]
Value to 32 dp after 19 iterations: 9.10977222864644365500113714088139
 
Golden ratio, where b = 1:
Value to 256 dp after 614 iterations: 1.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374847540880753868917521266338622235369317931800607667263544333890865959395829056383226613199282902678806752087668925017116962070322210432162695486262963136144
</pre>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Formatting
import Base.iterate, Base.IteratorSize, Base.IteratorEltype, Base.Iterators.take
 
Line 369 ⟶ 997:
println("Golden ratio to 256 decimal places:")
metallic(1, 256)
</langsyntaxhighlight>{{out}}
<pre>
The first 15 elements of the Lucas sequence named Platinum and b of 0 are:
Line 414 ⟶ 1,042:
After 615 iterations, the value of 1.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374847540880753868917521266338622235369317931800607667263544333890865959395829056383226613199282902678806752087668925017116962070322210432162695486262963136144 is stable to 256 decimal places.
</pre>
 
=={{header|Kotlin}}==
{{trans|Go}}
<syntaxhighlight lang="scala">import java.math.BigDecimal
import java.math.BigInteger
 
val names = listOf("Platinum", "Golden", "Silver", "Bronze", "Copper", "Nickel", "Aluminium", "Iron", "Tin", "Lead")
 
fun lucas(b: Long) {
println("Lucas sequence for ${names[b.toInt()]} ratio, where b = $b:")
print("First 15 elements: ")
var x0 = 1L
var x1 = 1L
print("$x0, $x1")
for (i in 1..13) {
val x2 = b * x1 + x0
print(", $x2")
x0 = x1
x1 = x2
}
println()
}
 
fun metallic(b: Long, dp:Int) {
var x0 = BigInteger.ONE
var x1 = BigInteger.ONE
var x2: BigInteger
val bb = BigInteger.valueOf(b)
val ratio = BigDecimal.ONE.setScale(dp)
var iters = 0
var prev = ratio.toString()
while (true) {
iters++
x2 = bb * x1 + x0
val thiz = (x2.toBigDecimal(dp) / x1.toBigDecimal(dp)).toString()
if (prev == thiz) {
var plural = "s"
if (iters == 1) {
plural = ""
}
println("Value after $iters iteration$plural: $thiz\n")
return
}
prev = thiz
x0 = x1
x1 = x2
}
}
 
fun main() {
for (b in 0L until 10L) {
lucas(b)
metallic(b, 32)
}
println("Golden ration, where b = 1:")
metallic(1, 256)
}</syntaxhighlight>
{{out}}
<pre>Lucas sequence for Platinum ratio, where b = 0:
First 15 elements: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
Value after 1 iteration: 1.00000000000000000000000000000000
 
Lucas sequence for Golden ratio, where b = 1:
First 15 elements: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610
Value after 78 iterations: 1.61803398874989484820458683436564
 
Lucas sequence for Silver ratio, where b = 2:
First 15 elements: 1, 1, 3, 7, 17, 41, 99, 239, 577, 1393, 3363, 8119, 19601, 47321, 114243
Value after 44 iterations: 2.41421356237309504880168872420970
 
Lucas sequence for Bronze ratio, where b = 3:
First 15 elements: 1, 1, 4, 13, 43, 142, 469, 1549, 5116, 16897, 55807, 184318, 608761, 2010601, 6640564
Value after 34 iterations: 3.30277563773199464655961063373525
 
Lucas sequence for Copper ratio, where b = 4:
First 15 elements: 1, 1, 5, 21, 89, 377, 1597, 6765, 28657, 121393, 514229, 2178309, 9227465, 39088169, 165580141
Value after 28 iterations: 4.23606797749978969640917366873128
 
Lucas sequence for Nickel ratio, where b = 5:
First 15 elements: 1, 1, 6, 31, 161, 836, 4341, 22541, 117046, 607771, 3155901, 16387276, 85092281, 441848681, 2294335686
Value after 25 iterations: 5.19258240356725201562535524577016
 
Lucas sequence for Aluminium ratio, where b = 6:
First 15 elements: 1, 1, 7, 43, 265, 1633, 10063, 62011, 382129, 2354785, 14510839, 89419819, 551029753, 3395598337, 20924619775
Value after 23 iterations: 6.16227766016837933199889354443272
 
Lucas sequence for Iron ratio, where b = 7:
First 15 elements: 1, 1, 8, 57, 407, 2906, 20749, 148149, 1057792, 7552693, 53926643, 385039194, 2749201001, 19629446201, 140155324408
Value after 22 iterations: 7.14005494464025913554865124576352
 
Lucas sequence for Tin ratio, where b = 8:
First 15 elements: 1, 1, 9, 73, 593, 4817, 39129, 317849, 2581921, 20973217, 170367657, 1383914473, 11241683441, 91317382001, 741780739449
Value after 20 iterations: 8.12310562561766054982140985597408
 
Lucas sequence for Lead ratio, where b = 9:
First 15 elements: 1, 1, 10, 91, 829, 7552, 68797, 626725, 5709322, 52010623, 473804929, 4316254984, 39320099785, 358197153049, 3263094477226
Value after 20 iterations: 9.10977222864644365500113714088140
 
Golden ration, where b = 1:
Value after 615 iterations: 1.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374847540880753868917521266338622235369317931800607667263544333890865959395829056383226613199282902678806752087668925017116962070322210432162695486262963136144</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">ClearAll[FindMetallicRatio]
FindMetallicRatio[b_, digits_] :=
Module[{n, m, data, acc, old, done = False},
{n, m} = {1, 1};
old = -100;
data = {};
While[done == False,
{n, m} = {m, b m + n};
AppendTo[data, {m, m/n}];
If[Length[data] > 15,
If[-N[Log10[Abs[data[[-1, 2]] - data[[-2, 2]]]]] > digits,
done = True
]
]
];
acc = -N[Log10[Abs[data[[-1, 2]] - data[[-2, 2]]]]];
<|"sequence" -> Join[{1, 1}, data[[All, 1]]],
"ratio" -> data[[All, 2]], "acc" -> acc,
"steps" -> Length[data]|>
]
Do[
out = FindMetallicRatio[b, 32];
Print["b=", b];
Print["b=", b, " first 15=", Take[out["sequence"], 15]];
Print["b=", b, " ratio=", N[Last[out["ratio"]], {\[Infinity], 33}]];
Print["b=", b, " Number of steps=", out["steps"]];
,
{b, 0, 9}
]
out = FindMetallicRatio[1, 256];
out["steps"]
N[out["ratio"][[-1]], 256]</syntaxhighlight>
{{out}}
<pre>b=0
b=0 first 15={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
b=0 ratio=1.00000000000000000000000000000000
b=0 Number of steps=16
b=1
b=1 first 15={1,1,2,3,5,8,13,21,34,55,89,144,233,377,610}
b=1 ratio=1.61803398874989484820458683436564
b=1 Number of steps=78
b=2
b=2 first 15={1,1,3,7,17,41,99,239,577,1393,3363,8119,19601,47321,114243}
b=2 ratio=2.41421356237309504880168872420970
b=2 Number of steps=44
b=3
b=3 first 15={1,1,4,13,43,142,469,1549,5116,16897,55807,184318,608761,2010601,6640564}
b=3 ratio=3.30277563773199464655961063373525
b=3 Number of steps=33
b=4
b=4 first 15={1,1,5,21,89,377,1597,6765,28657,121393,514229,2178309,9227465,39088169,165580141}
b=4 ratio=4.236067977499789696409173668731276
b=4 Number of steps=28
b=5
b=5 first 15={1,1,6,31,161,836,4341,22541,117046,607771,3155901,16387276,85092281,441848681,2294335686}
b=5 ratio=5.19258240356725201562535524577016
b=5 Number of steps=25
b=6
b=6 first 15={1,1,7,43,265,1633,10063,62011,382129,2354785,14510839,89419819,551029753,3395598337,20924619775}
b=6 ratio=6.162277660168379331998893544432719
b=6 Number of steps=23
b=7
b=7 first 15={1,1,8,57,407,2906,20749,148149,1057792,7552693,53926643,385039194,2749201001,19629446201,140155324408}
b=7 ratio=7.14005494464025913554865124576352
b=7 Number of steps=21
b=8
b=8 first 15={1,1,9,73,593,4817,39129,317849,2581921,20973217,170367657,1383914473,11241683441,91317382001,741780739449}
b=8 ratio=8.123105625617660549821409855974077
b=8 Number of steps=20
b=9
b=9 first 15={1,1,10,91,829,7552,68797,626725,5709322,52010623,473804929,4316254984,39320099785,358197153049,3263094477226}
b=9 ratio=9.10977222864644365500113714088140
b=9 Number of steps=19
614
1.618033988749894848204586834365638117720309179805762862135448622705260462818902449707207204189391137484754088075386891752126633862223536931793180060766726354433389086595939582905638322661319928290267880675208766892501711696207032221043216269548626296313614</pre>
 
=={{header|Nim}}==
{{libheader|bignum}}
<syntaxhighlight lang="nim">import strformat
import bignum
 
type Metal {.pure.} = enum platinum, golden, silver, bronze, copper, nickel, aluminium, iron, tin, lead
 
iterator sequence(b: int): Int =
## Yield the successive terms if a “Lucas” sequence.
## The first two terms are ignored.
var x, y = newInt(1)
while true:
x += b * y
swap x, y
yield y
 
 
template plural(n: int): string =
if n >= 2: "s" else: ""
 
 
proc computeRatio(b: Natural; digits: Positive) =
## Compute the ratio for the given "n" with the required number of digits.
 
let M = 10^culong(digits)
 
var niter = 0 # Number of iterations.
var prevN = newInt(1) # Previous value of "n".
var ratio = M # Current value of ratio.
 
for n in sequence(b):
inc niter
let nextRatio = n * M div prevN
if nextRatio == ratio: break
prevN = n.clone
ratio = nextRatio
 
var str = $ratio
str.insert(".", 1)
echo &"Value to {digits} decimal places after {niter} iteration{plural(niter)}: ", str
 
 
when isMainModule:
 
for b in 0..9:
echo &"“Lucas” sequence for {Metal(b)} ratio where b = {b}:"
stdout.write "First 15 elements: 1 1"
var count = 2
for n in sequence(b):
stdout.write ' ', n
inc count
if count == 15: break
echo ""
computeRatio(b, 32)
echo ""
 
echo "Golden ratio where b = 1:"
computeRatio(1, 256)</syntaxhighlight>
 
{{out}}
<pre>“Lucas” sequence for platinum ratio where b = 0:
First 15 elements: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Value to 32 decimal places after 1 iteration: 1.00000000000000000000000000000000
 
“Lucas” sequence for golden ratio where b = 1:
First 15 elements: 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610
Value to 32 decimal places after 79 iterations: 1.61803398874989484820458683436563
 
“Lucas” sequence for silver ratio where b = 2:
First 15 elements: 1 1 3 7 17 41 99 239 577 1393 3363 8119 19601 47321 114243
Value to 32 decimal places after 45 iterations: 2.41421356237309504880168872420969
 
“Lucas” sequence for bronze ratio where b = 3:
First 15 elements: 1 1 4 13 43 142 469 1549 5116 16897 55807 184318 608761 2010601 6640564
Value to 32 decimal places after 33 iterations: 3.30277563773199464655961063373524
 
“Lucas” sequence for copper ratio where b = 4:
First 15 elements: 1 1 5 21 89 377 1597 6765 28657 121393 514229 2178309 9227465 39088169 165580141
Value to 32 decimal places after 28 iterations: 4.23606797749978969640917366873127
 
“Lucas” sequence for nickel ratio where b = 5:
First 15 elements: 1 1 6 31 161 836 4341 22541 117046 607771 3155901 16387276 85092281 441848681 2294335686
Value to 32 decimal places after 25 iterations: 5.19258240356725201562535524577016
 
“Lucas” sequence for aluminium ratio where b = 6:
First 15 elements: 1 1 7 43 265 1633 10063 62011 382129 2354785 14510839 89419819 551029753 3395598337 20924619775
Value to 32 decimal places after 23 iterations: 6.16227766016837933199889354443271
 
“Lucas” sequence for iron ratio where b = 7:
First 15 elements: 1 1 8 57 407 2906 20749 148149 1057792 7552693 53926643 385039194 2749201001 19629446201 140155324408
Value to 32 decimal places after 21 iterations: 7.14005494464025913554865124576351
 
“Lucas” sequence for tin ratio where b = 8:
First 15 elements: 1 1 9 73 593 4817 39129 317849 2581921 20973217 170367657 1383914473 11241683441 91317382001 741780739449
Value to 32 decimal places after 20 iterations: 8.12310562561766054982140985597407
 
“Lucas” sequence for lead ratio where b = 9:
First 15 elements: 1 1 10 91 829 7552 68797 626725 5709322 52010623 473804929 4316254984 39320099785 358197153049 3263094477226
Value to 32 decimal places after 19 iterations: 9.10977222864644365500113714088139
 
Golden ratio where b = 1:
Value to 256 decimal places after 614 iterations: 1.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374847540880753868917521266338622235369317931800607667263544333890865959395829056383226613199282902678806752087668925017116962070322210432162695486262963136144</pre>
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature qw(say state);
Line 454 ⟶ 1,362:
}
 
printf "\nGolden ratio to 256 decimal places %s reached after %d iterations", metallic(gen_lucas(1),256);</langsyntaxhighlight>
{{out}}
<pre>'Lucas' sequence for Platinum ratio, where b = 0:
Line 498 ⟶ 1,406:
Golden ratio to 256 decimal places 1.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374847540880753868917521266338622235369317931800607667263544333890865959395829056383226613199282902678806752087668925017116962070322210432162695486262963136144 reached after 615 iterations</pre>
 
=={{header|Perl 6Phix}}==
{{trans|Go}}
{{libheader|Phix/mpfr}}
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">names</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"Platinum"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Golden"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Silver"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Bronze"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Copper"</span><span style="color: #0000FF;">,</span>
<span style="color: #008000;">"Nickel"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Aluminium"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Iron"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Tin"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"Lead"</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">lucas</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Lucas sequence for %s ratio, where b = %d:\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">names</span><span style="color: #0000FF;">[</span><span style="color: #000000;">b</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">})</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">x0</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">x1</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">x2</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"First 15 elements: %d, %d"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">x0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">x1</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">13</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">x2</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">*</span><span style="color: #000000;">x1</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">x0</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">", %d"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">x2</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">x0</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">x1</span>
<span style="color: #000000;">x1</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">x2</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">metallic</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">dp</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">mpz</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">x0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">x1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">x2</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">bb</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpz_inits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">b</span><span style="color: #0000FF;">})</span>
<span style="color: #004080;">mpfr</span> <span style="color: #000000;">ratio</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_init</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,-(</span><span style="color: #000000;">dp</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">))</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">iterations</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">prev</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_get_fixed</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ratio</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dp</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">while</span> <span style="color: #004600;">true</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">iterations</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #7060A8;">mpz_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">bb</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x1</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpz_add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x0</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpfr_set_z</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ratio</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x2</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpfr_div_z</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ratio</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ratio</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x1</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">curr</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_get_fixed</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ratio</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dp</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">prev</span> <span style="color: #0000FF;">==</span> <span style="color: #000000;">curr</span> <span style="color: #008080;">then</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">plural</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">iterations</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">?</span><span style="color: #008000;">""</span><span style="color: #0000FF;">:</span><span style="color: #008000;">"s"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">curr</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">curr</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Value to %d dp after %2d iteration%s: %s\n\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">dp</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">iterations</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">plural</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">curr</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">exit</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">prev</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">curr</span>
<span style="color: #7060A8;">mpz_set</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x1</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">mpz_set</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x2</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">b</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">9</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">lucas</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">metallic</span><span style="color: #0000FF;">(</span><span style="color: #000000;">b</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">32</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Golden ratio, where b = 1:\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">metallic</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">256</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</syntaxhighlight>-->
{{out}}
The final 258 digits includes the "1.", and dp+2 was needed on ratio to exactly match the Go (etc) output.
<pre style="height:45ex">
Lucas sequence for Platinum ratio, where b = 0:
First 15 elements: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
Value to 32 dp after 1 iteration: 1.00000000000000000000000000000000
 
Lucas sequence for Golden ratio, where b = 1:
First 15 elements: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610
Value to 32 dp after 78 iterations: 1.61803398874989484820458683436564
 
Lucas sequence for Silver ratio, where b = 2:
First 15 elements: 1, 1, 3, 7, 17, 41, 99, 239, 577, 1393, 3363, 8119, 19601, 47321, 114243
Value to 32 dp after 44 iterations: 2.41421356237309504880168872420970
 
Lucas sequence for Bronze ratio, where b = 3:
First 15 elements: 1, 1, 4, 13, 43, 142, 469, 1549, 5116, 16897, 55807, 184318, 608761, 2010601, 6640564
Value to 32 dp after 34 iterations: 3.30277563773199464655961063373525
 
Lucas sequence for Copper ratio, where b = 4:
First 15 elements: 1, 1, 5, 21, 89, 377, 1597, 6765, 28657, 121393, 514229, 2178309, 9227465, 39088169, 165580141
Value to 32 dp after 28 iterations: 4.23606797749978969640917366873128
 
Lucas sequence for Nickel ratio, where b = 5:
First 15 elements: 1, 1, 6, 31, 161, 836, 4341, 22541, 117046, 607771, 3155901, 16387276, 85092281, 441848681, 2294335686
Value to 32 dp after 25 iterations: 5.19258240356725201562535524577016
 
Lucas sequence for Aluminium ratio, where b = 6:
First 15 elements: 1, 1, 7, 43, 265, 1633, 10063, 62011, 382129, 2354785, 14510839, 89419819, 551029753, 3395598337, 20924619775
Value to 32 dp after 23 iterations: 6.16227766016837933199889354443272
 
Lucas sequence for Iron ratio, where b = 7:
First 15 elements: 1, 1, 8, 57, 407, 2906, 20749, 148149, 1057792, 7552693, 53926643, 385039194, 2749201001, 19629446201, 140155324408
Value to 32 dp after 22 iterations: 7.14005494464025913554865124576352
 
Lucas sequence for Tin ratio, where b = 8:
First 15 elements: 1, 1, 9, 73, 593, 4817, 39129, 317849, 2581921, 20973217, 170367657, 1383914473, 11241683441, 91317382001, 741780739449
Value to 32 dp after 20 iterations: 8.12310562561766054982140985597408
 
Lucas sequence for Lead ratio, where b = 9:
First 15 elements: 1, 1, 10, 91, 829, 7552, 68797, 626725, 5709322, 52010623, 473804929, 4316254984, 39320099785, 358197153049, 3263094477226
Value to 32 dp after 20 iterations: 9.10977222864644365500113714088140
 
Golden ratio, where b = 1:
Value to 256 dp after 615 iterations: 1.61803398874989484...2695486262963136144 (258 digits)
</pre>
 
=={{header|Python}}==
<syntaxhighlight lang="python">from itertools import count, islice
from _pydecimal import getcontext, Decimal
 
def metallic_ratio(b):
m, n = 1, 1
while True:
yield m, n
m, n = m*b + n, m
 
def stable(b, prec):
def to_decimal(b):
for m,n in metallic_ratio(b):
yield Decimal(m)/Decimal(n)
 
getcontext().prec = prec
last = 0
for i,x in zip(count(), to_decimal(b)):
if x == last:
print(f'after {i} iterations:\n\t{x}')
break
last = x
 
for b in range(4):
coefs = [n for _,n in islice(metallic_ratio(b), 15)]
print(f'\nb = {b}: {coefs}')
stable(b, 32)
 
print(f'\nb = 1 with 256 digits:')
stable(1, 256)</syntaxhighlight>
{{out}}
<pre>b = 0: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
after 1 iterations:
1
 
b = 1: [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610]
after 77 iterations:
1.6180339887498948482045868343656
 
b = 2: [1, 1, 3, 7, 17, 41, 99, 239, 577, 1393, 3363, 8119, 19601, 47321, 114243]
after 43 iterations:
2.4142135623730950488016887242097
 
b = 3: [1, 1, 4, 13, 43, 142, 469, 1549, 5116, 16897, 55807, 184318, 608761, 2010601, 6640564]
after 33 iterations:
3.3027756377319946465596106337352
 
b = 1 with 256 digits:
after 613 iterations:
1.618033988749894848204586834365638117720309179805762862135448622705260462818902449707207204189391137484754088075386891752126633862223536931793180060766726354433389086595939582905638322661319928290267880675208766892501711696207032221043216269548626296313614
</pre>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="Quackery"> [ $ "bigrat.qky" loadfile ] now!
 
[ [ table
$ "Platinum" $ "Golden"
$ "Silver" $ "Bronze"
$ "Copper" $ "Nickel"
$ "Aluminium" $ "Iron"
$ "Tin" $ "Lead" ]
do echo$ ] is echoname ( n --> )
 
[ temp put
' [ 1 1 ]
13 times
[ dup -1 peek
temp share *
over -2 peek +
join ]
temp release ] is task1 ( n --> [ )
 
[ temp put
' [ 0 1 1 ]
[ dup -3 split nip
do dip tuck swap
32 approx= if done
dup -1 peek
temp share *
over -2 peek +
join
again ]
temp release
dup size 3 - swap
-3 split nip
-1 split drop
do swap rot ] is task2 ( n --> n/d n )
 
10 times
[ i^ echoname cr
i^ task1
witheach [ echo sp ] cr
i^ task2
echo sp
32 point$ echo$
cr cr ]</syntaxhighlight>
 
{{out}}
 
<pre>Platinum
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1
 
Golden
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610
78 1.61803398874989484820458683436564
 
Silver
1 1 3 7 17 41 99 239 577 1393 3363 8119 19601 47321 114243
44 2.4142135623730950488016887242097
 
Bronze
1 1 4 13 43 142 469 1549 5116 16897 55807 184318 608761 2010601 6640564
33 3.30277563773199464655961063373524
 
Copper
1 1 5 21 89 377 1597 6765 28657 121393 514229 2178309 9227465 39088169 165580141
28 4.23606797749978969640917366873128
 
Nickel
1 1 6 31 161 836 4341 22541 117046 607771 3155901 16387276 85092281 441848681 2294335686
25 5.19258240356725201562535524577016
 
Aluminium
1 1 7 43 265 1633 10063 62011 382129 2354785 14510839 89419819 551029753 3395598337 20924619775
23 6.16227766016837933199889354443272
 
Iron
1 1 8 57 407 2906 20749 148149 1057792 7552693 53926643 385039194 2749201001 19629446201 140155324408
21 7.14005494464025913554865124576351
 
Tin
1 1 9 73 593 4817 39129 317849 2581921 20973217 170367657 1383914473 11241683441 91317382001 741780739449
20 8.12310562561766054982140985597408
 
Lead
1 1 10 91 829 7552 68797 626725 5709322 52010623 473804929 4316254984 39320099785 358197153049 3263094477226
19 9.10977222864644365500113714088139
</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2019.07.1}}
 
Note: Arrays, Lists and Sequences are zero indexed in Perl 6Raku.
 
<syntaxhighlight lang="raku" perl6line>use Rat::Precise;
use Lingua::EN::Numbers;
 
Line 535 ⟶ 1,689:
 
# Stretch goal
say join "\n", "\nGolden ratio to 256 decimal places:", display |metallic lucas(1), 256;</langsyntaxhighlight>
{{out}}
<pre>Lucas sequence for Platinum ratio; where b = 0:
Line 584 ⟶ 1,738:
=={{header|REXX}}==
For this task, &nbsp; the elements of the Lucas sequence are zero based.
<langsyntaxhighlight lang="rexx">/*REXX pgm computes the 1st N elements of the Lucas sequence for Metallic ratios 0──►9. */
parse arg n bLO bHI digs . /*obtain optional arguments from the CL*/
if n=='' | n=="," then n= 15 /*Not specified? Then use the default.*/
Line 592 ⟶ 1,746:
numeric digits digs + length(.) /*specify number of decimal digs to use*/
metals= 'platinum golden silver bronze copper nickel aluminum iron tin lead'
@decDigs= ' decimal digits past the decimal point:' /*a literal used in SAY.*/
approx= 'the approximate value reached after ' /*literals that are used for SAYs. */
!.= /*the default name for a metallic ratio*/
do k=0 to 9; !.k= word(metals, k+1) /*assign the (ten) metallic ratio names*/
Line 611 ⟶ 1,765:
if n>0 then do; say 'the first ' n " elements are:"; say $
end /*if N is positive, then show N nums.*/
@approx= 'approximate' /*literal (1 word) that is used for SAY*/
say approx #-1 ' iterations with ' digs " decimal digits past the decimal point:"
sayr= format(r,,digs); say /*displaylimit thedecimal rationdigits plusfor a blankR line to digs.*/
endif datatype(r, 'W') then do; /*m*/ r= r/1; @approx= "exact"; /*stick a fork in it, we're all done. */</lang>end
say 'the' @approx "value reached after" #-1 " iterations with " digs @DecDigs
say r; say /*display the ration plus a blank line.*/
end /*m*/ /*stick a fork in it, we're all done. */</syntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
 
Line 621 ⟶ 1,778:
the first 15 elements are:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
the approximateexact value reached after 2 iterations with 32 decimal digits past the decimal point:
1
1.00000000000000000000000000000000
 
══════════════════════════ Lucas sequence for the golden ratio, where B is 1 ═══════════════════════════
the first 15 elements are:
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610
the approximate value reached after 78 iterations with 32 decimal digits past the decimal point:
1.61803398874989484820458683436564
 
Line 633 ⟶ 1,790:
the first 15 elements are:
1 1 3 7 17 41 99 239 577 1393 3363 8119 19601 47321 114243
the approximate value reached after 44 iterations with 32 decimal digits past the decimal point:
2.41421356237309504880168872420970
 
Line 639 ⟶ 1,796:
the first 15 elements are:
1 1 4 13 43 142 469 1549 5116 16897 55807 184318 608761 2010601 6640564
the approximate value reached after 34 iterations with 32 decimal digits past the decimal point:
3.30277563773199464655961063373525
 
Line 645 ⟶ 1,802:
the first 15 elements are:
1 1 5 21 89 377 1597 6765 28657 121393 514229 2178309 9227465 39088169 165580141
the approximate value reached after 28 iterations with 32 decimal digits past the decimal point:
4.23606797749978969640917366873128
 
Line 651 ⟶ 1,808:
the first 15 elements are:
1 1 6 31 161 836 4341 22541 117046 607771 3155901 16387276 85092281 441848681 2294335686
the approximate value reached after 25 iterations with 32 decimal digits past the decimal point:
5.19258240356725201562535524577016
 
Line 657 ⟶ 1,814:
the first 15 elements are:
1 1 7 43 265 1633 10063 62011 382129 2354785 14510839 89419819 551029753 3395598337 20924619775
the approximate value reached after 23 iterations with 32 decimal digits past the decimal point:
6.16227766016837933199889354443272
 
Line 663 ⟶ 1,820:
the first 15 elements are:
1 1 8 57 407 2906 20749 148149 1057792 7552693 53926643 385039194 2749201001 19629446201 140155324408
the approximate value reached after 22 iterations with 32 decimal digits past the decimal point:
7.14005494464025913554865124576352
 
Line 669 ⟶ 1,826:
the first 15 elements are:
1 1 9 73 593 4817 39129 317849 2581921 20973217 170367657 1383914473 11241683441 91317382001 741780739449
the approximate value reached after 20 iterations with 32 decimal digits past the decimal point:
8.12310562561766054982140985597408
 
Line 675 ⟶ 1,832:
the first 15 elements are:
1 1 10 91 829 7552 68797 626725 5709322 52010623 473804929 4316254984 39320099785 358197153049 3263094477226
the approximate value reached after 20 iterations with 32 decimal digits past the decimal point:
9.10977222864644365500113714088140
</pre>
Line 686 ⟶ 1,843:
1.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374847540880753868917521266338622235369317931800607667263544333890865959395829056383226613199282902678806752087668925017116962070322210432162695486262963136144
</pre>
=={{header|RPL}}==
{{works with|HP|49}}
« UNROT SWAP OVER IDIV2 SWAP "." + 4 ROLLD UNROT
→ b
« 1 SWAP '''START'''
10 * b IDIV2
UNROT + SWAP
'''NEXT''' DROP
» » '<span style="color:blue">LDIVN</span>' STO <span style="color:grey">@ ''( a b p → "float" )'' with float = a/b with p decimal places</span>
« 0 9 '''FOR''' b
{ "Pt" "Au" "Ag" "CuSn" "Cu" "Ni" "Al" "Fe" "Sn" "Pb" } b 1 + DUP SUB
1 1
3 15 '''START''' DUP2 b * + '''NEXT'''
15 →LIST "seq" →TAG +
0 1 1 "1.00000000000000000000000000000000"
'''DO''' UNROT DUP b * ROT +
4 ROLL 1 + 4 ROLLD
'''UNTIL''' DUP2 SWAP 32 <span style="color:blue">LDIVN</span> 4 ROLL OVER == '''END'''
"ratio" →TAG UNROT DROP2 ROT SWAP +
SWAP "iter" →TAG +
'''NEXT'''
» '<span style="color:blue">TASK</span>' STO
{{out}}
<pre>
10: { "Pt" seq:{ 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 } ratio:"1.00000000000000000000000000000000" iter:1 }
9: { "Au" seq:{ 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 }
ratio:"1.61803398874989484820458683436563" iter:79 }
8: { "Ag" seq:{ 1 1 3 7 17 41 99 239 577 1393 3363 8119 19601 47321 114243 } ratio:"2.41421356237309504880168872420969" iter:45 }
7: { "CuSn" seq:{ 1 1 4 13 43 142 469 1549 5116 16897 55807 184318 608761 2010601 6640564 } ratio:"3.30277563773199464655961063373524" iter:33 } { "Cu" seq:{ 1 1 5 6: 21 89 377 1597 6765 28657 121393 514229 2178309 9227465 39088169 165580141 } ratio:"4.23606797749978969640917366873127" iter:28 }
5: { "Ni" seq:{ 1 1 6 31 161 836 4341 22541 117046 607771 3155901 16387276 85092281 441848681 2294335686 } ratio:"5.19258240356725201562535524577016" iter:25 }
4: { "Al" seq:{ 1 1 7 43 265 1633 10063 62011 382129 2354785 14510839 89419819 551029753 3395598337 20924619775 } ratio:"6.16227766016837933199889354443271" iter:23 }
3: { "Fe" seq:{ 1 1 8 57 407 2906 20749 148149 1057792 7552693 53926643 385039194 2749201001 19629446201 140155324408 } ratio:"7.14005494464025913554865124576351" iter:21 }
2: { "Sn" seq:{ 1 1 9 73 593 4817 39129 317849 2581921 20973217 170367657 1383914473 11241683441 91317382001 741780739449 } ratio:"8.12310562561766054982140985597407" iter:20 }
1: { "Pb" seq:{ 1 1 10 91 829 7552 68797 626725 5709322 52010623 473804929 4316254984 39320099785 358197153049 3263094477226 } ratio:"9.10977222864644365500113714088139" iter:19 }
</pre>
 
=={{header|Ruby}}==
{{works with|Ruby|2.3}}
<syntaxhighlight lang="ruby">require('bigdecimal')
require('bigdecimal/util')
 
# An iterator over the Lucas Sequence for 'b'.
# (The special case of: x(n) = b * x(n-1) + x(n-2).)
 
def lucas(b)
Enumerator.new do |yielder|
xn2 = 1 ; yielder.yield(xn2)
xn1 = 1 ; yielder.yield(xn1)
loop { xn2, xn1 = xn1, b * xn1 + xn2 ; yielder.yield(xn1) }
end
end
 
# Compute the Metallic Ratio to 'precision' from the Lucas Sequence for 'b'.
# (Uses the lucas(b) iterator, above.)
# The metallic ratio is approximated by x(n) / x(n-1).
# Returns a struct of the approximate metallic ratio (.ratio) and the
# number of terms required to achieve the given precision (.terms).
 
def metallic_ratio(b, precision)
xn2 = xn1 = prev = this = 0
lucas(b).each.with_index do |xn, inx|
case inx
when 0
xn2 = BigDecimal(xn)
when 1
xn1 = BigDecimal(xn)
prev = xn1.div(xn2, 2 * precision).round(precision)
else
xn2, xn1 = xn1, BigDecimal(xn)
this = xn1.div(xn2, 2 * precision).round(precision)
return Struct.new(:ratio, :terms).new(prev, inx - 1) if prev == this
prev = this
end
end
end
 
NAMES = [ 'Platinum', 'Golden', 'Silver', 'Bronze', 'Copper',
'Nickel', 'Aluminum', 'Iron', 'Tin', 'Lead' ]
 
puts
puts('Lucas Sequences...')
puts('%1s %s' % ['b', 'sequence'])
(0..9).each do |b|
puts('%1d %s' % [b, lucas(b).first(15)])
end
 
puts
puts('Metallic Ratios to 32 places...')
puts('%-9s %1s %3s %s' % ['name', 'b', 'n', 'ratio'])
(0..9).each do |b|
rn = metallic_ratio(b, 32)
puts('%-9s %1d %3d %s' % [NAMES[b], b, rn.terms, rn.ratio.to_s('F')])
end
 
puts
puts('Golden Ratio to 256 places...')
puts('%-9s %1s %3s %s' % ['name', 'b', 'n', 'ratio'])
gold_rn = metallic_ratio(1, 256)
puts('%-9s %1d %3d %s' % [NAMES[1], 1, gold_rn.terms, gold_rn.ratio.to_s('F')])</syntaxhighlight>
{{out}}
<pre>Lucas Sequences...
b sequence
0 [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
1 [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610]
2 [1, 1, 3, 7, 17, 41, 99, 239, 577, 1393, 3363, 8119, 19601, 47321, 114243]
3 [1, 1, 4, 13, 43, 142, 469, 1549, 5116, 16897, 55807, 184318, 608761, 2010601, 6640564]
4 [1, 1, 5, 21, 89, 377, 1597, 6765, 28657, 121393, 514229, 2178309, 9227465, 39088169, 165580141]
5 [1, 1, 6, 31, 161, 836, 4341, 22541, 117046, 607771, 3155901, 16387276, 85092281, 441848681, 2294335686]
6 [1, 1, 7, 43, 265, 1633, 10063, 62011, 382129, 2354785, 14510839, 89419819, 551029753, 3395598337, 20924619775]
7 [1, 1, 8, 57, 407, 2906, 20749, 148149, 1057792, 7552693, 53926643, 385039194, 2749201001, 19629446201, 140155324408]
8 [1, 1, 9, 73, 593, 4817, 39129, 317849, 2581921, 20973217, 170367657, 1383914473, 11241683441, 91317382001, 741780739449]
9 [1, 1, 10, 91, 829, 7552, 68797, 626725, 5709322, 52010623, 473804929, 4316254984, 39320099785, 358197153049, 3263094477226]
 
Metallic Ratios to 32 places...
name b n ratio
Platinum 0 1 1.0
Golden 1 78 1.61803398874989484820458683436564
Silver 2 44 2.4142135623730950488016887242097
Bronze 3 34 3.30277563773199464655961063373525
Copper 4 28 4.23606797749978969640917366873128
Nickel 5 25 5.19258240356725201562535524577016
Aluminum 6 23 6.16227766016837933199889354443272
Iron 7 22 7.14005494464025913554865124576352
Tin 8 20 8.12310562561766054982140985597408
Lead 9 20 9.1097722286464436550011371408814
 
Golden Ratio to 256 places...
name b n ratio
Golden 1 615 1.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374847540880753868917521266338622235369317931800607667263544333890865959395829056383226613199282902678806752087668925017116962070322210432162695486262963136144</pre>
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func seqRatio(f, places = 32) {
1..Inf -> reduce {|t,n|
var r = (f(n+1)/f(n)).round(-places)
Line 710 ⟶ 1,997:
say "Approximated value: #{v}"
say "Reached after #{n} iterations"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 752 ⟶ 2,039:
Approximated value: 1.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374847540880753868917521266338622235369317931800607667263544333890865959395829056383226613199282902678806752087668925017116962070322210432162695486262963136144
Reached after 616 iterations
</pre>
 
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<syntaxhighlight lang="vbnet">Imports BI = System.Numerics.BigInteger
 
Module Module1
 
Function IntSqRoot(v As BI, res As BI) As BI
REM res is the initial guess
Dim term As BI = 0
Dim d As BI = 0
Dim dl As BI = 1
While dl <> d
term = v / res
res = (res + term) >> 1
dl = d
d = term - res
End While
Return term
End Function
 
Function DoOne(b As Integer, digs As Integer) As String
REM calculates result via square root, not iterations
Dim s = b * b + 4
digs += 1
Dim g As BI = Math.Sqrt(s * Math.Pow(10, digs))
Dim bs = IntSqRoot(s * BI.Parse("1" + New String("0", digs << 1)), g)
bs += b * BI.Parse("1" + New String("0", digs))
bs >>= 1
bs += 4
Dim st = bs.ToString
digs -= 1
Return String.Format("{0}.{1}", st(0), st.Substring(1, digs))
End Function
 
Function DivIt(a As BI, b As BI, digs As Integer) As String
REM performs division
Dim al = a.ToString.Length
Dim bl = b.ToString.Length
digs += 1
a *= BI.Pow(10, digs << 1)
b *= BI.Pow(10, digs)
Dim s = (a / b + 5).ToString
digs -= 1
Return s(0) + "." + s.Substring(1, digs)
End Function
 
REM custom formatting
Function Joined(x() As BI) As String
Dim wids() = {1, 1, 2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}
Dim res = ""
For i = 0 To x.Length - 1
res += String.Format("{0," + (-wids(i)).ToString + "} ", x(i))
Next
Return res
End Function
 
Sub Main()
REM calculates and checks each "metal"
Console.WriteLine("Metal B Sq.Rt Iters /---- 32 decimal place value ----\\ Matches Sq.Rt Calc")
Dim t = ""
Dim n As BI
Dim nm1 As BI
Dim k As Integer
Dim j As Integer
For b = 0 To 9
Dim lst(14) As BI
lst(0) = 1
lst(1) = 1
For i = 2 To 14
lst(i) = b * lst(i - 1) + lst(i - 2)
Next
REM since all the iterations (except Pt) are > 15, continue iterating from the end of the list of 15
n = lst(14)
nm1 = lst(13)
k = 0
j = 13
While k = 0
Dim lt = t
t = DivIt(n, nm1, 32)
If lt = t Then
k = If(b = 0, 1, j)
End If
Dim onn = n
n = b * n + nm1
nm1 = onn
 
j += 1
End While
Console.WriteLine("{0,4} {1} {2,2} {3, 2} {4} {5}" + vbNewLine + "{6,19} {7}", "Pt Au Ag CuSn Cu Ni Al Fe Sn Pb".Split(" ")(b), b, b * b + 4, k, t, t = DoOne(b, 32), "", Joined(lst))
Next
REM now calculate and check big one
n = 1
nm1 = 1
k = 0
j = 1
While k = 0
Dim lt = t
t = DivIt(n, nm1, 256)
If lt = t Then
k = j
End If
Dim onn = n
n += nm1
nm1 = onn
 
j += 1
End While
Console.WriteLine()
Console.WriteLine("Au to 256 digits:")
Console.WriteLine(t)
Console.WriteLine("Iteration count: {0} Matched Sq.Rt Calc: {1}", k, t = DoOne(1, 256))
End Sub
 
End Module</syntaxhighlight>
{{out}}
<pre>Metal B Sq.Rt Iters /---- 32 decimal place value ----\\ Matches Sq.Rt Calc
Pt 0 4 1 1.00000000000000000000000000000000 True
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Au 1 5 78 1.61803398874989484820458683436564 True
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610
Ag 2 8 44 2.41421356237309504880168872420970 True
1 1 3 7 17 41 99 239 577 1393 3363 8119 19601 47321 114243
CuSn 3 13 34 3.30277563773199464655961063373525 True
1 1 4 13 43 142 469 1549 5116 16897 55807 184318 608761 2010601 6640564
Cu 4 20 28 4.23606797749978969640917366873128 True
1 1 5 21 89 377 1597 6765 28657 121393 514229 2178309 9227465 39088169 165580141
Ni 5 29 25 5.19258240356725201562535524577016 True
1 1 6 31 161 836 4341 22541 117046 607771 3155901 16387276 85092281 441848681 2294335686
Al 6 40 23 6.16227766016837933199889354443272 True
1 1 7 43 265 1633 10063 62011 382129 2354785 14510839 89419819 551029753 3395598337 20924619775
Fe 7 53 22 7.14005494464025913554865124576352 True
1 1 8 57 407 2906 20749 148149 1057792 7552693 53926643 385039194 2749201001 19629446201 140155324408
Sn 8 68 20 8.12310562561766054982140985597408 True
1 1 9 73 593 4817 39129 317849 2581921 20973217 170367657 1383914473 11241683441 91317382001 741780739449
Pb 9 85 20 9.10977222864644365500113714088140 True
1 1 10 91 829 7552 68797 626725 5709322 52010623 473804929 4316254984 39320099785 358197153049 3263094477226
 
Au to 256 digits:
1.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374847540880753868917521266338622235369317931800607667263544333890865959395829056383226613199282902678806752087668925017116962070322210432162695486262963136144
Iteration count: 616 Matched Sq.Rt Calc: True</pre>
 
=={{header|Wren}}==
{{trans|Go}}
{{libheader|Wren-big}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./big" for BigInt, BigRat
import "./fmt" for Fmt
 
var names = ["Platinum", "Golden", "Silver", "Bronze", "Copper","Nickel", "Aluminium", "Iron", "Tin", "Lead"]
 
var lucas = Fn.new { |b|
Fmt.print("Lucas sequence for $s ratio, where b = $d:", names[b], b)
System.write("First 15 elements: ")
var x0 = 1
var x1 = 1
Fmt.write("$d, $d", x0, x1)
for (i in 1..13) {
var x2 = b*x1 + x0
Fmt.write(", $d", x2)
x0 = x1
x1 = x2
}
System.print()
}
 
var metallic = Fn.new { |b, dp|
var x0 = BigInt.one
var x1 = BigInt.one
var x2 = BigInt.zero
var bb = BigInt.new(b)
var ratio = BigRat.new(BigInt.one, BigInt.one)
var iters = 0
var prev = ratio.toDecimal(dp)
while (true) {
iters = iters + 1
x2 = bb*x1 + x0
ratio = BigRat.new(x2, x1)
var curr = ratio.toDecimal(dp)
if (prev == curr) {
var plural = (iters == 1) ? " " : "s"
Fmt.print("Value to $d dp after $2d iteration$s: $s\n", dp, iters, plural, curr)
return
}
prev = curr
x0 = x1
x1 = x2
}
}
 
for (b in 0..9) {
lucas.call(b)
metallic.call(b, 32)
}
System.print("Golden ratio, where b = 1:")
metallic.call(1, 256)</syntaxhighlight>
 
{{out}}
<pre>
Lucas sequence for Platinum ratio, where b = 0:
First 15 elements: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
Value to 32 dp after 1 iteration : 1
 
Lucas sequence for Golden ratio, where b = 1:
First 15 elements: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610
Value to 32 dp after 78 iterations: 1.61803398874989484820458683436564
 
Lucas sequence for Silver ratio, where b = 2:
First 15 elements: 1, 1, 3, 7, 17, 41, 99, 239, 577, 1393, 3363, 8119, 19601, 47321, 114243
Value to 32 dp after 44 iterations: 2.41421356237309504880168872420970
 
Lucas sequence for Bronze ratio, where b = 3:
First 15 elements: 1, 1, 4, 13, 43, 142, 469, 1549, 5116, 16897, 55807, 184318, 608761, 2010601, 6640564
Value to 32 dp after 34 iterations: 3.30277563773199464655961063373525
 
Lucas sequence for Copper ratio, where b = 4:
First 15 elements: 1, 1, 5, 21, 89, 377, 1597, 6765, 28657, 121393, 514229, 2178309, 9227465, 39088169, 165580141
Value to 32 dp after 28 iterations: 4.23606797749978969640917366873128
 
Lucas sequence for Nickel ratio, where b = 5:
First 15 elements: 1, 1, 6, 31, 161, 836, 4341, 22541, 117046, 607771, 3155901, 16387276, 85092281, 441848681, 2294335686
Value to 32 dp after 25 iterations: 5.19258240356725201562535524577016
 
Lucas sequence for Aluminium ratio, where b = 6:
First 15 elements: 1, 1, 7, 43, 265, 1633, 10063, 62011, 382129, 2354785, 14510839, 89419819, 551029753, 3395598337, 20924619775
Value to 32 dp after 23 iterations: 6.16227766016837933199889354443272
 
Lucas sequence for Iron ratio, where b = 7:
First 15 elements: 1, 1, 8, 57, 407, 2906, 20749, 148149, 1057792, 7552693, 53926643, 385039194, 2749201001, 19629446201, 140155324408
Value to 32 dp after 22 iterations: 7.14005494464025913554865124576352
 
Lucas sequence for Tin ratio, where b = 8:
First 15 elements: 1, 1, 9, 73, 593, 4817, 39129, 317849, 2581921, 20973217, 170367657, 1383914473, 11241683441, 91317382001, 741780739449
Value to 32 dp after 20 iterations: 8.12310562561766054982140985597408
 
Lucas sequence for Lead ratio, where b = 9:
First 15 elements: 1, 1, 10, 91, 829, 7552, 68797, 626725, 5709322, 52010623, 473804929, 4316254984, 39320099785, 358197153049, 3263094477226
Value to 32 dp after 20 iterations: 9.10977222864644365500113714088140
 
Golden ratio, where b = 1:
Value to 256 dp after 615 iterations: 1.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374847540880753868917521266338622235369317931800607667263544333890865959395829056383226613199282902678806752087668925017116962070322210432162695486262963136144
</pre>
 
=={{header|zkl}}==
{{libheader|GMP}} GNU Multiple Precision Arithmetic Library
<langsyntaxhighlight lang="zkl">var [const] BI=Import("zklBigNum"); // libGMP
fcn lucasSeq(b){
Walker.zero().tweak('wrap(xs){
Line 777 ⟶ 2,306:
b,mr = c,mr2;
}
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">metals:="Platinum Golden Silver Bronze Copper Nickel Aluminum Iron Tin Lead";
foreach metal in (metals.split(" ")){ n:=__metalWalker.idx;
println("\nLucas sequence for %s ratio; where b = %d:".fmt(metal,n));
Line 784 ⟶ 2,313:
mr,i := metallicRatio(lucasSeq(n));
println("Approximated value: %s - Reached after ~%d iterations.".fmt(mr,i));
}</langsyntaxhighlight>
{{out}}
<pre style="height:45ex">
Line 827 ⟶ 2,356:
Approximated value: 9.10977222864644365500113714088140 - Reached after ~19 iterations.
</pre>
<langsyntaxhighlight lang="zkl">println("Golden ratio (B==1) to 256 digits:");
mr,i := metallicRatio(lucasSeq(1),256);
println("Approximated value: %s\nReached after ~%d iterations.".fmt(mr,i));</langsyntaxhighlight>
{{out}}
<pre>
1,150

edits