Java 减少斐波那契数的特殊代码所花费时间的方法?

Java 减少斐波那契数的特殊代码所花费时间的方法?,java,performance,time-complexity,fibonacci,Java,Performance,Time Complexity,Fibonacci,作为一个有趣的项目,我开始编写这段代码,希望每次都能找到斐波那契数平方的最后一位。它工作的很好,但我想减少它所花费的时间。。。有什么建议吗 琐事:我找到斐波那契数,将它们平方,然后找到总和 import java.util.*; import java.math.*; public class FibonacciSumSquares { private static BigInteger getFibonacciSumSquares(int n) { if(n<=

作为一个有趣的项目,我开始编写这段代码,希望每次都能找到斐波那契数平方的最后一位。它工作的很好,但我想减少它所花费的时间。。。有什么建议吗

琐事:我找到斐波那契数,将它们平方,然后找到总和

import java.util.*;
import java.math.*;

public class FibonacciSumSquares {
    private static BigInteger getFibonacciSumSquares(int n) {
        if(n<=2)return BigInteger.valueOf(n);
    BigInteger sum = BigInteger.valueOf(2);
    long last = 1, lastTwo = 1, current = 0;
    BigInteger lastBigInteger = BigInteger.ONE;
    BigInteger lastTwoBigInteger = BigInteger.ONE;
    BigInteger currentBigInteger;
    boolean isUsePrimary = true;

    for (int i = 3; i <=n; i++) {
    if (isUsePrimary){
        current = last + lastTwo;
        current = current * current;
        if (String.valueOf(current).length()<12) {
            lastTwo = last;
            last = current;
            sum = sum.add(BigInteger.valueOf(current));
        } else {
            isUsePrimary = false;
            lastTwoBigInteger = BigInteger.valueOf(lastTwo);
            lastBigInteger = BigInteger.valueOf(last);
            currentBigInteger = lastBigInteger.add(lastTwoBigInteger);
            currentBigInteger = currentBigInteger.pow(2);

            sum = sum.add(currentBigInteger);
        }
    } //end of outer if
    else {
        currentBigInteger = lastBigInteger.add(lastTwoBigInteger);
        lastTwoBigInteger=lastBigInteger;
        currentBigInteger = currentBigInteger.pow(2);
        lastBigInteger= currentBigInteger;
        sum = sum.add(currentBigInteger);
    }
   }//end of for
  return sum.remainder(BigInteger.valueOf(10));
}//end of function


public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);

    int n = scanner.nextInt();

    System.out.println(getFibonacciSumSquares(n));
}
import java.util.*;
导入java.math.*;
公共级斐波那契广场{
私有静态BigInteger getFibonacciSumSquares(int n){

如果(哦,你只需要最后一个数字,这是一个经典的数学转折:-)

通常情况下,如果你只需要最后一个数字,你可以忽略所有步骤中的其他数字,只要它只是加法或乘法

您的公式基本上是f(n+2)=[f(n)+f(n+1)]^2,因此,最后一位数字仅取决于先前结果的最后一位数字。此外,总和的最后一位数字仅取决于您添加的每个数字的最后一位数字

所以你可以对每一个当前值或求和值进行mod 10,你不需要BigInteger甚至long,你只需要使用int

private static int getFibonacciSquaredSumMod10(int n) {
    if (n <= 1)
        return n;
    int sum = 0;
    int last = 1, lastTwo = 1, current = 0;

    for (int i = 2; i <= n; i++) {
            current = last + lastTwo;
            current = (current * current) % 10;
            lastTwo = last;
            last = current;
            sum = (sum + current) % 10;
    }
    return sum;
}
private static int getFibonacciSquaredSumMod10(int n){

如果(那么,这不是斐波那契数列,而是一个新数列,其性质为f(n+2)=[f(n)+f(n+1)]^2?对于线性递归数列,如斐波那契数列或卢卡斯数列,有闭合形式的表达式,但我不知道更高阶的数列(即平方).最好问一下你的问题,在我想要整数的情况下,有没有类似的技巧?只有最后一个。我没有看到任何更快的技巧。没有平方和和,有很多方法可以改进它,例如,这些方法不适用于平方和和,但可能还有其他方法?你可以在上找到答案。