user
Many people have heard of the **Golden ratio**, phi (**φ**). Phi is just one of a series
of related ratios that are referred to as the "**Metallic ratios**".

The **Golden ratio** was discovered and named by ancient civilizations as it was
thought to be the most pure and beautiful (like Gold). The **Silver ratio** was was
also known to the early Greeks, though was not named so until later as a nod to
the **Golden ratio** to which it is closely related. The series has been extended to
encompass all of the related ratios and was given the general name **Metallic ratios** (or *Metallic means*).
*Somewhat incongruously as the original Golden ratio referred to the adjective "golden" rather than the metal "gold".*

**Metallic ratios** are the real roots of the general form equation:

```
         x2 - bx - 1 = 0 

```

where the integer **b** determines which specific one it is.

Using the quadratic equation:

```
         ( -b ± √(b2 - 4ac) ) / 2a = x 

```

Substitute in (from the top equation) **1** for **a**, **-1** for **c**, and recognising that -b is negated we get:

```
         ( b ± √(b2 + 4) ) ) / 2 = x 

```

We only want the real root:

```
         ( b + √(b2 + 4) ) ) / 2 = x 

```

When we set **b** to **1**, we get an irrational number: the **Golden ratio**.

```
         ( 1 + √(12 + 4) ) / 2  =  (1 + √5) / 2 = ~1.618033989... 

```

With **b** set to **2**, we get a different irrational number: the **Silver ratio**.

```
         ( 2 + √(22 + 4) ) / 2  =  (2 + √8) / 2 = ~2.414213562... 

```

When the ratio **b** is **3**, it is commonly referred to as the **Bronze** ratio, **4** and **5**
are sometimes called the **Copper** and **Nickel** ratios, though they aren't as
standard. After that there isn't really any attempt at standardized names. They
are given names here on this page, but consider the names fanciful rather than
canonical.

Note that technically, **b** can be **0** for a "smaller" ratio than the **Golden ratio**.
We will refer to it here as the **Platinum ratio**, though it is kind-of a
degenerate case.

**Metallic ratios** where **b** > **0** are also defined by the irrational continued fractions:

```
         [b;b,b,b,b,b,b,b,b,b,b,b,b,b,b,b,b...] 

```

So, The first ten **Metallic ratios** are:

:   :   :   :   :   :   Metallic ratios

                        | Name | **b** | Equation | Value | Continued fraction | OEIS link |
                        | --- | --- | --- | --- | --- | --- |
                        | Platinum | 0 | (0 + √4) / 2 | 1 | - | - |
                        | Golden | 1 | (1 + √5) / 2 | 1.618033988749895... | [1;1,1,1,1,1,1,1,1,1,1...] | OEIS:A001622 |
                        | Silver | 2 | (2 + √8) / 2 | 2.414213562373095... | [2;2,2,2,2,2,2,2,2,2,2...] | OEIS:A014176 |
                        | Bronze | 3 | (3 + √13) / 2 | 3.302775637731995... | [3;3,3,3,3,3,3,3,3,3,3...] | OEIS:A098316 |
                        | Copper | 4 | (4 + √20) / 2 | 4.23606797749979... | [4;4,4,4,4,4,4,4,4,4,4...] | OEIS:A098317 |
                        | Nickel | 5 | (5 + √29) / 2 | 5.192582403567252... | [5;5,5,5,5,5,5,5,5,5,5...] | OEIS:A098318 |
                        | Aluminum | 6 | (6 + √40) / 2 | 6.16227766016838... | [6;6,6,6,6,6,6,6,6,6,6...] | OEIS:A176398 |
                        | Iron | 7 | (7 + √53) / 2 | 7.140054944640259... | [7;7,7,7,7,7,7,7,7,7,7...] | OEIS:A176439 |
                        | Tin | 8 | (8 + √68) / 2 | 8.123105625617661... | [8;8,8,8,8,8,8,8,8,8,8...] | OEIS:A176458 |
                        | Lead | 9 | (9 + √85) / 2 | 9.109772228646444... | [9;9,9,9,9,9,9,9,9,9,9...] | OEIS:A176522 |

---

There are other ways to find the **Metallic ratios**; one, (the focus of this task)
is through **successive approximations of Lucas sequences**.

A traditional **Lucas sequence** is of the form:

```
   xn = P * xn-1 - Q * xn-2

```

and starts with the first 2 values **0, 1**.

For our purposes in this task, to find the metallic ratios we'll use the form:

