斐波那契数列最大公约数java

【问题描述】 斐波那契数列满足 F1 = F2 = 1,从 F3 开始有 Fn = Fn 1 + Fn 2。请你计算 GCD(F2020, F520),其中 GCD(A, B) 表示 A 和 B 的最大公约数。 【答案提交】 这是一道结果填空题,你只需要算出结果后提交即可。本题的结果为一个 整数,在提交答案时只填写这个整数,填写多余的内容将无法得分

import java.math.BigInteger;

public class Main7 {
          
   

	public static void main(String[] args) {
          
   
		// TODO Auto-generated method stub
		BigInteger f1 = new BigInteger("1");
		BigInteger f2 = new BigInteger("1");
		BigInteger f3;
		BigInteger[] big = new BigInteger[2051];
		big[1] = f1;
		big[2] = f2;
		for(int i=3;i<=2050;i++)
		{
          
   
			big[i] = big[i-1].add(big[i-2]);
		}
		f3 = gcd(big[2020],big[520]);
		System.out.println(f3);
	}
	public static BigInteger gcd(BigInteger a,BigInteger b)
	{
          
   
		if(a.mod(b)==BigInteger.ZERO)
			return b;
		else
			return gcd(b,a.mod(b));
	}

}
经验分享 程序员 微信小程序 职场和发展