public class Test{ private int total =1; private static int a = 0; private static int b = 1; public static void main(String[] args){ Test c = new Test(); c.calcMethod(a, b, 40); } public void calcMethod(int i, int j, int count) { if(a==0){ System.out.println(1); } if (total <= count) { b = i + j; a = j; total++; System.out.println(b); calcMethod(a, b, count); } }
}
public class Test { public static void main(String[] args) throws Exception { System.out.println(Test.circulate(40)); System.out.println(Test.recursive(40)); }
public static int circulate(int num) { int value1 = 1; int value2 = 1; int temp = value1 + value2; for (int i = 3; i <= num; i++) { temp = value1 + value2; value1 = value2; value2 = temp; } return temp; }
public static int recursive(int num) { if (num > 2) { return recursive(num - 1) + recursive(num - 2); } if (num <= 2) { return 1; } return 0; } }望改正!
递归: 代码如下: public class Fibonacci { public static void main(String args[]) { System.out.println(f(40)); } public static int f(int n) { if(n == 1 || n == 2) { return 1; }else { return f(n - 1) + f(n - 2); } } }
改一下递归的:public static void recursive(int i, int j, int num) { if (total <= num) { total++; if (total == num) { System.out.println(i + j); } recursive(j, i + j, num); } }
1L和3L的看的有点迷糊. 4L的容易理解一些. - - 多谢楼上各位.
不知道还有没有简单点的for方法.
再来个数学方法的:public class Test { public static void main(String[] args) { long n = fibonacci(40); System.out.println("F(40) = " + n); }
public static long fibonacci(int month) { if(month < 1) { return 0; } double sqrt5 = Math.sqrt(5); double a = Math.pow((1 + sqrt5) / 2, month); double b = Math.pow((1 - sqrt5) / 2, month); double r = (sqrt5 / 5) * (a - b); return Math.round(r); } }数学公式:
public class Test{
private int total =1;
private static int a = 0;
private static int b = 1;
public static void main(String[] args){
Test c = new Test();
c.calcMethod(a, b, 40);
}
public void calcMethod(int i, int j, int count) {
if(a==0){
System.out.println(1);
}
if (total <= count) {
b = i + j;
a = j;
total++;
System.out.println(b);
calcMethod(a, b, count);
}
}
}
public class Test {
public static void main(String[] args) throws Exception {
System.out.println(Test.circulate(40));
System.out.println(Test.recursive(40));
}
public static int circulate(int num) {
int value1 = 1;
int value2 = 1;
int temp = value1 + value2;
for (int i = 3; i <= num; i++) {
temp = value1 + value2;
value1 = value2;
value2 = temp;
}
return temp;
}
public static int recursive(int num) {
if (num > 2) {
return recursive(num - 1) + recursive(num - 2);
}
if (num <= 2) {
return 1;
}
return 0;
}
}望改正!
代码如下:
public class Fibonacci {
public static void main(String args[]) {
System.out.println(f(40));
}
public static int f(int n) {
if(n == 1 || n == 2) {
return 1;
}else {
return f(n - 1) + f(n - 2);
}
}
}
if (total <= num) {
total++;
if (total == num) {
System.out.println(i + j);
}
recursive(j, i + j, num);
}
}
4L的容易理解一些.
- -
多谢楼上各位.
long n = fibonacci(40);
System.out.println("F(40) = " + n);
}
public static long fibonacci(int month) {
if(month < 1) {
return 0;
}
double sqrt5 = Math.sqrt(5);
double a = Math.pow((1 + sqrt5) / 2, month);
double b = Math.pow((1 - sqrt5) / 2, month);
double r = (sqrt5 / 5) * (a - b);
return Math.round(r);
}
}数学公式:
for(i=3;i<=40;i++){
c=a+b;
a=b;
b=c; }System.out.println(c);
public static void main(String args[]) {
for(int i=1; i<=40; i++) {
System.out.println("第"+i+"个="+fib(i));
}
}
public static int fib(int n) {
if(n == 1||n == 2) {
return 1;
}
else
return fib(n-1)+fib(n-2);
}
}第1个=1
第2个=1
第3个=2
第4个=3
第5个=5
第6个=8
第7个=13
第8个=21
第9个=34
第10个=55
第11个=89
第12个=144
第13个=233
第14个=377
第15个=610
第16个=987
第17个=1597
第18个=2584
第19个=4181
第20个=6765
第21个=10946
第22个=17711
第23个=28657
第24个=46368
第25个=75025
第26个=121393
第27个=196418
第28个=317811
第29个=514229
第30个=832040
第31个=1346269
第32个=2178309
第33个=3524578
第34个=5702887
第35个=9227465
第36个=14930352
第37个=24157817
第38个=39088169
第39个=63245986
第40个=102334155