write a function to calculate the fibonacchi numbers in java
- User
write a function to calculate the fibonacchi numbers in java
Assistant
Here is a function to calculate Fibonacci numbers in Java:
javapublic static int fibonacci(int n) { if (n <= 1) { return n; } int[] fibArray = new int[n+1]; fibArray[0] = 0; fibArray[1] = 1; for(int i=2; i<=n; i++) { fibArray[i] = fibArray[i-1] + fibArray[i-2]; } return fibArray[n]; }To use it:
javaint result = fibonacci(6); // returns 8How it works:
- It takes the index n as a parameter to calculate the nth Fibonacci number
- Base cases are 0th and 1st numbers which are 0 and 1
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.