一.题目描述
斐波那契数列满足 F 1 = F 2 = 1,从 F 3开始有 F n = F n − 1 + F n − 2。请你计算G C D ( F 2020 , F 520 ),其中 G C D ( A , B ) 表示 A 和 B的最大公约数。
二.题目代码
import java.math.BigInteger;//引入这个类
public class test1{
public static void main(String[] args){
BigInteger []a = new BigInteger[2021];//BigInteger内部是用一个Int[]数组
a[1] = new BigInteger(String.valueOf(1));//将数字转换为字符串(括号里的内容必须是字符串)
a[2] = new BigInteger(String.valueOf(1));//valueOf()将括号内的参数转换成前面指定的数据类型
for(int i=3;i<2021;i++){
a[i]=a[i-1].add(a[i-2]);//斐波拉契数列
}
BigInteger gcd = a[520].gcd(a[2020]);//BigInteger类中就有可以查照最大公约数的方法
System.out.println(gcd);//输出结果:6765
}
/*不用BigInteger的方法,用方法也可以实现
public static BigInteger gcd(BigInteger x,BigInteger y){
if(y.equals(BigInteger.ZERO)){
return x;
}
return gcb(y,x.mod(y));
}*/
}
三.知识点
1.什么是最大公约数?最大公约数的求法?
最大公约数:两个或多个整数共有约数中最大的一个
(1)用较大数除以较小数
(2)再用除数去除以出现的余数
(3)再用第一余数去除以出现的余数(第二余数)
(4)如此反复,直到最后余数是0为止。最后为0,则除数为最大公约数。
eg:求18和30 的最大公约数,方法如图所示。
代码:
public class test1{
public static void main(String[] args){
int a = gcb(18,30);
System.out.println(a);//输出:6
}
/*(1)用较大数除以较小数
(2)再用除数去除以出现的余数
(3)再用第一余数(此时的除数)去除以出现的余数(第二余数)
(4)如此反复,直到最后余数是0为止。最后为0,则除数为最大公约数。*/
public static int gcb(int i,int j){//函数调用(方法)
if(j==0){//除数为0时返回被除数
return i;
}
return gcb(j,i%j);
/* i=18,j=30
return gcb(30,18%30=18)
i=30,j=18
return(18,30%18=12)
i=18,j=12
return(12,18%12=6)
i=12,j=6
return(6,12%6=0)
j==0
return i=6
*/
}
}
2.斐波拉契数列:
又称黄金分割数列、因数学家列昂纳多·斐波那契(Leonardoda Fibonacci)以兔子繁殖为例子而引入,故又称为“兔子数列”,指的是这样一个数列:1、1、2、3、5、8、13、21、34、……其规律很明显,从第3个数开始,每个数都等于它前两个数的和
代码:
public class test1{
public static void main(String[] args){
int a=F(10);
System.out.println(a);//输出:55
}
public static int F(int i){//函数调用(方法)
if(i==1||i==2){//第一和第二项均返回1
return 1;
}
return F(i-1)+F(i-2);//返回前两个数的和
}
}
3.BigInteger的使用
(1)概念:
在java中有两个类BigInteger和BigDecimal分别表示大整数类和大浮点数类,从原则上是可以表示很大的数字,在使用之前,我们需要导入java.math.BigInteger包
java.math.BigInteger 就是用来表示任意大小的整数,而BigInteger内部是用一个Int[]数组来模拟
(2)介绍BigInteger经常使用到的一些函数
A.valueOf(参数); 这个函数的作用是将括号内的参数转换成前面指定的数据类型,例如以下例子:
数字直接valueOf转BigInteger类型,不需用new,数组需先用valueOf转String类型再转为BigInteger类型,需要用new
import java.math.BigInteger;//引入这个类
public class test1{
public static void main(String[] args) {
//1.int-->BigInteger
int a = 45;
BigInteger b = BigInteger.valueOf(a);//不用写new
System.out.println(b);//此时的b就是BigInteger类型的数45
//2.数组 int--->String--->BigInteger
BigInteger []arr = new BigInteger[3];
arr[0] = new BigInteger(String.valueOf(0));//需要写new
arr[1] = new BigInteger(String.valueOf(1));
arr[2] = new BigInteger(String.valueOf(2));
for(int i =0;i<arr.length;i++){
System.out.println(arr[i]);//遍历输出
}
}
}
B.运算
import java.math.BigInteger;//引入这个类
public class test1{
public static void main(String[] args) {
BigInteger a = new BigInteger("1000");
BigInteger b = new BigInteger("2000");
BigInteger add = a.add(b);//加法 3000
BigInteger subtract = a.subtract(b);//减法 -1000
BigInteger multiply = a.multiply(b);//乘法 2000000
BigInteger divide = a.divide(b);//除法 0
BigInteger mod = a.mod(b);//求余(只有正数) 1000
BigInteger remainder= a.remainder(b);//求余(正负数都有) 1000
System.out.print(add+" "+subtract+" "+multiply+" "+divide+" "+mod+" "+remainder);
}
}
C.其他运算
a.比较两个数的大小 compareTo()
a和b比大小,a小于b,返回-1,a等于b,返回0,a大于b,返回1
b.最大值与最小值 max() min()
c.最大公约数 gcd()
d.绝对值 abs()
e.幂运算 pow()
f.判断是否相等 equals()
g.素数的判断 isProbablePrime()
如果返回false,那么一定是合数。如果返回true可能是素数,概率和传入值有关
h.素数的获取 nextProbablePrime()
返回大于a的下一个素数
import java.math.BigInteger;//引入这个类
public class test1{
public static void main(String[] args) {
BigInteger a = new BigInteger("6");
BigInteger b = new BigInteger("18");
//1.比较两个数的大小 compareTo() a和b比大小,a小于b,返回-1,a等于b,返回0,a大于b,返回1
int i = a.compareTo(b);// 输出:-1
//2.最大值与最小值
BigInteger max = a.max(b);// 输出:18
BigInteger min = a.min(b);// 输出:6
//3.最大公约数
BigInteger gcd = a.gcd(b);// 输出:6
//4.绝对值
BigInteger abs = a.abs();// 输出:6
//5.幂的运算
BigInteger pow = a.pow(2);// 输出:36
//6.判断是否相等 相等返回true,不相等返回false
boolean equals = a.equals(b);// 输出:false
//7.素数的判断与获取
//(1)素数的判断 如果返回false,那么一定是合数。如果返回true可能是素数,概率和传入值有关
boolean probablePrime = a.isProbablePrime(1);//输出:false
//(2)下一个素数的获取
BigInteger c = a.nextProbablePrime();//返回大于a的下一个素数 输出:7
System.out.println(i);
System.out.println(max);
System.out.println(min);
System.out.println(gcd);
System.out.println(abs);
System.out.println(pow);
System.out.println(equals);
System.out.println(probablePrime);
System.out.println(c);
}
}
D.内部定义的常量——BigInteger.ZERO,BigInteger.ONE,BigInteger.TEN
import java.math.BigInteger;//引入这个类
public class test1{
public static void main(String[] args) {
//0
BigInteger a = BigInteger.ZERO;
BigInteger A = BigInteger.valueOf(0);
//1
BigInteger b = BigInteger.ONE;
BigInteger B = BigInteger.valueOf(1);
//2
BigInteger c = BigInteger.TWO;
BigInteger C = BigInteger.valueOf(2);
System.out.println(a);
System.out.println(A);
System.out.println(b);
System.out.println(B);
System.out.println(c);
System.out.println(C);
}
}