網頁

2011年6月2日 星期四

Fibonacci數列

a[0]=0
a[1]=a[2]=1
a[n]=a[n-1]+a[n-2]

利用遞迴公式列出a[n] n=2....40
並比較下列公式







f[2]=1
f[3]=2
f[4]=3
f[5]=5
f[6]=8
f[7]=13
f[8]=21
f[9]=34
f[10]=55
f[11]=89
f[12]=144
f[13]=233
f[14]=377
f[15]=610
f[16]=987
f[17]=1597
f[18]=2584
f[19]=4181
f[20]=6765
f[21]=10946
f[22]=17711
f[23]=28657
f[24]=46368
f[25]=75025
f[26]=121393
f[27]=196418
f[28]=317811
f[29]=514229
f[30]=832040
f[31]=1346269
f[32]=2178309
f[33]=3524578
f[34]=5702887
f[35]=9227465
f[36]=14930352
f[37]=24157817
f[38]=39088169
f[39]=63245986
f[40]=102334155

20 則留言:

  1. import java.util.*;
    public class cc{
    public static double a(int n){
    double sqrt5=Math.sqrt(5);
    double a1=Math.pow((1+sqrt5)/2, n);
    double a2=Math.pow((1-sqrt5)/2, n);
    return (a1-a2)/sqrt5;
    }

    public static void f(int n){
    int f[]=new int [n+1];
    f[0]=0;f[1]=1;
    for (int i=2; i<=n; i++){
    f[i]=f[i-1]+f[i-2];
    System.out.print("f["+i+"]="+f[i]);
    System.out.println("\ta("+i+")="+a(i));

    }
    }
    public static void main(String[] args)throws Exception{
    Scanner input=new Scanner(System.in);
    int n=input.nextInt();
    f(n);
    }
    }

    回覆刪除
  2. import java.util.*;
    public class cc{
    public static void f(int n){
    int f[]=new int [n+1];
    f[0]=0;f[1]=1;
    for (int i=2; i<=n; i++){
    f[i]=f[i-1]+f[i-2];
    System.out.print("f["+i+"]="+f[i]);
    System.out.println("\t"+"a("+i+")="+a(i));
    }
    }
    public static double a(int n){
    double sqrt5=Math.sqrt(5);
    double a1=Math.pow((1+sqrt5)/2, n);
    double a2=Math.pow((1-sqrt5)/2, n);
    return (a1-a2)/sqrt5;
    }
    public static void main(String[] args)throws Exception{
    Scanner input=new Scanner(System.in);
    int n=input.nextInt();
    f(n);
    }
    }

    回覆刪除
  3. import java.util.*;
    public class cc{
    public static double a(int n){
    double sqrt5=Math.sqrt(5);
    double a1=Math.pow((1+sqrt5)/2, n);
    double a2=Math.pow((1-sqrt5)/2, n);
    return (a1-a2)/sqrt5;
    }
    public static void f(int n){
    int f[]=new int [n+1];
    f[0]=0;f[1]=1;
    for (int i=2; i<=n; i++){
    f[i]=f[i-1]+f[i-2];
    System.out.print("f["+i+"]="+f[i]);
    System.out.println("\ta("+i+")="+a(i));
    }
    }
    public static void main(String[] args)throws Exception{
    Scanner input=new Scanner(System.in);
    int n=input.nextInt();
    f(n);
    }
    }

    回覆刪除
  4. import java.util.*;
    public class abc{
    public static double a(int n){
    double sqrt5=Math.sqrt(5);
    double a1=Math.pow((1+sqrt5)/2, n);
    double a2=Math.pow((1-sqrt5)/2, n);
    return (a1-a2)/sqrt5;
    }
    public static void f(int n){
    int f[]=new int [n+1];
    f[0]=0;f[1]=1;
    for (int i=2; i<=n; i++){
    f[i]=f[i-1]+f[i-2];
    System.out.print("f["+i+"]="+f[i]);
    System.out.println("\ta("+i+")="+a(i));
    }
    }
    public static void main(String[] args)throws Exception{
    Scanner input=new Scanner(System.in);
    int n=input.nextInt();
    f(n);
    }
    }

    回覆刪除
  5. import java.util.*;
    public class celtics{
    public static double a(int n){
    double sqrt5=Math.sqrt(5);
    double a1=Math.pow((1+sqrt5)/2, n);
    double a2=Math.pow((1-sqrt5)/2, n);
    return (a1-a2)/sqrt5;
    }
    public static void f(int n){
    int f[]=new int [n+1];
    f[0]=0;f[1]=1;
    for (int i=2; i<=n; i++){
    f[i]=f[i-1]+f[i-2];
    System.out.print("f["+i+"]="+f[i]);
    System.out.println("\ta("+i+")="+a(i));
    }
    }
    public static void main(String[] args)throws Exception{
    Scanner input=new Scanner(System.in);
    int n=input.nextInt();
    f(n);
    }
    }

    回覆刪除
  6. import java.util.*;
    public class jjj{
    public static double a(int n){
    double sqrt5=Math.sqrt(5);
    double a1=Math.pow((1+sqrt5)/2, n);
    double a2=Math.pow((1-sqrt5)/2, n);
    return (a1-a2)/sqrt5;
    }
    public static void f(int n){
    int f[]=new int [n+1];
    f[0]=0;f[1]=1;
    for (int i=2; i<=n; i++){
    f[i]=f[i-1]+f[i-2];
    System.out.print("f["+i+"]="+f[i]);
    System.out.println("\ta("+i+")="+a(i));
    }
    }
    public static void main(String[] args)throws Exception{
    Scanner input=new Scanner(System.in);
    int n=input.nextInt();
    f(n);
    }
    }

    回覆刪除
  7. import java.util.*;
    public class ggggg{
    public static void f(int n){
    int f[]=new int [n+1];
    f[0]=0;f[1]=1;
    for (int i=2; i<=n; i++){
    f[i]=f[i-1]+f[i-2];
    System.out.print("f["+i+"]="+f[i]);
    System.out.println("\ta("+i+")="+a(i));
    }
    }
    public static double a(int n)
    {
    double sqrt5=Math.sqrt(5);
    double a1=Math.pow((1+sqrt5)/2, n);
    double a2=Math.pow((1-sqrt5)/2, n);
    return (a1-a2)/sqrt5;
    }

    public static void main(String[] args){
    Scanner input=new Scanner(System.in);
    int n=input.nextInt();
    f(n);
    }
    }

    回覆刪除
  8. import java.util.*;
    public class fibonacci3{
    public static double a(int n){
    double sqrt5=Math.sqrt(5);
    double a1=Math.pow((1+sqrt5)/2,n);
    double a2=Math.pow((1-sqrt5)/2,n);
    return(a1-a2)/sqrt5;
    }
    public static void f(int n){
    int f[]=new int[n+1];
    f[0]=0;f[1]=1;
    for(int i=2;i<=n;i++){
    f[i]=f[i-1]+f[i-2];
    System.out.println("f["+i+"]="+f[i]);
    System.out.println("\ta("+i+")="+a(i));
    }
    }
    public static void main(String[]args)throws Exception{
    Scanner input=new Scanner(System.in);
    int n=input.nextInt();
    f(n);
    }
    }

    回覆刪除
  9. import java.util.*;
    public class Fibonacci{
    public static double a(int n){
    double sqrt5=Math.sqrt(5);
    double a1=Math.pow((1+sqrt5)/2, n);
    double a2=Math.pow((1-sqrt5)/2, n);
    return (a1-a2)/sqrt5;
    }
    public static void f(int n){
    int f[]=new int [n+1];
    f[0]=0;f[1]=1;
    for (int i=2; i<=n; i++){
    f[i]=f[i-1]+f[i-2];
    System.out.print("f["+i+"]="+f[i]);
    System.out.println("\ta("+i+")="+a(i));
    }
    }
    public static void main(String[] args)throws Exception{
    Scanner input=new Scanner(System.in);
    int n=input.nextInt();
    f(n);
    }
    }

    回覆刪除
  10. import java.util.*;
    public class Fibonacci{
    public static double a(int n)
    {
    double sqrt5=Math.sqrt(5);
    double a1=Math.pow((1+sqrt5)/2,n);
    double a2=Math.pow((1-sqrt5)/2,n);
    return (a1-a2)/sqrt5;
    }
    public static void f(int n){
    int f[]=new int[n+1];
    f[0]=0;f[1]=1;
    for(int i=2; i<=n; i++){
    f[i]=f[i-1]+f[i-2];
    System.out.print("f["+i+"]="+f[i]);
    System.out.println("a("+1+")="+a(i));
    }
    }
    public static void main(String[] args)throws Exception{
    Scanner input=new Scanner(System.in);
    int n=input.nextInt();
    f(n);
    }
    }

    回覆刪除
  11. import java.util.*;
    public class Fibonacci{
    public static double a(int n){
    double sqrt5=Math.sqrt(5);
    double a1= Math.pow((1+sqrt5)/2,n);
    double a2= Math.pow((1-sqrt5)/2,n);
    return (a1-a2)/sqrt5;
    }

    public static void f(int n){
    int f[]=new int[n+1];
    f[0]=0;f[1]=1;
    for (int i=2; i<=n; i++){
    f[i]=f[i-1]+f[i-2];
    System.out.print("f["+i+"]="+f[i]);
    System.out.println("a("+1+")="+a(i));
    }
    }
    public static void main(String[] args)throws Exception{
    Scanner input=new Scanner(System.in);
    int n=input.nextInt();
    f(n);
    }
    }

    回覆刪除
  12. import java.util.*;
    public class fibonacci{
    public static double a(int n){
    double sqrt5=Math.sqrt(5);
    double a1=Math.pow((1+sqrt5)/2, n);
    double a2=Math.pow((1-sqrt5)/2, n);
    return (a1-a2)/sqrt5;
    }
    public static void f(int n){
    int f[]=new int [n+1];
    f[0]=0;f[1]=1;
    for (int i=2; i<=n; i++){
    f[i]=f[i-1]+f[i-2];
    System.out.print("f["+i+"]="+f[i]);
    System.out.println("\ta("+i+")="+a(i));
    }
    }
    public static void main(String[] args)throws Exception{
    Scanner input=new Scanner(System.in);
    int n=input.nextInt();
    f(n);
    }
    }

    回覆刪除
  13. import java.util.*;
    public class Fibonacci{
    public static double a(int n){
    double sqrt5=Math.sqrt(5);
    double a1=Math.pow((1+sqrt5)/2, n);
    double a2=Math.pow((1-sqrt5)/2, n);
    return (a1-a2)/sqrt5;
    }
    public static void f(int n){
    int f[]=new int [n+1];
    f[0]=0;f[1]=1;
    for (int i=2; i<=n; i++){
    f[i]=f[i-1]+f[i-2];
    System.out.print("f["+i+"]="+f[i]);
    System.out.println("\ta("+i+")="+a(i));
    }
    }
    public static void main(String[] args)throws Exception{
    Scanner input=new Scanner(System.in);
    int n=input.nextInt();
    f(n);
    }
    }

    回覆刪除
  14. import java.util.*;
    public class Fibonacci{
    public static double a(int n)
    {
    double sqrt5=Math.sqrt(5);
    double a1=Math.pow((1+sqrt5)/2,n);
    double a2=Math.pow((1-sqrt5)/2,n);
    return (a1-a2)/sqrt5;
    }
    public static void f(int n){
    int f[]=new int[n+1];
    f[0]=0;f[1]=1;
    for(int i=2; i&lt;=n; i++){
    f[i]=f[i-1]+f[i-2];
    System.out.print(&quot;f[&quot;+i+&quot;]=&quot;+f[i]);
    System.out.println(&quot;a(&quot;+1+&quot;)=&quot;+a(i));
    }
    }
    public static void main(String[] args)throws Exception{
    Scanner input=new Scanner(System.in);
    int n=input.nextInt();
    f(n);
    }
    }

    回覆刪除
  15. import java.util.*;
    public class Fibonacci{
    public static double a(int n)
    {
    double sqrt5=Math.sqrt(5);
    double a1=Math.pow((1+sqrt5)/2,n);
    double a2=Math.pow((1-sqrt5)/2,n);
    return (a1-a2)/sqrt5;
    }
    public static void f(int n){
    int f[]=new int[n+1];
    f[0]=0;f[1]=1;
    for(int i=2; i&lt;=n; i++){
    f[i]=f[i-1]+f[i-2];
    System.out.print(&quot;f[&quot;+i+&quot;]=&quot;+f[i]);
    System.out.println(&quot;a(&quot;+1+&quot;)=&quot;+a(i));
    }
    }
    public static void main(String[] args)throws Exception{
    Scanner input=new Scanner(System.in);
    int n=input.nextInt();
    f(n);
    }
    }

    回覆刪除
  16. import java.util.*;
    public class celtics{
    public static double a(int n){
    double sqrt5=Math.sqrt(5);
    double a1=Math.pow((1+sqrt5)/2,n);
    double a2=Math.pow((1-sqrt5)/2,n);
    return (a1-a2)/sqrt5;
    }
    public static void f(int n){
    int f[]=new int [n+1];
    f[0]=0;f[1]=1;
    for (int i=2; i<=n; i++){
    f[i]=f[i-1]+f[i-2];
    System.out.print("f["+i+"]="+f[i]);
    System.out.println("\ta("+i+")="+a(i));
    }
    }
    public static void main (String[]args)throws Exception{
    Scanner input=new Scanner(System.in);
    int n=input.nextInt();
    f(n);
    }
    }

    回覆刪除
  17. import java.util.*;
    public class y1{
    public static double a(int n)
    {
    double sqrt5=Math.sqrt(5);
    double a1=Math.pow((1+sqrt5)/2,n);
    double a2=Math.pow((1-sqrt5)/2,n);
    return (a1-a2)/sqrt5;
    }
    public static void f(int n){
    int f[]=new int[n+1];
    f[0]=0;f[1]=1;
    for(int i=2; i<=n; i++){
    f[i]=f[i-1]+f[i-2];
    System.out.print("f["+i+"]="+f[i]);
    System.out.println("a("+1+")="+a(i));
    }
    }
    public static void main(String[] args)throws Exception{
    Scanner input=new Scanner(System.in);
    int n=input.nextInt();
    f(n);
    }
    }

    回覆刪除
  18. import java.util.*;
    public class Fibonacci{
    public static double a(int n){
    double sqrt5=Math.sqrt(5);
    double a1=Math.pow((1+sqrt5)/2, n);
    double a2=Math.pow((1-sqrt5)/2, n);
    return (a1-a2)/sqrt5;
    }
    public static void f(int n){
    int f[]=new int [n+1];
    f[0]=0;f[1]=1;
    for (int i=2; i<=n; i++){
    f[i]=f[i-1]+f[i-2];
    System.out.print("f["+i+"]="+f[i]);
    System.out.println("\ta("+i+")="+a(i));
    }
    }
    public static void main(String[] args)throws Exception{
    Scanner input=new Scanner(System.in);
    int n=input.nextInt();
    f(n);
    }
    }

    回覆刪除
  19. import java.util.*;
    public class Fibonacci{
    public static double a(int n){
    double sqrt5=Math.sqrt(5);
    double a1= Math.pow((1+sqrt5)/2,n);
    double a2= Math.pow((1-sqrt5)/2,n);
    return (a1-a2)/sqrt5;
    }

    public static void f(int n){
    int f[]=new int[n+1];
    f[0]=0;f[1]=1;
    for (int i=2; i<=n; i++){
    f[i]=f[i-1]+f[i-2];
    System.out.print("f["+i+"]="+f[i]);
    System.out.println("a("+1+")="+a(i));
    }
    }
    public static void main(String[] args)throws Exception{
    Scanner input=new Scanner(System.in);
    int n=input.nextInt();
    f(n);
    }
    }

    回覆刪除
  20. import java.util.*;
    public class cc{
    public static double a(int n){
    double sqrt5=Math.sqrt(5);
    double a1=Math.pow((1+sqrt5)/2, n);
    double a2=Math.pow((1-sqrt5)/2, n);
    return (a1-a2)/sqrt5;
    }

    public static void f(int n){
    int f[]=new int [n+1];
    f[0]=0;f[1]=1;
    for (int i=2; i<=n; i++){
    f[i]=f[i-1]+f[i-2];
    System.out.print("f["+i+"]="+f[i]);
    System.out.println("\ta("+i+")="+a(i));

    }
    }
    public static void main(String[] args)throws Exception{
    Scanner input=new Scanner(System.in);
    int n=input.nextInt();
    f(n);
    }
    }

    回覆刪除

HTML 編輯器