Fibonacci sequence, also known as the golden segmentation sequence, factor mathematician Leonardoda Fibonacci [1] ) was introduced with rabbit breeding as an example, so it is also called "rabbit sequence", which refers to a sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... In mathematics, the Fibonacci sequence is defined by the following recursive method: F(0)=0, F(1)=1, F(n)=F(n-1)+F(n-2)(n≥2, n∈N*) In modern physics, quasicrystal structure, chemistry and other fields, Fibonacci sequences have direct applications. For this reason, the American Mathematical Society has published a mathematical journal named "Fibonacci Sequence Quarterly" since 1963 to specifically publish research results in this area.
Below I implement the different ways of recursion and non-recursion in JAVA language:
public class Feibonacii { //Use recursive methods to implement Fibonacci sequence public static int feibonacci1(int n){ if(n==0){return 0;} if(n==1){return 1;} return feibonacci1(n-1)+feibonacci1(n-2); } //Use non-recursive methods to implement Fibonacci sequence public static int feibonacci2(int n){ int arr[] = new int[n+1]; arr[0]=0; arr[1]=1; for(int i=2;i<=n;i++){ arr[i] = arr[i-1]+arr[i-2]; } return arr[n]; } public static void main(String[] args) { for(int i=40;i<=45;i++){ System.out.println("feibonaci1 i="+i+",vaule="+feibonaci1(i)); } for(int i=40;i<=45;i++){ System.out.println("feibonaci2 i="+i+",vaule="+feibonaci2(i)); } }}It is obvious that the recursive method 43 is executed relatively slowly after execution, while the non-recursive method execution is quite fast.
analyze:
(1) Java uses methods to recursively implement the Fibonacci sequence. Feibonaci1 (45) is executed once. Java executes the method feibonaci1 with 2^44+2^43+...+2^1+1 times. feibonaci2(45), the method is only executed once, but the number of calculations is the same as that of feibonaci1.
Conclusion: JAVA describes Fibonacci sequences, which are more suitable for calculation using non-recursive methods.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.