hello, I am doing some code on calculating the fibonacci sequence in 2 different methods, one is through recursion and the other is through a for loop. And so, I am trying to figure out the max value or limit for each of these methods where the function/method does not result in stack overflow, and I am not sure how to go about this, here is my code so far:
import java.util.Scanner; public class Recursion { public static void main(String[] args) { Scanner kb = new Scanner(System.in); long n, result1, result2, startTime1, stopTime1, startTime2, stopTime2; System.out.println("Please Enter A Number in between 0 and maxValue, will be calcualtred using 2 methods...."); n = kb.nextLong(); startTime1 = System.nanoTime(); result1 = fibRecursive(n); stopTime1 = System.nanoTime(); startTime2 = System.nanoTime(); result2 = fibLoop(n); stopTime2 = System.nanoTime(); System.out.println("\nDisplaying solution for recursive method "+ result1 + "Time Taken: " + (stopTime1 - startTime1)); System.out.println("\nDisplaying solution for loop method "+ result2 + "Time Taken: " + (stopTime2 - startTime2)); System.out.println("\nThanks for using our fibnoacci calculator. "); } public static long fibRecursive(long i) { if(i == 0) return 0; else if(i == 1) return 1; else return fibRecursive(i - 1) + fibRecursive(i - 2); } public static long fibLoop(long k) { long a = 0, b = 1, ans = 0; for(int i = 1; i < k; i++) { ans = a + b; a = b; b = ans; } return ans; } }
is there perhaps a built in function/feature in java to determine this number? In my case I am looking for the max value for "long n". Any help is great appreciated, thanks.