Jump to content

Metallic ratios: Difference between revisions

m
→‎{{header|zkl}}: update functionality
(Added Perl example)
m (→‎{{header|zkl}}: update functionality)
Line 682:
}.fp(L(BI(1),BI(1)))).push(1,1) // xn can get big so use BigInts
}
fcn metallicRatio(lucasSeq,digits=32,roundup=True){ #-->(String,num iterations)
const bige:=BI("1e331e",E=10+(digits+1)); //# x[n-1]*bige*b / x[n-2] to get our digits from Ints
a,b,mr := lucasSeq.next(), lucasSeq.next(), BI(bige).mul(*b).div(a);
do(100020_000){ // limit iterations
c,m2mr2 := lucasSeq.next(), BI(bige).mul(*c).div(b);
a,b,if(mr = b,c,m2;=mr2){
if((mr - m2).abs()<E) return(mr.div(E),lucasSeq.idx); // idx ignores push(), ie first 2 terms
mr=mr2.add(5*roundup).div(10).toString();
a,b,mr = b,c,m2;
.fmtreturn(String(mr[0],".",mr.del(0)),i));
if((mr - m2).abs()<E) return(mr.div(E),lucasSeq.idx); // idx ignores push(), ie first 2 terms
}
b,mr = c,mr2;
}
}</lang>
<lang zkl>metals:="Platinum Golden Silver Bronze Copper Nickel Aluminum Iron Tin Lead";
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));
println("First 15 elements: ",lucasSeq(n).walk(15).concat(" "));
mr,i := metallicRatio(lucasSeq(n));
println("Approximated value: %s.%s - Reached after ~%d iterations.".fmt(mr,i));
mr = mr.toString();
println("Approximated value: %s.%s - Reached after ~%d iterations."
.fmt(mr[0],mr.del(0),i));
}</lang>
{{out}}
Line 708 ⟶ 711:
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
Approximated value: 1.61803398874989484820458683436564 - Reached after ~7781 iterations.
 
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
Approximated value: 2.41421356237309504880168872420970 - Reached after ~4345 iterations.
 
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
Approximated value: 3.3027756377319946465596106337352430277563773199464655961063373525 - Reached after ~3234 iterations.
 
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
Approximated value: 4.2360679774997896964091736687312723606797749978969640917366873128 - Reached after ~2728 iterations.
 
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
Approximated value: 5.19258240356725201562535524577016 - Reached after ~2425 iterations.
 
Lucas sequence for Aluminum ratio; where b = 6:
First 15 elements: 1 1 7 43 265 1633 10063 62011 382129 2354785 14510839 89419819 551029753 3395598337 20924619775
Approximated value: 6.1622776601683793319988935444327116227766016837933199889354443272 - Reached after ~22 iterations.
 
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
Approximated value: 7.1400549446402591355486512457635114005494464025913554865124576352 - Reached after ~2021 iterations.
 
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
Approximated value: 8.1231056256176605498214098559740712310562561766054982140985597408 - Reached after ~1920 iterations.
 
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
Approximated value: 9.1097722286464436550011371408813910977222864644365500113714088140 - Reached after ~1819 iterations.
</pre>
<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));</lang>
{{out}}
<pre>
Golden ratio (B==1) to 256 digits:
Approximated value: 1.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374847540880753868917521266338622235369317931800607667263544333890865959395829056383226613199282902678806752087668925017116962070322210432162695486262963136144
Reached after ~616 iterations.
</pre>
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.