如何在 Java 中计算卢卡斯数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3793647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to calculate Lucas Numbers in Java
提问by sjaak
Hey guys, I am a starting programmer and need to program an application in java which ask for a number and then prints the first n number of Lucas Numbers. for example when 7 is put in, it outputs: 2, 1, 3, 4, 7, 11, 18.
嘿伙计们,我是一个初级程序员,需要用 java 编写一个应用程序,它要求一个数字,然后打印卢卡斯数字的前 n 个数字。例如当输入 7 时,它输出:2, 1, 3, 4, 7, 11, 18。
Just to be clear, lucas numbers are defined as:
需要明确的是,卢卡斯数定义为:
2 if n = 0
2 如果 n = 0
1 if n = 1
1 如果 n = 1
L(n-1) + L(n-2) if n>1
L(n-1) + L(n-2) 如果 n>1
I am really not sure howver how to programm this in java. Because I cannot translate this into java code. I've been thinking for a while now but still can't figure it out. Also when i would have the code for calculating the Nth lucas number, i would now how to output all the first Lucan Numbers up until the Nth number. Can some of you guys help me gte on the right way or give me tips? Thanks a lot!
我真的不知道如何在 java 中编程。因为我无法将其翻译成 java 代码。我已经思考了一段时间,但仍然无法弄清楚。此外,当我有计算第 N 个卢卡斯数的代码时,我现在将如何输出所有第一个 Lucan 数,直到第 N 个数。你们中的一些人可以帮助我走正确的道路或给我提示吗?非常感谢!
回答by Lagerbaer
The definition you have for the Lucas number is recursive, i.e., to calculate the Nth lucas number, you already need to know the N-1th and the N-2nd.
您对卢卡斯数的定义是递归的,即要计算第 N 个卢卡斯数,您已经需要知道第 N-1 个和第 N-2 个。
A naiveway to do this would be
一种天真的方法是
public int lucas(int N) {
if( N == 0 ) return 2;
if( N == 1 ) return 1;
return lucas(N-1) + lucas(N-2);
}
However, you only have to print the numbers, don't you? Then it's quite easy, actually.
但是,您只需打印数字,不是吗?那么其实很简单。
int L2 = 2;
int L1 = 1;
for( int i = 2; i <= N; i++ ) {
int L = L1 + L2;
print(L); //or whatever output function you have
L2 = L1;
L1 = L;
}
The idea is to keep the last two numbers, which you need to compute the next two numbers, always at hand.
这个想法是保持最后两个数字,你需要计算接下来的两个数字,总是在手边。
PS: These Lucas numbers are just like the Fibonacci numbers with different starting values, so any algorithm for the Fibonacci numbers will do. If you're really good at math, you can even try to find a closed formula for the Lucas numbers, but it's definitely beyond high school math (search tag would be "linear difference equation with constant coefficients").
PS:这些卢卡斯数就像斐波那契数的不同起始值,所以任何斐波那契数的算法都可以。如果你真的很擅长数学,你甚至可以尝试为卢卡斯数找到一个封闭的公式,但这绝对超出了高中数学的范围(搜索标签将是“具有常数系数的线性差分方程”)。
回答by brabster
If you're not sure how to do the whole thing, break the problem down into smaller pieces until you can start.
如果你不确定如何做整件事,把问题分解成更小的部分,直到你可以开始。
Try taking each case, one at a time. Implement the '2 if n=0' case, test that it works. The calculation is trivial, but you will have to write the code that calls your implementation, too. The trivial case helps you check that the code around it works properly.
尝试处理每个案例,一次一个。实现 '2 if n=0' 情况,测试它是否有效。计算很简单,但您也必须编写调用您的实现的代码。这个简单的案例可以帮助您检查它周围的代码是否正常工作。
Then implement the next one, check it still works, implement the final one. It'll become clearer as you go.
然后执行下一个,检查它仍然有效,执行最后一个。随着你的走,它会变得更清晰。
回答by darinks
Just in case someone is looking for a formula and happens on this page then maybe this will help. In Windows take the following line and replace each N
with the desired integer, copy the modified line and paste it into Windows' calculator:
以防万一有人正在寻找一个公式并发生在此页面上,那么这可能会有所帮助。在 Windows 中,取以下行并将每个替换N
为所需的整数,复制修改后的行并将其粘贴到 Windows 的计算器中:
(5@/2+0.5)yN+(-(5@/2+0.5))y(-N)=
(5@/2+0.5)yN+(-(5@/2+0.5))y(-N)=
For example if you want to find the Lucus number for 7
you would paste this line into Windows' calculator:
例如,如果您想为7
您找到 Lucus 数,可以将此行粘贴到 Windows 的计算器中:
(5@/2+0.5)y7+(-(5@/2+0.5))y(-7)=
(5@/2+0.5)y7+(-(5@/2+0.5))y(-7)=
And the result will be 29
.
结果将是29
。