As expected of Jingdong's big cow! Detailed examples of implementing the golden section number in Java (with code)

This article mainly introduces the detailed examples of how to implement the golden ratio in java. It has a good reference value and I hope it will be helpful to everyone. Let's follow the editor to take a look.

The golden ratio of 0.618 has an important relationship with aesthetics. The position where the announcer stands on the stage is about 0.618 of the width of the stage,

The portraits on the wall are generally hung at 0.618 of the height of the room, and even the fluctuation of stocks is said to be able to find the shadow of 0.618...

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

0.618 is just its approximate value. Its true value can be obtained by subtracting 1 from the square root of 5 and dividing by 2.

We take a more accurate approximation: 0.618034

What's interesting is that some simple series will also contain this irrational number, which shocked mathematicians!

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

If you observe 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 ratio!

Your task is to figure out which one to start from. After rounding the ratio, it has reached an accuracy consistent with 0.618034.

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;
}
}
}

operation result:

1364/2207

Supplement: Two realizations of Fibonacci sequence in java + function of approximating the golden ratio

The simple implementation of Fibonacci sequence is a common recursion problem, but there are many ways to implement it. Of course, the algorithm must be simple and efficient.

Those recursive algorithms on the Internet always feel ugly, so I wrote the following program myself. After all, the algorithm is based on problems. It is better to have your own ideas when you encounter problems, especially the algorithm.

package test;
 
import java.text.DecimalFormat;
import java.util.Arrays;
 
//两种方法打印个数为n的斐波那契数列
public class Fibonacci {
 
 //不借助工具容器的数学计算,当增加的功能越来越多时,简洁性和可读性都会大大降低
 // n为需要显示的数列个数 (注:仅显示int值范围类数列,大概能显示45个)
 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");
 }
 //可显示n为1开始的任何数的数列
 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);
 }
 
 //借助数组的迭代实现,有很好的可读性,同时十分简洁,在后续功能增加的情况下也不复杂
 //比如这里增加一个功能,求黄金分割率,要用上面的实现的话,那代码就太乱了
 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");//控制小数位数,可取消该功能
 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 the golden section point in JAVA


#Description : Find the division of two numbers, the result is closest to the golden section point 0.618

(1) The denominator and numerator cannot be even numbers at the same time

(2) The range of denominator and numerator is [1-20

/** 
* <p>Title: Excise1</p> 
* <p>Description:    黄金分割点              </p>
*  描述:寻找某两个数相除,其结果 离黄金分割点 0.618最近  
*  (1)分母和分子不能同时为偶数      (2)分母和分子 取值范围在[1-20]
* @author Mr.chen 
* @date 2018年8月22日 
*/
public class Excise1 {
 public static void main(String[] args) {
 
  int A = 0;          //A 比较后传出来的新分子
     int B=1;           //B 比较后传出的新分母
     double c=0,C=1;   //C 比较后传出来的新a/b的值
     for(int a=1;a<21;a++) {   //循环分子
       for(int b=1;b<21;b++) { //循环分母
         if(a%2==0&b%2==0)  //如果两个同时为偶数是跳出
           continue;
         c=(double)a/b;    //计算a/b的值并且 强制转化类型 赋值给c
         if(Math.abs(c-0.618)<Math.abs(C-0.618)) { //如果通过math函数调用.abs()方 
                             法;取方法内参数的绝对值
           C=c;   //通过画x坐标轴 如果算出来的值小于一开始设定的(大C-0.168)就证 
               // 明距离0.168左边的距离比右边的短 所以赋值给大C 并且再次循环 
               //目的使得通过循环让分子和分母的比值越来越趋近于0.168
           A=a;  //将合适的分子a赋给一开始设定好的A
           B=b;   //将合适的分母b赋给一开始设定好的B
         }
         
       }
     }
     System.out.println("离黄金分割点(0.618)最近的两个数相除是:"+A+"/"+B+"="+C);
 //将传给A B 的值输出来
  }
}

For Xiaobai, this logic is a real dick! ! ! ! ! ! ! ! ! ! ! !

The above is the whole content of this article, I hope it will be helpful to everyone's study, and I hope everyone will support you

The latest high-frequency interview questions collected in 2021 (all organized into documents), there are a lot of dry goods, including mysql, netty, spring, thread, spring cloud, jvm, source code, algorithm and other detailed explanations. There are also detailed learning plans and interviews. Questions, etc., friends who need to obtain these content, please add Q Junyang: 547998459

Guess you like

Origin blog.csdn.net/p1830095583/article/details/114882962