The third session-golden consecutive points

Title: Golden
Continued Fraction The Golden Ratio 0.61803... is an irrational number. This constant is very important and will appear in many engineering problems. Sometimes it is necessary to find this number very accurately. For some precision engineering, the accuracy of the constant is very important. How do we find the most accurate value of the golden ratio? There are many ways. The simpler one is to use continued fractions:
1
golden number = ---------------------
1
1 + ------------ -----
1
1 + -------------
1
1 + ---------
1 +…

The more "layers" the continued fraction is calculated, the closer its value is to the golden ratio.
Please use this feature to find a sufficiently accurate value of the golden ratio, requiring rounding to 100 digits after the decimal point.
The value of 3 digits after the decimal point is: 0.618 The value
of 4 digits after the decimal point is: 0.6180 (note the 0 at the end, can not be ignored)
Your task is: write the golden section value accurate to 100 digits after the decimal point.
Note: the mantissa is rounded off! Keep the mantissa 0!
Obviously the answer is a decimal with 100 digits after the decimal point. Please submit the number directly through the browser.

Idea: Do a
pen to calculate, find the pattern, and find that this is not the Fibonacci sequence?
1. Convert to finding two adjacent Fibonacci numbers, but to which item?
The more the more, the more accurate, the n/n+1 term, the condition that n must meet: n increases again, the 101 digits after the decimal point of this ratio is stable, that is, the constant
2. double cannot represent 100 decimal places, Need to use big number operations BigInteger and BigDecimal

public class Test {
    
    
	public static void main(String[] args) {
    
    
		BigInteger a = BigInteger.ONE;
		BigInteger b = BigInteger.ONE;
		// int aa = 1, bb = 1, r = 3;
		// while (r < 300) {
    
    
		// int cc = aa + bb;
		// aa = bb;
		// bb = cc;
		// }
		// int ans =aa/bb;
		for (int i = 3; i < 500; i++) {
    
    
			BigInteger temp = a.add(b);
			a = b;
			b = temp;
		}
		BigDecimal ans = new BigDecimal(a, 110).divide(new BigDecimal(b, 110), BigDecimal.ROUND_HALF_DOWN);
		System.out.println(ans.toPlainString().substring(0, 103));
	}

}

Method analysis:

  1. The most appropriate way of saying BigDecimal.ROUND_HALF_DOWN should be called rounding. If the discarded part is greater than 5, it will be rounded up, and it will be discarded if it is less than or equal to 5.
  2. String toPlainString() ——Returns a string representation of this BigDecimal without an exponent field. (an exponent field. 指数域)
  3. String toString() ——Returns the string representation of this BigDecimal, using scientific notation if an exponent is needed.
  4. toString() displays the value in scientific notation in some cases, and toPlainString() always accurately displays the value itself.
    Have to think hard. So this uses toPlainString().
  5. public String substring(int beginIndex, int endIndex )——returns a string substring [beginIndex, endIndex-1]. The length of such a substring is endIndex-beginIndex.

Here, the substring is intercepted from 0, intercepted to 101 digits after the decimal point, plus the decimal point and 0 before the decimal point, for a total of 103 digits.
Because the question requires 100 digits, the last digit of the answer must be rounded up! !
Insert picture description here
The final answer:

0.61803398874989484820458683436563811772030917980576286213544862270526046281890244970720720418939113745

end.

Guess you like

Origin blog.csdn.net/weixin_44998686/article/details/109008914