You are viewing a single comment's thread. Return to all comments →
Java Soln
import java.util.*;
public class Solution { public static int minFibonacciWithNDigits(int digits) { if (digits == 1) return 1; // F1 = 1 has 1 digit
double phi = (1 + Math.sqrt(5)) / 2; double log10_phi = Math.log10(phi); double log10_sqrt5 = 0.5 * Math.log10(5); // Rearranged Binet's formula to solve for n int n = (int)Math.ceil((digits - 1 + log10_sqrt5) / log10_phi); return n; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); System.out.println(minFibonacciWithNDigits(n)); } }
}
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #25: N-digit Fibonacci number
You are viewing a single comment's thread. Return to all comments →
Java Soln
import java.util.*;
public class Solution { public static int minFibonacciWithNDigits(int digits) { if (digits == 1) return 1; // F1 = 1 has 1 digit
}