Metallic ratios: Difference between revisions

m
syntax highlighting fixup automation
m (Ruby 3.1 changed "/".)
m (syntax highlighting fixup automation)
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 854:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Formatting
import Base.iterate, Base.IteratorSize, Base.IteratorEltype, Base.Iterators.take
 
Line 890:
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:
=={{header|Kotlin}}==
{{trans|Go}}
<langsyntaxhighlight lang="scala">import java.math.BigDecimal
import java.math.BigInteger
 
Line 991:
println("Golden ration, where b = 1:")
metallic(1, 256)
}</langsyntaxhighlight>
{{out}}
<pre>Lucas sequence for Platinum ratio, where b = 0:
Line 1,037:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[FindMetallicRatio]
FindMetallicRatio[b_, digits_] :=
Module[{n, m, data, acc, old, done = False},
Line 1,068:
out = FindMetallicRatio[1, 256];
out["steps"]
N[out["ratio"][[-1]], 256]</langsyntaxhighlight>
{{out}}
<pre>b=0
Line 1,115:
=={{header|Nim}}==
{{libheader|bignum}}
<langsyntaxhighlight Nimlang="nim">import strformat
import bignum
 
Line 1,170:
 
echo "Golden ratio where b = 1:"
computeRatio(1, 256)</langsyntaxhighlight>
 
{{out}}
Line 1,217:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature qw(say state);
Line 1,255:
}
 
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:
{{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:
<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:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">from itertools import count, islice
from _pydecimal import getcontext, Decimal
 
Line 1,432:
 
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,461:
Note: Arrays, Lists and Sequences are zero indexed in Raku.
 
<syntaxhighlight lang="raku" perl6line>use Rat::Precise;
use Lingua::EN::Numbers;
 
Line 1,493:
 
# 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:
=={{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:
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,649:
=={{header|Ruby}}==
{{works with|Ruby|2.3}}
<langsyntaxhighlight lang="ruby">require('bigdecimal')
require('bigdecimal/util')
 
Line 1,709:
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:
 
=={{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:
say "Approximated value: #{v}"
say "Reached after #{n} iterations"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,810:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Imports BI = System.Numerics.BigInteger
 
Module Module1
Line 1,921:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>Metal B Sq.Rt Iters /---- 32 decimal place value ----\\ Matches Sq.Rt Calc
Line 1,953:
{{libheader|Wren-big}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "/big" for BigInt, BigRat
import "/fmt" for Fmt
 
Line 2,002:
}
System.print("Golden ratio, where b = 1:")
metallic.call(1, 256)</langsyntaxhighlight>
 
{{out}}
Line 2,052:
=={{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:
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:
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:
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>
10,333

edits