Java 代码中的斐波那契数列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19045103/
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
Fibonacci in Java code
提问by Marz
public class Fibonacci {
public static long fib(int n) {
if (n <= 1) return n;
else return fib(n-1) + fib(n-2);
}
public static void main(String[] args) {
int N = Integer.parseInt(args[0]);
for (int i = 1; i <= N; i++)
System.out.println(i + ": " + fib(i));
}
}
Let's assume that the user input "java Fibonacci 7", so the result would be like this: 1: 1
假设用户输入“java Fibonacci 7”,那么结果是这样的:1:1
2: 1
2:1
3: 2
3:2
4: 3
4:3
5: 5
5:5
6: 8
6:8
7: 13
7:13
I seem to be totally confused with how this works. Starting with number 3. When the fib(i) method passes 3, shouldn't it return 3 as well since if n = 3 then the sum of fib(n-1) /which is 2/ and fib(n-2) /which 1/ is 3. And so on the other numbers forward.
我似乎完全不明白这是如何工作的。从数字 3 开始。 当 fib(i) 方法通过 3 时,它不应该也返回 3,因为如果 n = 3 那么 fib(n-1) / 的总和为 2/ 和 fib(n-2) / which 1/ is 3. 以此类推,其他数字向前。
采纳答案by Alex VII
If you pass 3 to your function it will do the following:
如果您将 3 传递给您的函数,它将执行以下操作:
fib(3) = fib(2) + fib(1) //so we we are into the else statement, because 3 > 1
= fib(2) + 1 //fib(1) = 1 because 1 <= 1 so you return it (if statement)
= (fib(1) + fib(0)) + 1 //2 > 1 => we go to the else statement
= (1 + 0) + 1 //0 <= 1 & 1 <= 1 so we are into the if and return the values
= 2
回答by hoekki
I think you are confusing the index (n) in the Fibonacci sequence with the actual value at that index. fib(n=3) = fib(n-1=2) + fib(n-2=1) = 1 + 1 = 2
我认为您将斐波那契数列中的索引 (n) 与该索引的实际值混淆了。fib(n=3) = fib(n-1=2) + fib(n-2=1) = 1 + 1 = 2
回答by Bhushankumar Lilapara
why dont you try this easy code. It's same like we do in Fibonacci without recursion.
你为什么不试试这个简单的代码。就像我们在没有递归的情况下在斐波那契中所做的一样。
public class FinnonnacciDemo2 {
static int no = 0, n = 8;
public static void main(String[] args) {
// This will print series till 8
fib(0, 1);
}
public static void fib(int a, int b) {
// Terminating condition.
if (a >= n) {
return;
}
else {
System.out.print("\t" + no);
no = a + b;
a = b;
b = no;
fib(a, b);
}
}
}
回答by Carni
The Fibonacci series either starts with zero or with 1, non of which options has the 3 as the third number.
斐波那契数列要么以 0 开头,要么以 1 开头,其中没有选项将 3 作为第三个数字。
1 1 2 3 5 ...
1 1 2 3 5 ...
0 1 1 2 3 5 8 ...
0 1 1 2 3 5 8 ...
The usual way is to have the second option but starting at index 0 so that fib(0) = 0, fib(1) = fib(2) = 1, as stated for example here: http://oeis.org/A000045
通常的方法是使用第二个选项,但从索引 0 开始,使 fib(0) = 0, fib(1) = fib(2) = 1,例如此处所述:http: //oeis.org/A000045
回答by Milaci
Is a Recursion method... fib(3)=fib(3-1)+fib(3-2)=
fib(2)+fib(1)=(fib(2-1)+fib(1-1))+fib(1)=
fib(1)+fib(0)+fib(1)=
1 + 0 + 1
是递归方法吗... fib(3)=fib(3-1)+fib(3-2)=
fib(2)+fib(1)=(fib(2-1)+fib(1-1))+fib(1)=
fib(1)+fib(0)+fib(1)=
1 + 0 + 1
回答by januprasad
This is a much simpler code to generate Fibonacci sequence like '0 1 1 2 3 ...'.
这是一个更简单的代码来生成像'0 1 1 2 3 ...'这样的斐波那契数列。
public static void main (String[] args) {
int f = 0;
int g = 1;
for (int i = 1; i <= 10; i++) {
System.out.print(f + " ");
f = f + g;
g = f - g;
}
System.out.println();
}
回答by Imtiaz Zaman Nishith
Fibonacci numbers are the calculated sum of previous consecutive two numbers. It starts with 0 1 1 2 3 5 8... and so on. So its something like--
斐波那契数是前两个连续数的计算总和。它以 0 1 1 2 3 5 8... 开头,依此类推。所以它就像 -
fib(3) = fib(2) + fib(1)
= fib(1) + fib(0) + 1 //fib(1) = 1
= 1 + 0 + 1 //fib(0) = 0
= 2
Recursion method is less efficient as it involves function calls which uses stack, also there are chances of stack overflow if function is called frequently for calculating larger Fibonacci numbers.
递归方法效率较低,因为它涉及使用堆栈的函数调用,如果频繁调用函数来计算更大的斐波那契数,也有可能发生堆栈溢出。
回答by Amandeep Kamboj
F(n)
/ \
F(n-1) F(n-2)
/ \ / \
F(n-2) F(n-3) F(n-3) F(n-4)
/ \
F(n-3) F(n-4)
Notice that many computations are repeated! Important point to note is this algorithm is exponential because it does not store the result of previous calculated numbers. eg F(n-3) is called 3 times.
请注意,许多计算是重复的!需要注意的重要一点是这个算法是指数的,因为它不存储先前计算的数字的结果。例如 F(n-3) 被调用 3 次。
For more details refer algorithm by dasgupta chapter 0.2
有关更多详细信息,请参阅 dasgupta 第 0.2 章的算法
回答by codebusta
public static int f(int n){
if (n <= 1) {
return n;
} else {
return f(n - 1) + f(n - 2);
}
}
public static void main(String[] args){
Integer c = 4;
Integer answer = f(c);
System.out.println("Fibonacci " + c + "'s number is: " + answer);
}
回答by Mcfaith
import java.util.Scanner;
public class Fibonacci2 {
public static void main(String[]args){
int a;
try (Scanner sc = new Scanner(System.in)) {
System.out.print("Number of Fibonacci numbers to print: ");
a = sc.nextInt();
sc.close();
}
int c=1; /*c current number b last number*/
int b=0;
System.out.println(b);
System.out.println(c);
int bb;
for (int z = 2; z < a ; z++){
bb = b;
b = c;
c = bb + b; /*bb last last number*/
System.out.println(z);
}
}
}