Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6702031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:00:18+00:00 2026-05-26T07:00:18+00:00

I am trying to solve a bigger problem, and I think that an important

  • 0

I am trying to solve a bigger problem, and I think that an important part of the program is spent on inefficient computations.

I need to compute for a given number N, the interval [P, Q], where P is the biggest fibonacci number that is <= to N, and Q is the smallest fibonacci number that is >= to N.

Currently, I am using a map to record the value of the fibonacci numbers.
A query normally involves searching all the fibonacci numbers up to N, and it is not very time efficient, as it involves a big number of comparisons.

This type of queries will occur quite often in my program, and I am interested in ways that I could improve the lookup, preferably with sub-linear complexity.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T07:00:18+00:00Added an answer on May 26, 2026 at 7:00 am

    The Fibonacci numbers are given by Binet’s formula

    F(n) = ( phi^n - (1-phi)^n ) / \sqrt{5}
    

    where phi is the golden ratio,

    phi = (1 + \sqrt{5}) / 2. 
    

    This can be implemented straightforwardly (Python example):

    <<fibonacci_binet.py>>=
    phi = (1 + 5**0.5) / 2
    
    def fib(n):
        return int(round((phi**n - (1-phi)**n) / 5**0.5))
    

    Because of floating-point rounding errors, this will however only give the right result for n < 70.

    Binet’s formula can be inverted by ignoring the (1-phi)^n term, which disappears for large n. We can therefore define the inverse Fibonacci function that, when given F(n), returns n (ignoring that F(1) = F(2)):

    <<fibonacci_binet.py>>=
    from math import log
    
    def fibinv(f):
        if f < 2:
            return f
        return int(round(log(f * 5**0.5) / log(phi)))
    

    Here rounding is used to our advantage: it removes the error introduced by our modification to Binet’s formula. The function will in fact return the right answer when given any Fibonacci number that can be stored as an exact integer in the computer’s memory. On the other hand, it does not verify that the given number actually is a Fibonacci number; inputting a large Fibonacci number or any number close to it will give the same result. Therefore you can use this idea to find the Fibonacci number closest to a given number.

    The idea, then is to apply the inverse Fibonacci map to find N and M, the two closest Fibonacci numbers on either side, then use the direct Fibonacci map to compute P = F(N) and Q = F(M). This involves more computation, but less searching.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to solve the 3n+1 problem and I have a for loop that
I'm trying to solve the following problem: I have a download button that when
Trying to solve this problem: I have the following set of divs that when
So I'm trying to solve this problem that asks to look for palindromes in
Trying to solve that problem, but no luck for hours... I have var screen1
Trying to solve a problem with templatetags. I have two templatetags: @register.inclusion_tag('directory/_alphabet.html') def alphabet_list(names):
Trying to solve this problem . I would like to learn how the bootstrapper
In trying to solve the ajax back button problem I have found the Really
I was trying to solve my XNA Font problem , when I found this
I'm trying to solve this flickering problem on the iphone (open gl es game).

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.