zoukankan      html  css  js  c++  java
  • 基础算法回顾

    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);
            }
        }
    }
    
    
  • 相关阅读:
    基于Grafana+SimpleJson的灵活报表解决方案
    Scala安装时的坑
    Windows批量添加防火墙例外端口
    VMware与Hyper-V
    InfluxDB:cannot use field in group by clause
    .Net版InfluxDB客户端使用时的一些坑
    KafkaManager中Group下不显示对应Topic的解决方案
    Linux下查看Go语言软件运行情况
    Flink升级到1.4版本遇到的坑
    spring cloud(一)带你进入分布式
  • 原文地址:https://www.cnblogs.com/ukzq/p/13435635.html
Copyright © 2011-2022 走看看