Metallic ratios: Difference between revisions
m
syntax highlighting fixup automation
m (Ruby 3.1 changed "/".) |
Thundergnat (talk | contribs) 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).
<
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)); }
}</
{{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}}
<
#include <iostream>
Line 280:
return 0;
}</
{{Out}}
<pre>Lucas sequence for Platinum ratio, where b = 0:
Line 326:
=={{header|F_Sharp|F#}}==
<
// 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>
{{out}}
<pre>
Line 372:
=={{header|Factor}}==
<
math prettyprint qw sequences ;
IN: rosetta-code.metallic-ratios
Line 400:
[ "- reached after %d iteration(s)\n\n" printf ]
} spread
] each-index</
{{out}}
<pre style="height:45ex">
Line 445:
=={{header|Go}}==
<
import (
Line 499:
fmt.Println("Golden ratio, where b = 1:")
metallic(1, 256)
}</
{{out}}
Line 549:
=={{header|Groovy}}==
{{trans|Kotlin}}
<
private static List<String> names = new ArrayList<>()
static {
Line 613:
metallic(1, 256)
}
}</
{{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">
258j256":%~/+/+/ .*~^:10 x:*i.2 2
1.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374847540880753868917521266338622235369317931800607667263544333890865959395829056383226613199282902678806752087668925017116962070322210432162695486262963136144
</syntaxhighlight>
However, the task implies we should use a different form of iteration, so we should probably stick to that here.
<syntaxhighlight 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>
=={{header|Java}}==
<
import java.math.BigDecimal;
import java.math.BigInteger;
Line 806:
}
</syntaxhighlight>
{{out}}
<pre>
Line 854:
=={{header|Julia}}==
<
import Base.iterate, Base.IteratorSize, Base.IteratorEltype, Base.Iterators.take
Line 890:
println("Golden ratio to 256 decimal places:")
metallic(1, 256)
</
<pre>
The first 15 elements of the Lucas sequence named Platinum and b of 0 are:
Line 938:
=={{header|Kotlin}}==
{{trans|Go}}
<
import java.math.BigInteger
Line 991:
println("Golden ration, where b = 1:")
metallic(1, 256)
}</
{{out}}
<pre>Lucas sequence for Platinum ratio, where b = 0:
Line 1,037:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
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]</
{{out}}
<pre>b=0
Line 1,115:
=={{header|Nim}}==
{{libheader|bignum}}
<
import bignum
Line 1,170:
echo "Golden ratio where b = 1:"
computeRatio(1, 256)</
{{out}}
Line 1,217:
=={{header|Perl}}==
<
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);</
{{out}}
<pre>'Lucas' sequence for Platinum ratio, where b = 0:
Line 1,302:
{{trans|Go}}
{{libheader|Phix/mpfr}}
<!--<
<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>
<!--</
{{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}}==
<
from _pydecimal import getcontext, Decimal
Line 1,432:
print(f'\nb = 1 with 256 digits:')
stable(1, 256)</
{{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"
use Lingua::EN::Numbers;
Line 1,493:
# Stretch goal
say join "\n", "\nGolden ratio to 256 decimal places:", display |metallic lucas(1), 256;</
{{out}}
<pre>Lucas sequence for Platinum ratio; where b = 0:
Line 1,542:
=={{header|REXX}}==
For this task, the elements of the Lucas sequence are zero based.
<
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. */</
{{out|output|text= when using the default inputs:}}
Line 1,649:
=={{header|Ruby}}==
{{works with|Ruby|2.3}}
<
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')])</
{{out}}
<pre>Lucas Sequences...
Line 1,742:
=={{header|Sidef}}==
<
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"
}</
{{out}}
<pre>
Line 1,810:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<
Module Module1
Line 1,921:
End Sub
End Module</
{{out}}
<pre>Metal B Sq.Rt Iters /---- 32 decimal place value ----\\ Matches Sq.Rt Calc
Line 1,953:
{{libheader|Wren-big}}
{{libheader|Wren-fmt}}
<
import "/fmt" for Fmt
Line 2,002:
}
System.print("Golden ratio, where b = 1:")
metallic.call(1, 256)</
{{out}}
Line 2,052:
=={{header|zkl}}==
{{libheader|GMP}} GNU Multiple Precision Arithmetic Library
<
fcn lucasSeq(b){
Walker.zero().tweak('wrap(xs){
Line 2,073:
b,mr = c,mr2;
}
}</
<
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));
}</
{{out}}
<pre style="height:45ex">
Line 2,123:
Approximated value: 9.10977222864644365500113714088140 - Reached after ~19 iterations.
</pre>
<
mr,i := metallicRatio(lucasSeq(1),256);
println("Approximated value: %s\nReached after ~%d iterations.".fmt(mr,i));</
{{out}}
<pre>
|