```
   xn = b * xn-1 + xn-2

```

( **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.*

At any rate, when **b = 1** we get:

```
   xn = xn-1 + xn-2

```

```
   1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144...

```

more commonly known as the Fibonacci sequence.

When **b = 2**:

```
   xn = 2 * xn-1 + xn-2

```

```
   1, 1, 3, 7, 17, 41, 99, 239, 577, 1393...

```

And so on.

To find the ratio by successive approximations, divide the (**n+1**)th term by the
**n**th. As **n** grows larger, the ratio will approach the **b** metallic ratio.

For **b = 1** (Fibonacci sequence):

```
   1/1   = 1
   2/1   = 2
   3/2   = 1.5
   5/3   = 1.666667
   8/5   = 1.6
   13/8  = 1.625
   21/13 = 1.615385
   34/21 = 1.619048
   55/34 = 1.617647
   89/55 = 1.618182
   etc.

```

It converges, but pretty slowly. In fact, the **Golden ratio** has the slowest
possible convergence for any irrational number.

Task

For each of the first **10 Metallic ratios**; **b** = **0** through **9**:

* Generate the corresponding "Lucas" sequence.
* Show here, on this page, at least the first **15** elements of the "Lucas" sequence.
* Using successive approximations, calculate the value of the ratio accurate to **32** decimal places.
* Show the **value** of the **approximation** at the required accuracy.
* Show the **value** of **n** when the approximation reaches the required accuracy (How many iterations did it take?).

Optional, stretch goal - Show the **value** and number of iterations **n**, to approximate the **Golden ratio** to **256** decimal places.

You may assume that the approximation has been reached when the next iteration does not cause the value (to the desired places) to change.

See also

* Wikipedia: Metallic mean
* Wikipedia: Lucas sequence
assistant
(formerly Perl 6)

**Works with**: Rakudo version 2019.07.1

Note: Arrays, Lists and Sequences are zero indexed in Raku.

```
use Rat::Precise;
use Lingua::EN::Numbers;

sub lucas ($b) { 1, 1, * + $b * * … * }

sub metallic ($seq, $places = 32) {
    my $n = 0;
    my $last = 0;
    loop {
        my $approx = FatRat.new($seq[$n + 1], $seq[$n]);
        my $this = $approx.precise($places, :z);
        last if $this eq $last;
        $last = $this;
        $n++;
    }
    $last, $n
}

sub display ($value, $n) {
    "Approximated value:", $value, "Reached after {$n} iterations: " ~
    "{ordinal-digit $n}/{ordinal-digit $n - 1} element."
}

for <Platinum Golden Silver Bronze Copper Nickel Aluminum Iron Tin Lead>.kv
  -> \b, $name {
    my $lucas = lucas b;
    print "\nLucas sequence for $name ratio; where b = {b}:\nFirst 15 elements: ";
    say join ', ', $lucas[^15];
    say join ' ', display |metallic($lucas);
}

# Stretch goal
say join "\n", "\nGolden ratio to 256 decimal places:", display |metallic lucas(1), 256;

```

Output:

```
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
Approximated value: 1.00000000000000000000000000000000 Reached after 1 iterations: 1st/0th element.

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
Approximated value: 1.61803398874989484820458683436564 Reached after 78 iterations: 78th/77th element.

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
Approximated value: 2.41421356237309504880168872420970 Reached after 44 iterations: 44th/43rd element.

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
Approximated value: 3.30277563773199464655961063373525 Reached after 34 iterations: 34th/33rd element.

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
Approximated value: 4.23606797749978969640917366873128 Reached after 28 iterations: 28th/27th element.

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
Approximated value: 5.19258240356725201562535524577016 Reached after 25 iterations: 25th/24th element.

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
Approximated value: 6.16227766016837933199889354443272 Reached after 23 iterations: 23rd/22nd element.

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
Approximated value: 7.14005494464025913554865124576352 Reached after 22 iterations: 22nd/21st element.

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
Approximated value: 8.12310562561766054982140985597408 Reached after 20 iterations: 20th/19th element.

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
Approximated value: 9.10977222864644365500113714088140 Reached after 20 iterations: 20th/19th element.

Golden ratio to 256 decimal places:
Approximated value:
1.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374847540880753868917521266338622235369317931800607667263544333890865959395829056383226613199282902678806752087668925017116962070322210432162695486262963136144
Reached after 615 iterations: 615th/614th element.
```