Metallic ratios: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (Promote to task; lots of entries, controversy is mostly resolved) |
|||
Line 442: | Line 442: | ||
Golden ratio, where b = 1: |
Golden ratio, where b = 1: |
||
Value after 615 iterations: 1.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374847540880753868917521266338622235369317931800607667263544333890865959395829056383226613199282902678806752087668925017116962070322210432162695486262963136144</pre> |
Value after 615 iterations: 1.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374847540880753868917521266338622235369317931800607667263544333890865959395829056383226613199282902678806752087668925017116962070322210432162695486262963136144</pre> |
||
=={{header|Java}}== |
|||
<lang java> |
|||
import java.math.BigDecimal; |
|||
import java.math.BigInteger; |
|||
import java.math.MathContext; |
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
public class MetallicRatios { |
|||
private static String[] ratioDescription = new String[] {"Platinum", "Golden", "Silver", "Bronze", "Copper", "Nickel", "Aluminum", "Iron", "Tin", "Lead"}; |
|||
public static void main(String[] args) { |
|||
int elements = 15; |
|||
for ( int b = 0 ; b < 10 ; b++ ) { |
|||
System.out.printf("Lucas sequence for %s ratio, where b = %d:%n", ratioDescription[b], b); |
|||
System.out.printf("First %d elements: %s%n", elements, lucasSequence(1, 1, b, elements)); |
|||
int decimalPlaces = 32; |
|||
BigDecimal[] ratio = lucasSequenceRatio(1, 1, b, decimalPlaces+1); |
|||
System.out.printf("Value to %d decimal places after %s iterations : %s%n", decimalPlaces, ratio[1], ratio[0]); |
|||
System.out.printf("%n"); |
|||
} |
|||
int b = 1; |
|||
int decimalPlaces = 256; |
|||
System.out.printf("%s ratio, where b = %d:%n", ratioDescription[b], b); |
|||
BigDecimal[] ratio = lucasSequenceRatio(1, 1, b, decimalPlaces+1); |
|||
System.out.printf("Value to %d decimal places after %s iterations : %s%n", decimalPlaces, ratio[1], ratio[0]); |
|||
} |
|||
private static BigDecimal[] lucasSequenceRatio(int x0, int x1, int b, int digits) { |
|||
BigDecimal x0Bi = BigDecimal.valueOf(x0); |
|||
BigDecimal x1Bi = BigDecimal.valueOf(x1); |
|||
BigDecimal bBi = BigDecimal.valueOf(b); |
|||
MathContext mc = new MathContext(digits); |
|||
BigDecimal fractionPrior = x1Bi.divide(x0Bi, mc); |
|||
int iterations = 0; |
|||
while ( true ) { |
|||
iterations++; |
|||
BigDecimal x = bBi.multiply(x1Bi).add(x0Bi); |
|||
BigDecimal fractionCurrent = x.divide(x1Bi, mc); |
|||
if ( fractionCurrent.compareTo(fractionPrior) == 0 ) { |
|||
break; |
|||
} |
|||
x0Bi = x1Bi; |
|||
x1Bi = x; |
|||
fractionPrior = fractionCurrent; |
|||
} |
|||
return new BigDecimal[] {fractionPrior, BigDecimal.valueOf(iterations)}; |
|||
} |
|||
private static List<BigInteger> lucasSequence(int x0, int x1, int b, int n) { |
|||
List<BigInteger> list = new ArrayList<>(); |
|||
BigInteger x0Bi = BigInteger.valueOf(x0); |
|||
BigInteger x1Bi = BigInteger.valueOf(x1); |
|||
BigInteger bBi = BigInteger.valueOf(b); |
|||
if ( n > 0 ) { |
|||
list.add(x0Bi); |
|||
} |
|||
if ( n > 1 ) { |
|||
list.add(x1Bi); |
|||
} |
|||
while ( n > 2 ) { |
|||
BigInteger x = bBi.multiply(x1Bi).add(x0Bi); |
|||
list.add(x); |
|||
n--; |
|||
x0Bi = x1Bi; |
|||
x1Bi = x; |
|||
} |
|||
return list; |
|||
} |
|||
} |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
Lucas sequence for Platinum ratio, where b = 0: |
|||
First 15 elements: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] |
|||
Value to 32 decimal places after 1 iterations : 1 |
|||
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] |
|||
Value to 32 decimal places after 78 iterations : 1.61803398874989484820458683436564 |
|||
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] |
|||
Value to 32 decimal places after 44 iterations : 2.41421356237309504880168872420970 |
|||
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] |
|||
Value to 32 decimal places after 34 iterations : 3.30277563773199464655961063373525 |
|||
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] |
|||
Value to 32 decimal places after 28 iterations : 4.23606797749978969640917366873128 |
|||
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] |
|||
Value to 32 decimal places after 25 iterations : 5.19258240356725201562535524577016 |
|||
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] |
|||
Value to 32 decimal places after 23 iterations : 6.16227766016837933199889354443272 |
|||
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] |
|||
Value to 32 decimal places after 22 iterations : 7.14005494464025913554865124576352 |
|||
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] |
|||
Value to 32 decimal places after 20 iterations : 8.12310562561766054982140985597408 |
|||
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] |
|||
Value to 32 decimal places after 20 iterations : 9.10977222864644365500113714088140 |
|||
Golden ratio, where b = 1: |
|||
Value to 256 decimal places after 615 iterations : 1.6180339887498948482045868343656381177203091798057628621354486227052604628189024497072072041893911374847540880753868917521266338622235369317931800607667263544333890865959395829056383226613199282902678806752087668925017116962070322210432162695486262963136144 |
|||
</pre> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |