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

  • Home
  • SEARCH
  • 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 8607123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:16:15+00:00 2026-06-12T03:16:15+00:00

Possible Duplicate: Handling very large numbers in Python I have a python function to

  • 0

Possible Duplicate:
Handling very large numbers in Python

I have a python function to generate fibonacci numbers:

def fib(n):                                                                                                            
        return ((1+math.sqrt(5))**n - (1-math.sqrt(5))**n)/(2**n*math.sqrt(5))

I can feed the fib function numbers up to 700, where it starts to

OverflowError: (34, 'Numerical result out of range')

Do I need to use a special type like long to get around this?

  • 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-06-12T03:16:16+00:00Added an answer on June 12, 2026 at 3:16 am

    The problem is that you’re using doubles to calculate the value and the doubles are overflowing.
    Doubles give exact solutions only to about the 85th Fibonacci number.

    If you want a fast and accurate calculation you’re better off using an algorithm based on a better recurrence relationship, and using python bignum integers.

    In particular you can use:

     fib(2*n) = fib(n)^2 + fib(n-1)^2
     fib(2*n-1) = fib(n)*(2*fib(n-1)+fib(n))
    

    Or the equivalent matrix exponentiation formula (excuse the ugly formatting)

     [ F_n     F_{n-1} ]      [ 1   1 ] ^N 
     [                 ]  =   [       ]
     [ F_{n-1} F_{n-2} ]      [ 1   0 ]
    

    Both of these result in algorithms that require O(log(N)) calculations rather than O(N).

    Here’s a complete solution in pseudo-code


    If you do want to perform your calculations using doubles and the explicit formulae, then the formulae can be tweaked to give something faster that doesn’t overflow until about the 1500th fibonacci number, and remains the same accuracy as your version. IIRC it is:

    def fib(n):                                                                                                            
        return round( ((1+math.sqrt(5))/2)**n / math.sqrt(5) )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Error handling in C code Let's say you have a function: int
Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: Error handling in C code What return value should you use for
Possible Duplicate: Using global exception handling with “setUncaughtExceptionHandler” and “Toast” I have implemented UncaughtExceptionHandler
Possible Duplicate: iphone/ipad orientation handling i beg your pardon. i am very new in
Possible Duplicate: Python urllib2 Progress Hook I have a script which uploads a file
Possible Duplicate: Handling a Click for all controls on a Form I have a
Possible Duplicate: Handling \r\n vs \n newlines in python on Mac vs Windows I'm
Possible Duplicate: Handling InterruptedException in Java I wonder how InterruptedException should be properly handled.
Possible Duplicate: Python : how to append new elements in a list of list?

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.