Advertisement
tomdodd4598

Untitled

Sep 19th, 2021
879
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.56 KB | None | 0 0
  1. public class FibNum {
  2.    
  3.     public static int fib(int n) {
  4.         if (n <= 2) {
  5.             return n;
  6.         }
  7.        
  8.         int fNum = fib(n - 1) + fib(n - 2);
  9.         return fNum;
  10.     }
  11.    
  12.     public static int firstNDigit(int n) {
  13.         if (n == 1) {
  14.             return 1;
  15.         }
  16.        
  17.         for (int number = 1; number <= 40000; number++) {
  18.            
  19.             int fibNumber = fib(number);
  20.             String str = Integer.toString(fibNumber);
  21.            
  22.             if (str.length() == n) {
  23.                 return number + 1;
  24.             }
  25.         }
  26.         return 1;
  27.     }
  28.    
  29.     public static void main(String[] args) {
  30.         int n = 2;
  31.        
  32.         System.out.println(firstNDigit(n));
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement