Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class FibNum {
- public static int fib(int n) {
- if (n <= 2) {
- return n;
- }
- int fNum = fib(n - 1) + fib(n - 2);
- return fNum;
- }
- public static int firstNDigit(int n) {
- if (n == 1) {
- return 1;
- }
- for (int number = 1; number <= 40000; number++) {
- int fibNumber = fib(number);
- String str = Integer.toString(fibNumber);
- if (str.length() == n) {
- return number + 1;
- }
- }
- return 1;
- }
- public static void main(String[] args) {
- int n = 2;
- System.out.println(firstNDigit(n));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement