Metallic ratios: Difference between revisions

m
→‎{{header|RPL}}: improved comment
m (Ruby 3.1 changed "/".)
m (→‎{{header|RPL}}: improved comment)
 
(7 intermediate revisions by 5 users not shown)
Line 156:
=={{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).
<langsyntaxhighlight lang="csharp">using static System.Math;
using static System.Console;
using BI = System.Numerics.BigInteger;
Line 200:
WriteLine("\nAu to 256 digits:"); WriteLine(t);
WriteLine("Iteration count: {0} Matched Sq.Rt Calc: {1}", k, t == doOne(1, 256)); }
}</langsyntaxhighlight>
{{out}}
<pre style="white-space:pre-wrap;">Metal B Sq.Rt Iters /---- 32 decimal place value ----\ Matches Sq.Rt Calc
Line 232:
{{Works with|C++11}}
{{libheader|Boost}}
<langsyntaxhighlight lang="cpp">#include <boost/multiprecision/cpp_dec_float.hpp>
#include <iostream>
 
Line 280:
 
return 0;
}</langsyntaxhighlight>
{{Out}}
<pre>Lucas sequence for Platinum ratio, where b = 0:
Line 326:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight 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)
Line 334:
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>
</lang>
{{out}}
<pre>
Line 372:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: combinators decimals formatting generalizations io kernel
math prettyprint qw sequences ;
IN: rosetta-code.metallic-ratios
Line 400:
[ "- reached after %d iteration(s)\n\n" printf ]
} spread
] each-index</langsyntaxhighlight>
{{out}}
<pre style="height:45ex">
Line 445:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 499:
fmt.Println("Golden ratio, where b = 1:")
metallic(1, 256)
}</langsyntaxhighlight>
 
{{out}}
Line 549:
=={{header|Groovy}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="groovy">class MetallicRatios {
private static List<String> names = new ArrayList<>()
static {
Line 613:
metallic(1, 256)
}
}</langsyntaxhighlight>
{{out}}
<pre>Lucas sequence for Platinum ratio, where b = 0
Line 662:
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">
<lang J>
258j256":%~/+/+/ .*~^:10 x:*i.2 2
1.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374847540880753868917521266338622235369317931800607667263544333890865959395829056383226613199282902678806752087668925017116962070322210432162695486262963136144
</syntaxhighlight>
</lang>
 
However, the task implies we should use a different form of iteration, so we should probably stick to that here.
 
<syntaxhighlight lang="j">
<lang J>
task=:{{
32 task y
Line 732:
seq=1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
615 iterations: 1.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374847540880753868917521266338622235369317931800607667263544333890865959395829056383226613199282902678806752087668925017116962070322210432162695486262963136144
</syntaxhighlight>
</lang>
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">
import java.math.BigDecimal;
import java.math.BigInteger;
Line 806:
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 851:
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 890 ⟶ 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 938 ⟶ 1,045:
=={{header|Kotlin}}==
{{trans|Go}}
<langsyntaxhighlight lang="scala">import java.math.BigDecimal
import java.math.BigInteger
 
Line 991 ⟶ 1,098:
println("Golden ration, where b = 1:")
metallic(1, 256)
}</langsyntaxhighlight>
{{out}}
<pre>Lucas sequence for Platinum ratio, where b = 0:
Line 1,037 ⟶ 1,144:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[FindMetallicRatio]
FindMetallicRatio[b_, digits_] :=
Module[{n, m, data, acc, old, done = False},
Line 1,068 ⟶ 1,175:
out = FindMetallicRatio[1, 256];
out["steps"]
N[out["ratio"][[-1]], 256]</langsyntaxhighlight>
{{out}}
<pre>b=0
Line 1,115 ⟶ 1,222:
=={{header|Nim}}==
{{libheader|bignum}}
<langsyntaxhighlight Nimlang="nim">import strformat
import bignum
 
Line 1,170 ⟶ 1,277:
 
echo "Golden ratio where b = 1:"
computeRatio(1, 256)</langsyntaxhighlight>
 
{{out}}
Line 1,217 ⟶ 1,324:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature qw(say state);
Line 1,255 ⟶ 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 1,302 ⟶ 1,409:
{{trans|Go}}
{{libheader|Phix/mpfr}}
<!--<langsyntaxhighlight Phixlang="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>
Line 1,355 ⟶ 1,462:
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</langsyntaxhighlight>-->
{{out}}
The final 258 digits includes the "1.", and dp+2 was needed on ratio to exactly match the Go (etc) output.
Line 1,404 ⟶ 1,511:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">from itertools import count, islice
from _pydecimal import getcontext, Decimal
 
Line 1,432 ⟶ 1,539:
 
print(f'\nb = 1 with 256 digits:')
stable(1, 256)</langsyntaxhighlight>
{{out}}
<pre>b = 0: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
Line 1,453 ⟶ 1,560:
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>
 
Line 1,461 ⟶ 1,657:
Note: Arrays, Lists and Sequences are zero indexed in Raku.
 
<syntaxhighlight lang="raku" perl6line>use Rat::Precise;
use Lingua::EN::Numbers;
 
Line 1,493 ⟶ 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 1,542 ⟶ 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 1,574 ⟶ 1,770:
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. */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
 
Line 1,647 ⟶ 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}}
<langsyntaxhighlight lang="ruby">require('bigdecimal')
require('bigdecimal/util')
 
Line 1,709 ⟶ 1,942:
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')])</langsyntaxhighlight>
{{out}}
<pre>Lucas Sequences...
Line 1,742 ⟶ 1,975:
 
=={{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 1,764 ⟶ 1,997:
say "Approximated value: #{v}"
say "Reached after #{n} iterations"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,810 ⟶ 2,043:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Imports BI = System.Numerics.BigInteger
 
Module Module1
Line 1,921 ⟶ 2,154:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>Metal B Sq.Rt Iters /---- 32 decimal place value ----\\ Matches Sq.Rt Calc
Line 1,953 ⟶ 2,186:
{{libheader|Wren-big}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./big" for BigInt, BigRat
import "./fmt" for Fmt
 
var names = ["Platinum", "Golden", "Silver", "Bronze", "Copper","Nickel", "Aluminium", "Iron", "Tin", "Lead"]
Line 2,002 ⟶ 2,235:
}
System.print("Golden ratio, where b = 1:")
metallic.call(1, 256)</langsyntaxhighlight>
 
{{out}}
Line 2,052 ⟶ 2,285:
=={{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 2,073 ⟶ 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 2,080 ⟶ 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 2,123 ⟶ 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