Java wariacje z ciągiem Fibonacciego

Zadanie

Utwórz rekurencyjna metode obliczajaca ciag Fibonacciego, zdefiniowany dla elementu fibonacci(n) jako sume fibonacci(n-1) + fibonacci(n-2) przy załozeniu, ze fibonacci(1) i fibonacci(2) maja odpowiednio wartosci 1 i 2.

Ciąg Fibonaciego

In mathematics, the Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and characterized by the fact that every number after the first two is the sum of the two preceding ones:[1][2]

1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 , 89 , 144 , …

Often, especially in modern usage, the sequence is extended by one more initial term:

0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 , 89 , 144 , …[3]

The Fibonacci spiral: an approximation of the golden spiral created by drawing circular arcs connecting the opposite corners of squares in the Fibonacci tiling;[4] this one uses squares of sizes 1, 1, 2, 3, 5, 8, 13, 21, and 34.

By definition, the first two numbers in the Fibonacci sequence are either 1 and 1, or 0 and 1, depending on the chosen starting point of the sequence, and each subsequent number is the sum of the previous two.

In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation

{\displaystyle F_{n}=F_{n-1}+F_{n-2},}

with seed values[1][2]

  {\displaystyle F_{1}=1,\;F_{2}=1}

or[5]

{\displaystyle F_{0}=0,\;F_{1}=1.}

The Fibonacci sequence is named after Italian mathematician Leonardo of Pisa, known as Fibonacci. His 1202 book Liber Abaci introduced the sequence to Western European mathematics,[6] although the sequence had been described earlier as Virahanka numbers in Indian mathematics.[7][8][9] The sequence described in Liber Abaci began with F1 = 1.

Fibonacci numbers are closely related to Lucas numbers  L_{n} in that they form a complementary pair of Lucas sequencesU_{n}(1,-1)=F_{n} and V_{n}(1,-1)=L_{n}. They are intimately connected with the golden ratio; for example, the closest rational approximations to the ratio are 2/1, 3/2, 5/3, 8/5, … .

Fibonacci numbers appear unexpectedly often in mathematics, so much so that there is an entire journal dedicated to their study, the Fibonacci Quarterly. Applications of Fibonacci numbers include computer algorithms such as the Fibonacci search technique and the Fibonacci heap data structure, and graphs called Fibonacci cubes used for interconnecting parallel and distributed systems. They also appear in biological settings,[10] such as branching in trees, phyllotaxis (the arrangement of leaves on a stem), the fruit sprouts of a pineapple,[11] the flowering of an artichoke, an uncurling fern and the arrangement of a pine cone‘s bracts.[12]

— Source: https://en.wikipedia.org/wiki/Fibonacci_number

Closed form

Fib(n) = Phin − (−Phi)−n  = Phin − ( −phi)n
√5 √5

Java program and others interesting links

Sources

/*
 * Example program to find fibonacci numbers
 */
package Numbers;

import static java.lang.Math.sqrt;
import java.util.Scanner;

/**
 *
 * @author KPreiskorn
 */
public class Fibonacci {

  /**
   * @param args the command line arguments
   */
  @SuppressWarnings("resource")
  public static void main(String args[]) {

    // input to print Fibonacci series upto how many numbers
    log("Enter number n Fibonacci to print: ");
    int number = new Scanner(System.in).nextInt();

    log("\nUsing Method-1: Using Recursion. Provided Number: " + number);
    // printing Fibonacci series upto number
    for (int i = 1; i <= number; i++) {
      System.out.printf("%3d %6d\n", i, fibonacciRecusion(i));
    }

    log("\nMethod-2: Fibonacci number at location " + number + " is ==> " + (fibonacciLoop(number) + ""));

    System.out.printf("\nMethod #3 (fibonacciClosedForm): Fibonacci number at location %d is %.0f\n\n", number, fibonacciClosedForm(number));

  }

  // Method-1: Java program for Fibonacci number using recursion.
  public static int fibonacciRecusion(int number) {
    if (number == 1 || number == 2) {
      return 1;
    }

    return fibonacciRecusion(number - 1) + fibonacciRecusion(number - 2); // tail recursion
  }

  // Method-2: Java program for Fibonacci number using Loop.
  public static int fibonacciLoop(int number) {
    if (number == 1 || number == 2) {
      return 1;
    }
    int fibo1 = 1, fibo2 = 1, fibonacci = 1;
    for (int i = 3; i <= number; i++) {
      fibonacci = fibo1 + fibo2; // Fibonacci number is sum of previous two Fibonacci number
      fibo1 = fibo2;
      fibo2 = fibonacci;

    }
    return fibonacci; // Fibonacci number
  }

  // metod 3: Closed-form expression
  public static double fibonacciClosedForm(int n) {
    double q = (1 + sqrt(5)) / 2;

    return Math.floor(Math.pow(q, n) / sqrt(5) + 1 / 2); // Fibonacci number
  }

  private static void log(String number) {
    System.out.println(number);

  }

}

 

 

Leave a Reply