Metallic ratios: Difference between revisions

Content added Content deleted
m (Ruby 3.1 changed "/".)
m (syntax highlighting fixup automation)
Line 156: Line 156:
=={{header|C#|csharp}}==
=={{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).
{{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).
<lang csharp>using static System.Math;
<syntaxhighlight lang="csharp">using static System.Math;
using static System.Console;
using static System.Console;
using BI = System.Numerics.BigInteger;
using BI = System.Numerics.BigInteger;
Line 200: Line 200:
WriteLine("\nAu to 256 digits:"); WriteLine(t);
WriteLine("\nAu to 256 digits:"); WriteLine(t);
WriteLine("Iteration count: {0} Matched Sq.Rt Calc: {1}", k, t == doOne(1, 256)); }
WriteLine("Iteration count: {0} Matched Sq.Rt Calc: {1}", k, t == doOne(1, 256)); }
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre style="white-space:pre-wrap;">Metal B Sq.Rt Iters /---- 32 decimal place value ----\ Matches Sq.Rt Calc
<pre style="white-space:pre-wrap;">Metal B Sq.Rt Iters /---- 32 decimal place value ----\ Matches Sq.Rt Calc
Line 232: Line 232:
{{Works with|C++11}}
{{Works with|C++11}}
{{libheader|Boost}}
{{libheader|Boost}}
<lang cpp>#include <boost/multiprecision/cpp_dec_float.hpp>
<syntaxhighlight lang="cpp">#include <boost/multiprecision/cpp_dec_float.hpp>
#include <iostream>
#include <iostream>


Line 280: Line 280:


return 0;
return 0;
}</lang>
}</syntaxhighlight>
{{Out}}
{{Out}}
<pre>Lucas sequence for Platinum ratio, where b = 0:
<pre>Lucas sequence for Platinum ratio, where b = 0:
Line 326: Line 326:


=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
<lang fsharp>
<syntaxhighlight lang="fsharp">
// Metallic Ration. Nigel Galloway: September 16th., 2020
// 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 rec fN i g (e,l)=match i with 0->g |_->fN (i-1) (int(l/e)::g) (e,(l%e)*10I)
Line 334: 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 ""
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
[0..9]|>Seq.iter(fun n->mR n 32; printfn ""); mR 1 256
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 372: Line 372:


=={{header|Factor}}==
=={{header|Factor}}==
<lang factor>USING: combinators decimals formatting generalizations io kernel
<syntaxhighlight lang="factor">USING: combinators decimals formatting generalizations io kernel
math prettyprint qw sequences ;
math prettyprint qw sequences ;
IN: rosetta-code.metallic-ratios
IN: rosetta-code.metallic-ratios
Line 400: Line 400:
[ "- reached after %d iteration(s)\n\n" printf ]
[ "- reached after %d iteration(s)\n\n" printf ]
} spread
} spread
] each-index</lang>
] each-index</syntaxhighlight>
{{out}}
{{out}}
<pre style="height:45ex">
<pre style="height:45ex">
Line 445: Line 445:


