📜  用Java打印Fibonacci系列的3种不同方法

📅  最后修改于: 2021-04-22 01:53:32             🧑  作者: Mango

给定数字N ,我们需要找到N项以内的斐波那契级数。

例子:

方法1 –迭代:将第一个和第二个数字初始化为0和1 。然后,我们打印第一个和第二个数字。然后,将流发送到迭代while循环,在该循环中,我们通过将前两个数字相加来获得下一个数字,同时将第一个数字与第二个交换,并将第二个与第三个交换。

下面是上述方法的实现:

Java
// Java program for the above approach
  
class GFG {
  
    // Function to print N Fibonacci Number
    static void Fibonacci(int N)
    {
        int num1 = 0, num2 = 1;
  
        int counter = 0;
  
        // Iterate till counter is N
        while (counter < N) {
  
            // Print the number
            System.out.print(num1 + " ");
  
            // Swap
            int num3 = num2 + num1;
            num1 = num2;
            num2 = num3;
            counter = counter + 1;
        }
    }
  
    // Driver Code
    public static void main(String args[])
    {
        // Given Number N
        int N = 10;
  
        // Function Call
        Fibonacci(N);
    }
}


Java
// Recursive implementation of
// Fibonacci Series
  
class GFG {
  
    // Function to print the fibonacci series
    static int fib(int n)
    {
        // Base Case
        if (n <= 1)
            return n;
  
        // Recursive call
        return fib(n - 1)
            + fib(n - 2);
    }
  
    // Driver Code
    public static void
    main(String args[])
    {
        // Given Number N
        int N = 10;
  
        // Print the first N numbers
        for (int i = 0; i < N; i++) {
  
            System.out.print(fib(i) + " ");
        }
    }
}


Java
// Dynamic Programming approach for
// Fibonacci Series
  
class fibonacci {
  
    // Function to find the fibonacci Series
    static int fib(int n)
    {
  
        // Declare an array to store
        // Fibonacci numbers.
        // 1 extra to handle case, n = 0
        int f[] = new int[n + 2];
  
        int i;
  
        // 0th and 1st number of
        // the series are 0 and 1
        f[0] = 0;
        f[1] = 1;
  
        for (i = 2; i <= n; i++) {
  
            // Add the previous 2 numbers
            // in the series and store it
            f[i] = f[i - 1] + f[i - 2];
        }
  
        // Nth Fibonacci Number
        return f[n];
    }
  
    public static void
    main(String args[])
    {
        // Given Number N
        int N = 10;
  
        // Print first 10 term
        for (int i = 0; i < N; i++)
            System.out.print(fib(i) + " ");
    }
}


输出:
0 1 1 2 3 5 8 13 21 34

时间复杂度: O(N)
辅助空间: O(1)

方法2 –使用递归由于斐波那契数是前两个数的和。我们可以根据以下条件使用递归:

  1. 获取需要计算斐波那契数列的数字。
  2. 从值N递归迭代到1:
    • 基本情况:如果递归调用该值小于1,则返回1的函数。
    • 递归调用:如果不满足基本条件,则递归调用前两个值,如:
    • 返回语句:在每个递归调用(基本情况除外)中,返回前两个值的递归函数为:

下面是上述方法的实现:

Java

// Recursive implementation of
// Fibonacci Series
  
class GFG {
  
    // Function to print the fibonacci series
    static int fib(int n)
    {
        // Base Case
        if (n <= 1)
            return n;
  
        // Recursive call
        return fib(n - 1)
            + fib(n - 2);
    }
  
    // Driver Code
    public static void
    main(String args[])
    {
        // Given Number N
        int N = 10;
  
        // Print the first N numbers
        for (int i = 0; i < N; i++) {
  
            System.out.print(fib(i) + " ");
        }
    }
}
输出:
0 1 1 2 3 5 8 13 21 34

时间复杂度: O(2 N )
辅助空间: O(1)

方法3 –使用动态编程通过存储到目前为止计算出的斐波那契数,我们可以避免方法2中的重复工作。步骤如下:

  1. 创建一个大小为N的数组arr []
  2. 初始化arr [0] = 0,arr [1] = 1。
  3. 遍历[2,N]并将数组arr []更新为:
  4. 打印arr [N]的值。

下面是上述方法的实现:

Java

// Dynamic Programming approach for
// Fibonacci Series
  
class fibonacci {
  
    // Function to find the fibonacci Series
    static int fib(int n)
    {
  
        // Declare an array to store
        // Fibonacci numbers.
        // 1 extra to handle case, n = 0
        int f[] = new int[n + 2];
  
        int i;
  
        // 0th and 1st number of
        // the series are 0 and 1
        f[0] = 0;
        f[1] = 1;
  
        for (i = 2; i <= n; i++) {
  
            // Add the previous 2 numbers
            // in the series and store it
            f[i] = f[i - 1] + f[i - 2];
        }
  
        // Nth Fibonacci Number
        return f[n];
    }
  
    public static void
    main(String args[])
    {
        // Given Number N
        int N = 10;
  
        // Print first 10 term
        for (int i = 0; i < N; i++)
            System.out.print(fib(i) + " ");
    }
}
输出:
0 1 1 2 3 5 8 13 21 34

时间复杂度: O(N)
辅助空间: O(N)