Detailed Explanation of the Example of java Realizing Golden Section Number

  • 2021-08-28 20:03:35
  • OfStack

The golden section of 0.618 has an important relationship with aesthetics. The announcer stands on the stage at about 0.618 of the width of the stage.

Portrait 1 on the wall also hangs at 0.618 height of the room, and even the fluctuation of stock is said to find the shadow of 0.618..

The golden section number is an irrational number, that is, it cannot be expressed as the ratio of two integers.

0.618 is just an approximation, and its true value can be obtained by subtracting 1 from the square of 5 and dividing it by 2.

We take a more accurate approximation of it: 0.618034

Interestingly, this irrational number is also included in a simple sequence, which shocked mathematicians!

1 3 4 7 11 18 29 47 … is called "Lucas queue". Every item after it is the sum of the two items before it.

If you look at the ratio of the two items before and after, that is, 1/3, 3/4, 4/7, 7/11, 11/18 … you will find that it is getting closer and closer to the golden section!

Your task is to figure out which item to start with. This ratio of 4 to 5 has reached the accuracy of 0.618034 1.

Please write down the ratio. The format is numerator/denominator. For example: 29/47


/*
*/
package Question40_49;
public class Question44 {
public static void main(String[] args) {
int a=1,b=3,t;
while(true){
if(Math.abs((double)a/b-0.618034)<0.000001){
System.out.println(a+"/"+b+" = "+(double)a/b);
break;
}
t=a;
a=b;
b+=t;
}
}
}

Run results:

1364/2207

Supplement: Two implementations of Fibonacci sequence java + function of approaching golden section rate

Fibonacci sequence of simple implementation, this is a common recursive problem, but there are many ways to achieve, of course, the algorithm must be simple and efficient,

Those recursive algorithms on the Internet always feel bad, so I wrote the following program myself. After all, the algorithm comes out according to the problem, and it is better to have my own ideas when encountering problems, especially the algorithm.


package test;
 
import java.text.DecimalFormat;
import java.util.Arrays;
 
// The number of prints in the two methods is n Fibonacci sequence of 
public class Fibonacci {
 
 // Without the help of mathematical calculation of tool container, the simplicity and readability will be greatly reduced as more and more functions are added 
 // n Is the number of sequences to be displayed   (Note: Only displays int Value range class sequence, which can probably display 45 A) 
 protected void way1(int n) {
 int n1 = 1;
 int n2 = 1; 
 int count = 0;
 String string = new String(1+"\t"+1+"\t"); 
 if(n == 1) { 
 System.out.println("1");
 }
 // Displayable n For 1 A sequence of any number at the beginning 
 while( count != n/2 -1 ) {
 n1 += n2;
 string += Integer.toString(n1)+"\t"; 
 n2 += n1; 
 string += Integer.toString(n2)+"\t";
 count ++;
 }
 if (n%2!=0) {
 n1 = n1 + n2;
 string += Integer.toString(n1)+"\t"; 
 }
 System.out.println(string);
 }
 
 // With the help of the iterative implementation of the array, it has good readability, and at the same time, 10 It is simple and not complicated when the subsequent functions are added 
 // For example, increase here 1 A function, seek the golden section rate, to use the above realization, that code is too messy 
 protected void way2(int n) {
 int[] fbci =new int [n];
 double[] goldindex = new double[n-1]; 
 fbci[0] = 1;
 fbci[1] = 1;
 goldindex[0] = 1.00; 
 for (int i = 2; i < fbci.length; i++) {
 fbci[i] = fbci[i-1] + fbci[i-2];
 }
 
 DecimalFormat dFormat = new DecimalFormat("0.000000");// Control the number of decimal places to cancel this function 
 String result = new String();
 for (int i = 1; i < goldindex.length; i++) {
 goldindex [i] = (double)(fbci[i])/(double)(fbci[i+1]);
 result += dFormat.format(goldindex [i])+"\t"; 
 } 
 System.out.println(Arrays.toString(fbci));
 System.out.println(result);
 }
 
 public static void main(String[] args) {
 // TODO Auto-generated method stub
 Fibonacci a1 = new Fibonacci();
 a1.way1(15);
 a1.way2(15);
 } 
}

1 1 2 3 5 8 13 21 34 55 89 144 233 377 610

[1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610]

0.500000 0.666667 0.600000 0.625000 0.615385 0.619048 0.617647 0.618182 0.617978 0.618056 0.618026 0.618037 0.618033

Supplement: The problem of golden section point in JAVA

Description:

Looking for two numbers to divide, the result is closest to the golden point of 0.618

(1) Denominator and numerator cannot be even at the same time

(2) The values of denominator and numerator are in the range of [1-20


/** 
* <p>Title: Excise1</p> 
* <p>Description:     Golden section point               </p>
*   Description: Find the result of dividing two numbers   Away from the golden section  0.618 Recently   
*   ( 1 ) Denominator and numerator cannot be even at the same time        ( 2 ) Denominator and numerator   The value range is in [1-20]
* @author Mr.chen 
* @date 2018 Year 8 Month 22 Day  
*/
public class Excise1 {
 public static void main(String[] args) {
 
  int A = 0;          //A  New molecules coming out after comparison 
     int B=1;           //B  New denominator after comparison 
     double c=0,C=1;   //C  New coming out after comparison a/b Value of 
     for(int a=1;a<21;a++) {   // Circulating molecule 
       for(int b=1;b<21;b++) { // Cyclic denominator 
         if(a%2==0&b%2==0)  // If both are even at the same time, it will jump out 
           continue;
         c=(double)a/b;    // Calculation a/b And the value of   Forced conversion type   Assign a value to c
         if(Math.abs(c-0.618)<Math.abs(C-0.618)) { // If passed math Function call .abs() Square  
                              Law; Take the absolute value of the parameters in the method 
           C=c;   // By painting x Coordinate axis   If the calculated value is less than 1 Beginning to set (large C-0.168 ) In evidence  
               //  Bright distance 0.168 The distance on the left is shorter than that on the right   So assign a value to a large C  And circulate again  
               // The purpose is to make the ratio of numerator and denominator closer and closer to 0.168
           A=a;  // Will the appropriate molecules a Assign 1 Start setting it up A
           B=b;   // Will the appropriate denominator b Assign 1 Start setting it up B
         }
         
       }
     }
     System.out.println(" Away from the golden section ( 0.618 ) The last two numbers are divided by: "+A+"/"+B+"="+C);
 // Will be passed to A B  Output the value of 
  }
}

This logic is true for me! ! ! ! ! ! ! ! ! ! ! !


Related articles: