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 N. When 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;
How did you derived whole formula?????
ReplyDeletenice concept!!! very helpful...
ReplyDeletevery very thanks dear , how have you derived this formula?
ReplyDelete