在Java中使用BigInteger递归生成Lucas级数

在Java中使用BigInteger递归生成Lucas级数,java,recursion,biginteger,fibonacci,Java,Recursion,Biginteger,Fibonacci,我试图使用递归和biginger类将Lucas级数转换为代码,但在设置基本情况时遇到了问题。这段代码正确地输出了斐波那契序列,但是我尝试获取起始值N(0)和N(1)到2和1的所有操作都完全失败了。我已经搜索了帮助文件,但没有找到使用BigIntegers的帮助文件,因为我计划使用int限制 import java.math.BigInteger; public class LucasSeries { private static BigInteger TWO = BigInteger

我试图使用递归和
biginger
类将Lucas级数转换为代码,但在设置基本情况时遇到了问题。这段代码正确地输出了斐波那契序列,但是我尝试获取起始值N(0)和N(1)到2和1的所有操作都完全失败了。我已经搜索了帮助文件,但没有找到使用
BigIntegers
的帮助文件,因为我计划使用
int
限制

import java.math.BigInteger;

public class LucasSeries {

    private static BigInteger TWO = BigInteger.valueOf(2);

    public static BigInteger fibonacci(BigInteger number) {
        if (number.equals(BigInteger.ZERO) || number.equals(BigInteger.ONE)) {
            return number;
        } else {
            return fibonacci(number.subtract(BigInteger.ONE)).add(fibonacci(number.subtract(TWO))); 
        }
    }

    public static void main(String[] args) {
        for (int counter = 0; counter <= 30; counter++) {
           System.out.printf("Lucas of %d is: %d%n", counter,
                fibonacci(BigInteger.valueOf(counter)));
        }
    }
}
import java.math.biginger;
公共级卢卡斯酒店{
私有静态BigInteger 2=BigInteger.valueOf(2);
公共静态BigInteger fibonacci(BigInteger数字){
if(number.equals(biginger.ZERO)| number.equals(biginger.ONE)){
返回号码;
}否则{
返回fibonacci(number.subtract(biginger.ONE)).add(fibonacci(number.subtract(TWO));
}
}
公共静态void main(字符串[]args){

对于(int counter=0;counter有两种基本情况
N(0)
N(1)
。您可以将它们组合在一起,但如果单独处理它们,代码会更加匹配,如:

public static BigInteger fibonacci(BigInteger number) {
    if (number.equals(BigInteger.ZERO))
        return TWO;

    if(number.equals(BigInteger.ONE))
        return BigInteger.ONE;

    return fibonacci(number.subtract(BigInteger.ONE)).add(
            fibonacci(number.subtract(TWO)));
}
它产生以下输出:

Lucas of 0 is: 2
Lucas of 1 is: 1
Lucas of 2 is: 3
Lucas of 3 is: 4
Lucas of 4 is: 7
Lucas of 5 is: 11
Lucas of 6 is: 18
Lucas of 7 is: 29
Lucas of 8 is: 47
// Remainer omitted

有两种基本情况
N(0)
N(1)
。您可以将它们组合起来,但如果单独处理它们,代码会更匹配,如:

public static BigInteger fibonacci(BigInteger number) {
    if (number.equals(BigInteger.ZERO))
        return TWO;

    if(number.equals(BigInteger.ONE))
        return BigInteger.ONE;

    return fibonacci(number.subtract(BigInteger.ONE)).add(
            fibonacci(number.subtract(TWO)));
}
它产生以下输出:

Lucas of 0 is: 2
Lucas of 1 is: 1
Lucas of 2 is: 3
Lucas of 3 is: 4
Lucas of 4 is: 7
Lucas of 5 is: 11
Lucas of 6 is: 18
Lucas of 7 is: 29
Lucas of 8 is: 47
// Remainer omitted
而不是

if (number.equals(BigInteger.ZERO) || number.equals(BigInteger.ONE))
    return number;
归来

if (number.equals(BigInteger.ZERO) || number.equals(BigInteger.ONE)) {
    return TWO.subtract(number);
仅此而已。

而不是

if (number.equals(BigInteger.ZERO) || number.equals(BigInteger.ONE))
    return number;
归来

if (number.equals(BigInteger.ZERO) || number.equals(BigInteger.ONE)) {
    return TWO.subtract(number);

仅此而已。

请包括适用的错误代码和消息如果我理解你的问题,这个代码是有效的,但是你尝试过的很多其他事情都不起作用?这些事情是什么?它们怎么不起作用?注意,
fibonacci
的参数没有必要是
biginger
。除非你需要超过2147483647个fibonacci数,否则
int
就可以了这是个坏主意。返回类型可以是
biginger
@BilltheLizard,我为不清楚而道歉,但大多数都是下面给出的格式不正确。我尝试了
return 2
,而不是
return TWO
,在用大约三到四种不同的方式分离BigInt zero和one之后。请包括适用的错误代码和消息如果我理解你的问题,这个代码是有效的,但是你尝试过的很多其他事情都不起作用?这些事情是什么?它们怎么不起作用?注意,
fibonacci
的参数没有必要是
biginger
。除非你需要超过2147483647个fibonacci数,否则
int
就可以了这是个坏主意。返回类型可以是
biginger
@BilltheLizard,我为不清楚而道歉,但大多数都是下面给出的格式不正确。我尝试了
return 2
,而不是
return TWO
,在以大约三到四种不同的方式分离BigInt zero和one之后。OP想要
0=>2
1=>1
。不确定你是从哪里得到这个主意的。@AlexHall好的,没问题,我修好了,谢谢你指出它。OP想要
0=>2
1=>1
。不确定你是从哪里得到这个主意的。@AlexHall好的,没问题,我修好了,谢谢你指出它。