Monday, 6 April 2015

Interesting Problem : To Find nth NON-FIBONACCI NUMBER

First Few Non-Fibonacci nos. are : 4,6,7,9,10,11,12..........................


This is very similar to generating fibonacci numbers.Initialize your loop with the 3 initial fibonacci numbers a=1,b=1,c=2.Now , whenever this loop iterates, this tuple moves on to the next tuple of fibonacci numbers.(a=1,b=2,c=3).At every iteration, the number of non-fibonacci numbers jumped over is c-b+1.Keep adding this count as long as it is less than NWhen this count exceeds N, it means that we just crossed the Nth Non-Fibonacci number on the numberLine. So if we move back the required numbers (the number by which count exceeds N), we will get the answer.

Complexity: O(log n)

              a=1 b=2 c=3
              while(n>0)
              {
               a=b;
               b=c;
               c=a+b;
               n-=(c-b-1);
              }
              n+=(c-b-1);
              ans=b+n;

3 comments:

  1. How did you derived whole formula?????

    ReplyDelete
  2. nice concept!!! very helpful...

    ReplyDelete
  3. very very thanks dear , how have you derived this formula?

    ReplyDelete