基础算法回顾

package 1;

/**
 * @Author: zhangQi
 * @Date: 2020-07-21 10:12
 */
public class PrimeTest {
    //print
    public static void main(String[] args) {
        int the10001PrimeNumber = getThe10001PrimeNumber();
        System.out.println(the10001PrimeNumber);
    }
    //get result
    public static int getThe10001PrimeNumber(){
       int the10001Index = 10001;
       int j =1;
       int i =1;
       int result =0;
       while(j<the10001Index){
           if(isPrimeNumber(i)){
               result =i;
               j++;
           }
           i+=2;
       }
       return result;
    }
    //is it prime?
    static boolean isPrimeNumber(int n){
        if(n<2){
            return false;
        }
        double max = Math.sqrt(n);
        for(int i=2;i<=max;i++){
            if(n%i==0){
                return false;
            }
        }
        return true;
    }
}

package 1;

/**
 * @Author: zhangQi
 * @Date: 2020-07-21 10:47
 */
public class FibonacciTest {

    //print
    public static void main(String[] args) {
        double[] bds = new double[4];
        bds[0] = 1E-6;
        bds[1] = 1E-8;
        bds[2] = 1E-12;
        bds[3] = 1E-16;
        //test begin
        for (int i = 0; i < bds.length; i++) {
            double goldenRatio = getGoldenRatio(bds[i]);
            System.out.println("the "+(i+1)+" return:"+goldenRatio);
        }
    }

    //get golden ratio
    public static double getGoldenRatio(double t) {
        long theNumber = 0;
        //n not be zero
        for (long n = 1; n < Long.MAX_VALUE; n++) {
            if (Math.abs(((fibonacci(n + 2) / fibonacci(n + 1)) - (fibonacci(n + 1) / fibonacci(n)))) < t) {
                theNumber = n;
                break;
            }
        }
        System.out.println("the n:"+theNumber);
        return fibonacci(theNumber + 1) / fibonacci(theNumber);
    }

    //get Fibonacci
    static long fibonacci(long n) {
        if ((n == 0) || (n == 1)) {
            return n;
        } else {
            return fibonacci(n - 1) + fibonacci(n - 2);
        }
    }
}

原文地址:https://www.cnblogs.com/ukzq/p/13435635.html