Metallic ratios
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:
x² - bx - 1 = 0
where the integer b determines which specific one it is.
Using the quadratic equation:
( -b ± √(b² - 4ac) ) / 2a = x
Substitute in (from the top equation) 1 for a, -1 for c, and recognising that -b is negated we get:
( b ± √(b² + 4) ) ) / 2 = x
We only want the real root:
( b + √(b² + 4) ) ) / 2 = x
When we set b to 1, we get an irrational number: the Golden ratio.
( 1 + √(1² + 4) ) / 2 = (1 + √5) / 2 = ~1.618033989...
With b set to 2, we get a different irrational number: the Silver ratio.
( 2 + √(2² + 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 10 Metallic ratios are:
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 Lucas sequence has the form xₙ = b * xₙ₋₁ + c * xₙ₋₂. The metallic ratios use Lucas sequences where c = 1 and where the first two terms are always 1, 1.
So for the Lucas sequence when b = 1:
xₙ = 1 * xₙ₋₁ + xₙ₋₂.
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144...
more commonly known as the Fibonacci sequence.
When b = 2:
xₙ = 2 * xₙ₋₁ + xₙ₋₂.
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
nth. 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
Factor
<lang factor>USING: combinators decimals formatting generalizations io kernel math prettyprint qw sequences ; IN: rosetta-code.metallic-ratios
- lucas ( n a b -- n a' b' ) tuck reach * + ;
- lucas. ( n -- )
1 pprint bl 1 1 14 [ lucas over pprint bl ] times 3drop nl ;
- approx ( a b -- d ) swap [ 0 <decimal> ] bi@ 32 D/ ;
- approximate ( n -- value iter )
-1 swap 1 1 0 1 [ 2dup = ] [ [ 1 + ] 5 ndip [ lucas 2dup approx ] 2dip drop ] until 4nip decimal>ratio swap ;
qw{
Platinum Golden Silver Bronze Copper Nickel Aluminum Iron Tin Lead
} [
dup dup approximate { [ "Lucas sequence for %s ratio " printf ] [ "where b = %d:\n" printf ] [ "First 15 elements: " write lucas. ] [ "Approximated value: %.32f " printf ] [ "- reached after %d iteration(s)\n\n" printf ] } spread
] each-index</lang>
- 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 iteration(s) 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.61803398874989484820458683436563 - reached after 78 iteration(s) 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.41421356237309504880168872420969 - reached after 44 iteration(s) 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.30277563773199464655961063373524 - reached after 32 iteration(s) 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.23606797749978969640917366873127 - reached after 27 iteration(s) 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 24 iteration(s) 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.16227766016837933199889354443271 - reached after 22 iteration(s) 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.14005494464025913554865124576351 - reached after 20 iteration(s) 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.12310562561766054982140985597407 - reached after 19 iteration(s) 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.10977222864644365500113714088139 - reached after 18 iteration(s)
Go
<lang go>package main
import (
"fmt" "math/big"
)
var names = [10]string{"Platinum", "Golden", "Silver", "Bronze", "Copper",
"Nickel", "Aluminium", "Iron", "Tin", "Lead"}
func lucas(b int64) {
fmt.Printf("Lucas sequence for %s ratio, where b = %d:\n", names[b], b) fmt.Print("First 15 elements: ") var x0, x1 int64 = 1, 1 fmt.Printf("%d, %d", x0, x1) for i := 1; i <= 13; i++ { x2 := b*x1 + x0 fmt.Printf(", %d", x2) x0, x1 = x1, x2 } fmt.Println()
}
func metallic(b int64, dp int) {
x0, x1, x2, bb := big.NewInt(1), big.NewInt(1), big.NewInt(0), big.NewInt(b) ratio := big.NewRat(1, 1) iters := 0 prev := ratio.FloatString(dp) for { iters++ x2.Mul(bb, x1) x2.Add(x2, x0) this := ratio.SetFrac(x2, x1).FloatString(dp) if prev == this { plural := "s" if iters == 1 { plural = " " } fmt.Printf("Value to %d dp after %2d iteration%s: %s\n\n", dp, iters, plural, this) return } prev = this x0.Set(x1) x1.Set(x2) }
}
func main() {
for b := int64(0); b < 10; b++ { lucas(b) metallic(b, 32) } fmt.Println("Golden ratio, where b = 1:") metallic(1, 256)
}</lang>
- 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 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.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374847540880753868917521266338622235369317931800607667263544333890865959395829056383226613199282902678806752087668925017116962070322210432162695486262963136144
Perl 6
Note: Arrays, Lists and Sequences are zero indexed in Perl 6.
<lang perl6>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;</lang>
- 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.
REXX
For this task, the elements of the Lucas sequence are zero based. <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.*/ if bLO== | bLO=="," then bLO= 0 /* " " " " " " */ if bHI== | bHI=="," then bHI= 9 /* " " " " " " */ if digs== | digs=="," then digs= 32 /* " " " " " " */ numeric digits digs + length(.) /*specify number of decimal digs to use*/ metals= 'platinum golden silver bronze copper nickel aluminum iron tin lead' 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*/ end /*k*/
do m=bLO to bHI; @.= 1; $= 1 1 /*compute the sequence numbers & ratios*/ r=. /*the ratio (so far). */ do #=2 until r=old; old= r /*compute sequence numbers & the ratio.*/ #_1= #-1; #_2= #-2 /*use variables for previous numbers. */ @.#= m * @.#_1 + @.#_2 /*calculate a number i the sequence. */ if #<n then $= $ @.# /*build a sequence list of N numbers.*/ r= @.# / @.#_1 /*calculate ratio of the last 2 numbers*/ end /*#*/
if words($)<n then $= subword($ copies('1 ', n), 1, n) /*extend list if too short*/ L= max(108, length($) ) /*ensure width of title. */ say center(' Lucas sequence for the' !.m "ratio, where B is " m' ', L, "═") if n>0 then do; say 'the first ' n " elements are:"; say $ end /*if N is positive, then show N nums.*/ say approx #-1 ' iterations with ' digs " decimal digits past the decimal point:" say format(r,,digs); say /*display the ration plus a blank line.*/ end /*m*/ /*stick a fork in it, we're all done. */</lang>
- output when using the default inputs:
═════════════════════════ Lucas sequence for the platinum ratio, where B is 0 ══════════════════════════ the first 15 elements are: 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 the approximate value reached after 2 iterations with 32 decimal digits past the decimal point: 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 ══════════════════════════ Lucas sequence for the silver ratio, where B is 2 ═══════════════════════════ 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 ══════════════════════════ Lucas sequence for the bronze ratio, where B is 3 ═══════════════════════════ 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 ══════════════════════════ Lucas sequence for the copper ratio, where B is 4 ═══════════════════════════ 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 ══════════════════════════ Lucas sequence for the nickel ratio, where B is 5 ═══════════════════════════ 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 ═════════════════════════ Lucas sequence for the aluminum ratio, where B is 6 ══════════════════════════ 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 ═══════════════════════════ Lucas sequence for the iron ratio, where B is 7 ════════════════════════════ 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 ════════════════════════════ Lucas sequence for the tin ratio, where B is 8 ════════════════════════════ 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 ═══════════════════════════ Lucas sequence for the lead ratio, where B is 9 ════════════════════════════ 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
{{out|output|text= when using the inputs of: 0 1 1 256
(Shown at three-quarter size.)
══════════════════════════ Lucas sequence for the golden ratio, where B is 1 ═══════════════════════════ the approximate value reached after 615 iterations with 256 decimal digits past the decimal point: 1.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374847540880753868917521266338622235369317931800607667263544333890865959395829056383226613199282902678806752087668925017116962070322210432162695486262963136144
- output when using the inputs of: 0 1 1 10000
(Shown at three-quarter size.)
══════════════════════════ Lucas sequence for the golden ratio, where B is 1 ═══════════════════════════ the approximate value reached after 23927 iterations with 10000 decimal digits past the decimal point: 1.61803398874989484820458683436563811772030917980576286213544862270526046281890244970720720418939113748475408807538689175212663386222353693179318006076672635443338908659593958290563832266131992829026788067520876689250171169620703222104321626954862629631361443814975870122034080588795445474924618569536486444924104432077134494704956584678850987433944221254487706647809158846074998871240076521705751797 8834166256249407589069704000281210427621771117778053153171410117046665991466979873176135600670874807101317952368942752194843530567830022878569978297783478458782289110976250030269615617002504643382437764861028383126833037242926752631165339247316711121158818638513316203840052221657912866752946549068113171599343235973494985090409476213222981017261070596116456299098162905552085247903524060201727997471 7534277759277862561943208275051312181562855122248093947123414517022373580577278616008688382952304592647878017889921990270776903895321968198615143780314997411069260886742962267575605231727775203536139362107673893764556060605921658946675955190040055590895022953094231248235521221241544400647034056573479766397239494994658457887303962309037503399385621024236902513868041457799569812244574717803417312645 3220416397232134044449487302315417676893752103068737880344170093954409627955898678723209512426893557309704509595684401755519881921802064052905518934947592600734852282101088194644544222318891319294689622002301443770269923007803085261180754519288770502109684249362713592518760777884665836150238913493333122310533923213624319263728910670503399282265263556209029798642472759772565508615487543574826471814 1451270006023890162077732244994353088999095016803281121943204819643876758633147985719113978153978074761507722117508269458639320456520989698555678141069683728840587461033781054443909436835835813811311689938555769754841491445341509129540700501947754861630754226417293946803673198058618339183285991303960720144559504497792120761247856459161608370594987860069701894098864007644361709334172709191433650137 1576601148038143062623805143211734815100559013456101180079050638142152709308588092875703450507808145458819906336129827981411745339273120809289727922213298064294687824274874017450554067787570832373109759151177629784432847479081765180977872684161176325038612112914368343767023503711163307258698832587103363222381098090121101989917684149175123313401527338438372345009347860497929459915822012581045982309 2552872124137043614910205471855496118087642657651106054588147560443178479858453973128630162544876114852021706440411166076695059775783257039511087823082710647893902111569103927683845386333321565829659773103436032322545743637204124406408882673758433953679593123221343732099574988946995656473600729599983912881031974263125179714143201231127955189477817269141589117799195648125580018455065632952859859100 0908621802977563789259991649946428193022293552346674759326951654214021091363018194722707890122087287361707348649998156255472811373479871656952748900814438405327483781378246691744422963491470815700735254570708977267546934382261954686153312095335792380146092735102101191902183606750973089575289577468142295433943854931553396303807291691758461014609950550648036793041472365720398600735507609023173125016 1320484358364817704848181099160244252327167219018933459637860878752870173935930301335901123710239171265904702634940283076687674363865132710628032317406931733448234356453185058135310854973335075996677871244905836367541328908624063245639535721252426117027802865604323494283730172557440583727826799603173936401328762770124367983114464369476705312724924104716700138247831286565064934341803900410178053395 0587724586655755229391582397084177298337282311525692609299594224000056062667867435792397245408481765197343626526894488855272027477874733598353672776140759171205132693448375299164998093602461784426757277679001919190703805220461232482391326104327191684512306023627893545432461769975753689041763650254785138246314658336383376023577899267298863216185839590363998183845827644912459809370430555596137973432 6134830494949686810895356963482817812886253646084203394653819441945714266682371839491832370908574850266568039897440662105360306400260817112665995419936873160945722888109207788227720363668448153256172841176909792666655223846883113718529919216319052015686312228207155998764684235520592853717578076560503677313097519122397388722468258057159744574048429878073522159842667662578077062019430400542550158312 5030175340941171910192989038447250332988024501436796844169479595453045910313811621870456799786636617460595700034459701135251813460065655352034788811741499412748264152135567763940390710387088182338068033500380468001748082205910968442026446402187705340100318028816644153091393948156403192822785482414510503188825189970074862287942155895742820216657062188090578088050324676991297287210387073697406435667 4589202586565739785608595665341070359978320446336346485489497663885351045527298242290699848853696828046459745762651434359050938321243743333870516657149005907105670248879858043718151261004403814880407252440616429022478227152724112085065788838712493635106806365166743222327767755797399270376231914704732395512060705503992088442603708790843334261838413597078164829553714321961189503797714630007555975379 5703552271449319132172556440128309180504500899218705121186069335731538959350790300736727023314165320423401553741442687154055116479611433230248544040940691145613987302603951828168034482525432673857590056043202453727192912486458133344169852993913574786989579864394980230471169671573622839120181273129165899527599192203183723568272793856373312654799859124632750300605925674549794350881192950568549325935 5318729141801136412187470752628106869830135760524719445593219553596104528303148839117693011965858343144248948985655842508341094295027719758335224429125736493807541711373924376014350682987849327129975122868819604983577515877178041069713196675347719479226365190163397712847390793361111914089983056033610609871717830554354035608952929081846414371392943781356048203894791257450770755751030024207266290018 0904229342494259060666141332287226980690145994511995478016399151412612525728280664331261657469388195106442167387180001100421848302580916543383749236411838885646851431500637319042951481469424314608952547072037405566913069220990804819452975110650464281054177552590951871318883591476599604131796020941530858553323877253802327276329773721431279682167162344211832018028814127474431688472184593927814354740 9999907223320305926297661123832798331698825393126200650370288447828666940447307947104761255865837529862362509998232335971550723383833244081525778193364262630433026589581708004512788731159355877472172564947000516366725771539209840950327451121536873009121996295227659131637093968607271342692623154753304379933165811073696431421719794340563915512108108136262688856974806806011691894175027229874158699179 1453499462444194012197858601373660828690722365147713912687420966513787562059185432888834174292090156313328319357562208971376563097850156315498245644586542479293572282875060848145335135218172958793299117100324762220521946451053624505129884308713444395072442673514628617991832336459836963763272257569159723954383052086647474238151107927349483695239647926899369832491799950278950006045966131346336302494 9951480805329017902975182515875049007435187983511836032722772601717404535571658855578297291061958193517105548257930709100576358699019297217995168731175563144485648100220014254540554292734588371160209947945720823780436871894480563689182580244499631878342027491015335791072733625328906933474123802222011626277119308544850295419132004009998655666517756640953656197897818380451030356510131589458902871861 0869058939471368014845700183664956472032943343742989464274125514359058434840919548701523614031739139036164401984550510491211697920012019996050699496640303508636929039410070194505320162348727632327324494396304808905542513797233147518520709102506368598167953048181007394245317002388047598343234504142584314063612721096022824233782280902797659607771084939151748873168777135223900911711735091860065462009 9024975852779254278165970383495058010626155333691093784659771052975022317307412177834418941184596586102980187787427445638669661277245038458605264151030408982577775447411533207640758816775149755380471162966777100587664615954967769270549623939857092550702740699781408431249653630718665337180605874224259816530705257383454157705429216299811491750861131176577317209561565647869547448927132060806354577946 2414531066983742113798168963823533304477883169339728728918103664083269856988254438516675862289930696434684897514840879039647604203610206021717394470263487633654393195229077383616738981178124248365578105034169451563626043003665743108476654877780128577923645418522447236171374229255841593135612866371670328072171553392646325730673063910854108868085742838588280602303341408550390973538726134511962926415 9952127893113544314601527309025538271043259662267439037455636122861390783194335705900381487008986613153981958574423304419708566967222931427307413848827889755888607997387044702031668348569419909654802982493198176579268298556297230106827772351627407838074318778273182119196952800516087915721288263379682312725628700015001829297577299935790949196407634428615757135444278983830404547027101945800425820212 0234458063034503365814721854920367998997293535391968121331951653797453991114942444518303385884129040181781882137600665928494136775431745160540938711036871521164040582193447120448277596054169486453987832626954801391501903899593130670318661670663719640256928671388714663118919268568269199527645799771827875946096161721886810945465157886912241060981419726861925547878992631535947292282508054251690681401 0781796021885330762305563816316401922454503257656739259976517530801427160714308718862859836037465057134204670083432754230277047793311183666903232885306873879907135900740304907459889513647687608678443238248218930617570319563803230819719363567274196438726258706154330729637038127515170406005057594882723856345156390526577104264594760405569509598408889037620799566388017861855915944111725092313279771138 03
zkl
GNU Multiple Precision Arithmetic Library
<lang zkl>var [const] BI=Import("zklBigNum"); // libGMP fcn lucasSeq(b){
Walker.zero().tweak('wrap(xs){ xm2,xm1 := xs; // x[n-2], x[n-1] xn:=xm1*b + xm2; xs.append(xn).del(0); xn }.fp(L(BI(1),BI(1)))).push(1,1) // xn can get big so use BigInts
} fcn metallicRatio(lucasSeq){
const bige="1e33",E=10; // x[n-1]*bige*b / x[n-2] to get our digits from Ints a,b,mr := lucasSeq.next(), lucasSeq.next(), BI(bige).mul(b).div(a); do(1000){ // limit iterations c,m2 := lucasSeq.next(), BI(bige).mul(c).div(b); if((mr - m2).abs()<E) return(mr.div(E),lucasSeq.idx); // idx ignores push(), ie first 2 terms a,b,mr = b,c,m2; }
}</lang> <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)); println("First 15 elements: ",lucasSeq(n).walk(15).concat(" ")); mr,i := metallicRatio(lucasSeq(n)); mr = mr.toString(); println("Approximated value: %s.%s - Reached after ~%d iterations." .fmt(mr[0],mr.del(0),i));
}</lang>
- 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 ~0 iterations. 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 ~77 iterations. 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 ~43 iterations. 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.30277563773199464655961063373524 - Reached after ~32 iterations. 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.23606797749978969640917366873127 - Reached after ~27 iterations. 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 ~24 iterations. 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.16227766016837933199889354443271 - Reached after ~22 iterations. 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.14005494464025913554865124576351 - Reached after ~20 iterations. 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.12310562561766054982140985597407 - Reached after ~19 iterations. 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.10977222864644365500113714088139 - Reached after ~18 iterations.