=={{header|Go}}==
=={{header|Go}}==
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 499: Line 499:
fmt.Println("Golden ratio, where b = 1:")
fmt.Println("Golden ratio, where b = 1:")
metallic(1, 256)
metallic(1, 256)
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 549: Line 549:
=={{header|Groovy}}==
=={{header|Groovy}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang groovy>class MetallicRatios {
<syntaxhighlight lang="groovy">class MetallicRatios {
private static List<String> names = new ArrayList<>()
private static List<String> names = new ArrayList<>()
static {
static {
Line 613: Line 613:
metallic(1, 256)
metallic(1, 256)
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Lucas sequence for Platinum ratio, where b = 0
<pre>Lucas sequence for Platinum ratio, where b = 0
Line 662: 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:
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
258j256":%~/+/+/ .*~^:10 x:*i.2 2
1.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374847540880753868917521266338622235369317931800607667263544333890865959395829056383226613199282902678806752087668925017116962070322210432162695486262963136144
1.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374847540880753868917521266338622235369317931800607667263544333890865959395829056383226613199282902678806752087668925017116962070322210432162695486262963136144
</syntaxhighlight>
</lang>


However, the task implies we should use a different form of iteration, so we should probably stick to that here.
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=:{{
task=:{{
32 task y
32 task y
Line 732: Line 732:
seq=1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
seq=1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597
615 iterations: 1.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374847540880753868917521266338622235369317931800607667263544333890865959395829056383226613199282902678806752087668925017116962070322210432162695486262963136144
615 iterations: 1.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374847540880753868917521266338622235369317931800607667263544333890865959395829056383226613199282902678806752087668925017116962070322210432162695486262963136144
</syntaxhighlight>
</lang>


=={{header|Java}}==
=={{header|Java}}==
<lang java>
<syntaxhighlight lang="java">
import java.math.BigDecimal;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.BigInteger;
Line 806: Line 806:
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 854: Line 854:


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>using Formatting
<syntaxhighlight lang="julia">using Formatting
import Base.iterate, Base.IteratorSize, Base.IteratorEltype, Base.Iterators.take
import Base.iterate, Base.IteratorSize, Base.IteratorEltype, Base.Iterators.take


Line 890: Line 890:
println("Golden ratio to 256 decimal places:")
println("Golden ratio to 256 decimal places:")
metallic(1, 256)
metallic(1, 256)
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
The first 15 elements of the Lucas sequence named Platinum and b of 0 are:
The first 15 elements of the Lucas sequence named Platinum and b of 0 are:
Line 938: Line 938:
=={{header|Kotlin}}==
=={{header|Kotlin}}==
{{trans|Go}}
{{trans|Go}}
<lang scala>import java.math.BigDecimal
<syntaxhighlight lang="scala">import java.math.BigDecimal
import java.math.BigInteger
import java.math.BigInteger


Line 991: Line 991:
println("Golden ration, where b = 1:")
println("Golden ration, where b = 1:")
metallic(1, 256)
metallic(1, 256)
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Lucas sequence for Platinum ratio, where b = 0:
<pre>Lucas sequence for Platinum ratio, where b = 0:
Line 1,037: Line 1,037:


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>ClearAll[FindMetallicRatio]
<syntaxhighlight lang="mathematica">ClearAll[FindMetallicRatio]
FindMetallicRatio[b_, digits_] :=
FindMetallicRatio[b_, digits_] :=
Module[{n, m, data, acc, old, done = False},
Module[{n, m, data, acc, old, done = False},
Line 1,068: Line 1,068:
out = FindMetallicRatio[1, 256];
out = FindMetallicRatio[1, 256];
out["steps"]
out["steps"]
N[out["ratio"][[-1]], 256]</lang>
N[out["ratio"][[-1]], 256]</syntaxhighlight>
{{out}}
{{out}}
<pre>b=0
<pre>b=0
Line 1,115: Line 1,115:
=={{header|Nim}}==
=={{header|Nim}}==
{{libheader|bignum}}
{{libheader|bignum}}
<lang Nim>import strformat
<syntaxhighlight lang="nim">import strformat
import bignum
import bignum


Line 1,170: Line 1,170:


echo "Golden ratio where b = 1:"
echo "Golden ratio where b = 1:"
computeRatio(1, 256)</lang>
computeRatio(1, 256)</syntaxhighlight>


{{out}}
{{out}}
Line 1,217: Line 1,217:


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
use feature qw(say state);
use feature qw(say state);
Line 1,255: Line 1,255:
}
}


printf "\nGolden ratio to 256 decimal places %s reached after %d iterations", metallic(gen_lucas(1),256);</lang>
printf "\nGolden ratio to 256 decimal places %s reached after %d iterations", metallic(gen_lucas(1),256);</syntaxhighlight>
{{out}}
{{out}}
<pre>'Lucas' sequence for Platinum ratio, where b = 0:
<pre>'Lucas' sequence for Platinum ratio, where b = 0:
Line 1,302: Line 1,302:
{{trans|Go}}
{{trans|Go}}
{{libheader|Phix/mpfr}}
{{libheader|Phix/mpfr}}
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 1,355: Line 1,355:
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
The final 258 digits includes the "1.", and dp+2 was needed on ratio to exactly match the Go (etc) output.
The final 258 digits includes the "1.", and dp+2 was needed on ratio to exactly match the Go (etc) output.
Line 1,404: Line 1,404:


=={{header|Python}}==
=={{header|Python}}==
<lang python>from itertools import count, islice
<syntaxhighlight lang="python">from itertools import count, islice
from _pydecimal import getcontext, Decimal
from _pydecimal import getcontext, Decimal


Line 1,432: Line 1,432:


print(f'\nb = 1 with 256 digits:')
print(f'\nb = 1 with 256 digits:')
stable(1, 256)</lang>
stable(1, 256)</syntaxhighlight>
{{out}}
{{out}}
<pre>b = 0: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
<pre>b = 0: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
Line 1,461: Line 1,461:
Note: Arrays, Lists and Sequences are zero indexed in Raku.
Note: Arrays, Lists and Sequences are zero indexed in Raku.


<lang perl6>use Rat::Precise;
<syntaxhighlight lang="raku" line>use Rat::Precise;
use Lingua::EN::Numbers;
use Lingua::EN::Numbers;


Line 1,493: Line 1,493:


# Stretch goal
# Stretch goal
say join "\n", "\nGolden ratio to 256 decimal places:", display |metallic lucas(1), 256;</lang>
say join "\n", "\nGolden ratio to 256 decimal places:", display |metallic lucas(1), 256;</syntaxhighlight>
{{out}}
{{out}}
<pre>Lucas sequence for Platinum ratio; where b = 0:
<pre>Lucas sequence for Platinum ratio; where b = 0:
Line 1,542: Line 1,542:
=={{header|REXX}}==
=={{header|REXX}}==
For this task, &nbsp; the elements of the Lucas sequence are zero based.
For this task, &nbsp; 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. */
<syntaxhighlight 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*/
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 n=='' | n=="," then n= 15 /*Not specified? Then use the default.*/
Line 1,574: Line 1,574:
say 'the' @approx "value reached after" #-1 " iterations with " digs @DecDigs
say 'the' @approx "value reached after" #-1 " iterations with " digs @DecDigs
say r; say /*display the ration plus a blank line.*/
say r; say /*display the ration plus a blank line.*/
end /*m*/ /*stick a fork in it, we're all done. */</lang>
end /*m*/ /*stick a fork in it, we're all done. */</syntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
{{out|output|text=&nbsp; when using the default inputs:}}


Line 1,649: Line 1,649:
=={{header|Ruby}}==
=={{header|Ruby}}==
{{works with|Ruby|2.3}}
{{works with|Ruby|2.3}}
<lang ruby>require('bigdecimal')
<syntaxhighlight lang="ruby">require('bigdecimal')
require('bigdecimal/util')
require('bigdecimal/util')


Line 1,709: Line 1,709:
puts('%-9s %1s %3s %s' % ['name', 'b', 'n', 'ratio'])
puts('%-9s %1s %3s %s' % ['name', 'b', 'n', 'ratio'])
gold_rn = metallic_ratio(1, 256)
gold_rn = metallic_ratio(1, 256)
puts('%-9s %1d %3d %s' % [NAMES[1], 1, gold_rn.terms, gold_rn.ratio.to_s('F')])</lang>
puts('%-9s %1d %3d %s' % [NAMES[1], 1, gold_rn.terms, gold_rn.ratio.to_s('F')])</syntaxhighlight>
{{out}}
{{out}}
<pre>Lucas Sequences...
<pre>Lucas Sequences...
Line 1,742: Line 1,742:


=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>func seqRatio(f, places = 32) {
<syntaxhighlight lang="ruby">func seqRatio(f, places = 32) {
1..Inf -> reduce {|t,n|
1..Inf -> reduce {|t,n|
var r = (f(n+1)/f(n)).round(-places)
var r = (f(n+1)/f(n)).round(-places)
Line 1,764: Line 1,764:
say "Approximated value: #{v}"
say "Approximated value: #{v}"
say "Reached after #{n} iterations"
say "Reached after #{n} iterations"
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,810: Line 1,810:
=={{header|Visual Basic .NET}}==
=={{header|Visual Basic .NET}}==
{{trans|C#}}
{{trans|C#}}
<lang vbnet>Imports BI = System.Numerics.BigInteger
<syntaxhighlight lang="vbnet">Imports BI = System.Numerics.BigInteger


Module Module1
Module Module1
Line 1,921: Line 1,921:
End Sub
End Sub


End Module</lang>
End Module</syntaxhighlight>
{{out}}
{{out}}
<pre>Metal B Sq.Rt Iters /---- 32 decimal place value ----\\ Matches Sq.Rt Calc
<pre>Metal B Sq.Rt Iters /---- 32 decimal place value ----\\ Matches Sq.Rt Calc
Line 1,953: Line 1,953:
{{libheader|Wren-big}}
{{libheader|Wren-big}}
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
<lang ecmascript>import "/big" for BigInt, BigRat
<syntaxhighlight lang="ecmascript">import "/big" for BigInt, BigRat
import "/fmt" for Fmt
import "/fmt" for Fmt


Line 2,002: Line 2,002:
}
}
System.print("Golden ratio, where b = 1:")
System.print("Golden ratio, where b = 1:")
metallic.call(1, 256)</lang>
metallic.call(1, 256)</syntaxhighlight>


{{out}}
{{out}}
Line 2,052: Line 2,052:
=={{header|zkl}}==
=={{header|zkl}}==
{{libheader|GMP}} GNU Multiple Precision Arithmetic Library
{{libheader|GMP}} GNU Multiple Precision Arithmetic Library
<lang zkl>var [const] BI=Import("zklBigNum"); // libGMP
<syntaxhighlight lang="zkl">var [const] BI=Import("zklBigNum"); // libGMP
fcn lucasSeq(b){
fcn lucasSeq(b){
Walker.zero().tweak('wrap(xs){
Walker.zero().tweak('wrap(xs){
Line 2,073: Line 2,073:
b,mr = c,mr2;
b,mr = c,mr2;
}
}
}</lang>
}</syntaxhighlight>
<lang zkl>metals:="Platinum Golden Silver Bronze Copper Nickel Aluminum Iron Tin Lead";
<syntaxhighlight lang="zkl">metals:="Platinum Golden Silver Bronze Copper Nickel Aluminum Iron Tin Lead";
foreach metal in (metals.split(" ")){ n:=__metalWalker.idx;
foreach metal in (metals.split(" ")){ n:=__metalWalker.idx;
println("\nLucas sequence for %s ratio; where b = %d:".fmt(metal,n));
println("\nLucas sequence for %s ratio; where b = %d:".fmt(metal,n));
Line 2,080: Line 2,080:
mr,i := metallicRatio(lucasSeq(n));
mr,i := metallicRatio(lucasSeq(n));
println("Approximated value: %s - Reached after ~%d iterations.".fmt(mr,i));
println("Approximated value: %s - Reached after ~%d iterations.".fmt(mr,i));
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre style="height:45ex">
<pre style="height:45ex">
Line 2,123: Line 2,123:
Approximated value: 9.10977222864644365500113714088140 - Reached after ~19 iterations.
Approximated value: 9.10977222864644365500113714088140 - Reached after ~19 iterations.
</pre>
</pre>
<lang zkl>println("Golden ratio (B==1) to 256 digits:");
<syntaxhighlight lang="zkl">println("Golden ratio (B==1) to 256 digits:");
mr,i := metallicRatio(lucasSeq(1),256);
mr,i := metallicRatio(lucasSeq(1),256);
println("Approximated value: %s\nReached after ~%d iterations.".fmt(mr,i));</lang>
println("Approximated value: %s\nReached after ~%d iterations.".fmt(mr,i));</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>