Metallic ratios: Difference between revisions

Added C# version, includes reality check of results
m (The subscripted characters in the task description weren't showing up on some browsers, so they were replaced by "sub" tags. Feel free to revert if it is worse now.)
(Added C# version, includes reality check of results)
Line 153:
* [[wp:Metallic_mean|Wikipedia: Metallic mean]]
* [[wp:Lucas_sequence|Wikipedia: Lucas sequence]]
 
 
=={{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).
<lang csharp>using static System.Math;
using static System.Console;
using BI = System.Numerics.BigInteger;
class Program {
static BI IntSqRoot(BI v, BI res) { // res is the initial guess
BI term = 0, d = 0, dl = 1; while (dl != d) { term = v / res; res = (res + term) >> 1;
dl = d; d = term - res; } return term; }
static string doOne(int b, int digs) { // calculates result via square root, not iterations
int s = b * b + 4; BI g = (BI)(Sqrt((double)s) * Pow(10, ++digs)),
bs = IntSqRoot(s * BI.Parse('1' + new string('0', digs << 1)), g);
bs += b * BI.Parse('1' + new string('0', digs));
bs >>= 1; bs += 4; string st = bs.ToString();
return string.Format("{0}.{1}", st[0], st.Substring(1, --digs)); }
static string divIt(BI a, BI b, int digs) { // performs division
int al = a.ToString().Length, bl = b.ToString().Length;
a *= BI.Pow(10, ++digs << 1); b *= BI.Pow(10, digs);
string s = (a / b + 5).ToString(); return s[0] + "." + s.Substring(1, --digs); }
// custom formating
static string joined(BI[] x) { int[] wids = {1, 1, 2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
string res = ""; for (int i = 0; i < x.Length; i++) res +=
string.Format("{0," + (-wids[i]).ToString() + "} ", x[i]); return res; }
static void Main(string[] args) { // calculates and checks each "metal"
WriteLine("Metal B Sq.Rt Iters /---- 32 decimal place value ----\\ Matches Sq.Rt Calc");
int k; string lt, t = ""; BI n, nm1, on; for (int b = 0; b < 10; b++) {
BI[] lst = new BI[15]; lst[0] = lst[1] = 1;
for (int i = 2; i < 15; i++) lst[i] = b * lst[i - 1] + lst[i - 2];
// since all the iterations (except Pt) are > 15, continue iterating from the end of the list of 15
n = lst[14]; nm1 = lst[13]; k = 0; for (int j = 13; k == 0; j++) {
lt = t; if (lt == (t = divIt(n, nm1, 32))) k = b == 0 ? 1 : j;
on = n; n = b * n + nm1; nm1 = on; }
WriteLine("{0,4} {1} {2,2} {3, 2} {4} {5}\n{6,19} {7}", "Pt Au Ag CuSn Cu Ni Al Fe Sn Pb"
.Split(' ')[b], b, b * b + 4, k, t, t == doOne(b, 32), "", joined(lst)); }
// calculate and check the big one
n = nm1 =1; k = 0; for (int j = 1; k == 0; j++) {
lt = t; if (lt == (t = divIt(n, nm1, 256))) k = j;
on = n; n += nm1; nm1 = on; }
WriteLine("\nAu to 256 digits:"); WriteLine(t); WriteLine("Matched Sq.Rt Calc: {0}", t == doOne(1, 256)); }
}</lang>
{{out}}
<pre style="white-space:pre-wrap;">Metal B Sq.Rt Iters /---- 32 decimal place value ----\ Matches Sq.Rt Calc
Pt 0 4 1 1.00000000000000000000000000000000 True
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Au 1 5 78 1.61803398874989484820458683436564 True
1 1 2 3 5 8 13 21 34 55 89 144 233 377 610
Ag 2 8 44 2.41421356237309504880168872420970 True
1 1 3 7 17 41 99 239 577 1393 3363 8119 19601 47321 114243
CuSn 3 13 34 3.30277563773199464655961063373525 True
1 1 4 13 43 142 469 1549 5116 16897 55807 184318 608761 2010601 6640564
Cu 4 20 28 4.23606797749978969640917366873128 True
1 1 5 21 89 377 1597 6765 28657 121393 514229 2178309 9227465 39088169 165580141
Ni 5 29 25 5.19258240356725201562535524577016 True
1 1 6 31 161 836 4341 22541 117046 607771 3155901 16387276 85092281 441848681 2294335686
Al 6 40 23 6.16227766016837933199889354443272 True
1 1 7 43 265 1633 10063 62011 382129 2354785 14510839 89419819 551029753 3395598337 20924619775
Fe 7 53 22 7.14005494464025913554865124576352 True
1 1 8 57 407 2906 20749 148149 1057792 7552693 53926643 385039194 2749201001 19629446201 140155324408
Sn 8 68 20 8.12310562561766054982140985597408 True
1 1 9 73 593 4817 39129 317849 2581921 20973217 170367657 1383914473 11241683441 91317382001 741780739449
Pb 9 85 20 9.10977222864644365500113714088140 True
1 1 10 91 829 7552 68797 626725 5709322 52010623 473804929 4316254984 39320099785 358197153049 3263094477226
 
Au to 256 digits:
1.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374847540880753868917521266338622235369317931800607667263544333890865959395829056383226613199282902678806752087668925017116962070322210432162695486262963136144
Matched Sq.Rt Calc: True</pre>Note: All the metals (except bronze) in this task are elements, so those symbols are used. Bronze is usually approx. 88% '''Cu''' and 12 % '''Sn''', and so it is labeled '''CuSn''' here.
 
=={{header|Factor}}==