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 3486954
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:05:34+00:00 2026-05-18T11:05:34+00:00

I was sent this nice non-recursive function for computing a fibonacci sequence. So I

  • 0

I was sent this nice non-recursive function for computing a fibonacci sequence.

alt text

So I coded up a bit of c# and was able to verify all numbers up to 1474 were correct.

The problem comes in when attempting to calculate it for 1475 and above. My c# math skills just aren’t up to the task of figuring out a different way. So, does someone have a better way of expressing this particular math function in c#? other than the traditional way of doing a recursive function?

Incidentally, I started to use BigInteger as the return type. But the problem really comes in when trying to raise (1+Math.Sqrt(5) /2) to the 1475th power. I just don’t see what data type I need (nor mechanism for that matter) to get this to come back with something other than Infinity.

Here’s a starting point.

private Double FibSequence(Int32 input) {
    Double part1 = (1 / Math.Sqrt(5));
    Double part2 = Math.Pow(((1 + Math.Sqrt(5)) / 2), input);
    Double part3 = Math.Pow(((1 - Math.Sqrt(5)) / 2), input);

    return (part1 * part2) - (part1 * part3);
}

And, no, it’s not homework. Just a “simple” problem for a slow day.

  • 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-18T11:05:35+00:00Added an answer on May 18, 2026 at 11:05 am

    I don’t think C# has a data type with enough floating precision and range to handle this naïvely.

    If you really want to go down this path, you can notice that the conjugate \Phi=\phi^{-1}=\phi-1=\frac{-1+\sqrt 5}{2} is less than one, so -\frac{(-\Phi)^n}{\sqrt 5} does the same as rounding to the nearest integer, thus you can simplify your solution to finding \left\lfloor\frac{\phi^n}{\sqrt 5}+\frac12\right\rfloor. Then use binomial expansion so that you only have to calculate \left\lfloor a+b\sqrt 5\right\rfloor, with appropriate a and b (which are rational and can be computed exactly with BigInteger). If you still go back to Double for this, you still won’t get much further than 1475, but you should be able to figure out how to do even this part with exact integer math only ☺

    \frac{\phi^n}{\sqrt 5}=\frac{(1+\sqrt 5)^n}{2^n\sqrt 5}=\frac{\sum_{k=0}^n{n\choose k}\sqrt 5^k}{2^n\sqrt 5}
    =\left(\sum_{k=0}^{\left\lfloor\frac{n-1}2\right\rfloor}\frac{{n\choose 2k+1}5^k}{2^n}\right)+\left(\sum_{k=0}^{\left\lfloor\frac n2\right\rfloor}\frac{{n\choose 2k}5^{k-1}}{2^n}\right)\sqrt 5


    There’s another neat method for computing Fibonacci numbers, using matrix exponentiation:

    \left(\begin{matrix}1&1\1&0\end{matrix}\right)^n=\left(\begin{matrix}F_n&F_{n-1}\F_{n-1}&F_{n-2}\end{matrix}\right)

    This can be done in O(log n) if you’re clever.


    I ended up implementing these in Haskell. fib1 is matrix exponentiation and fib2 is exact integer translation of the closed-form formula, as described above. Their respective runtimes look like this, as measured by Criterion when compiled by GHC 7.0.3:
    Matrix exponentiation runtime Closed-form runtime

    import Control.Arrow
    import Data.List
    import Data.Ratio
    
    newtype Matrix2 a = Matrix2 (a, a, a, a) deriving (Show, Eq)
    instance (Num a) => Num (Matrix2 a) where
        Matrix2 (a, b, c, d) * Matrix2 (e, f, g, h) =
            Matrix2 (a*e+b*g, a*f+b*h, c*e+d*g, c*f+d*h)
        fromInteger x = let y = fromInteger x in Matrix2 (y, 0, 0, y)
    fib1 n = let Matrix2 (_, x, _, _) = Matrix2 (1, 1, 1, 0) ^ n in x
    
    binom n =
        scanl (\a (b, c)-> a*b `div` c) 1 $
        takeWhile ((/=) 0 . fst) $ iterate (pred *** succ) (n, 1)
    evens (x:_:xs) = x : evens xs
    evens xs = xs
    odds (_:x:xs) = x : odds xs
    odds _ = []
    iterate' f x = x : (iterate' f $! f x)
    powers b = iterate' (b *) 1
    esqrt e n = x where
        (_, x):_ = dropWhile ((<=) e . abs . uncurry (-)) $ zip trials (tail trials)
        trials = iterate (\x -> (x + n / x) / 2) n
    fib' n = (a, b) where
        d = 2 ^ n
        a = sum (zipWith (*) (odds $ binom n) (powers 5)) % d
        b = sum (zipWith (*) (evens $ binom n) (powers 5)) % d
    fib2 n = numerator r `div` denominator r where
        (a, b) = fib' n
        l = lcm (denominator a) (denominator a)
        r = a + esqrt (1 % max 3 l) (b * b / 5) + 1 % 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably an simple problem but would someone nice be able to give
This is what my browser sent, when logging into some site: POST http://www.some.site/login.php HTTP/1.0
This is my 'game server'. It's nothing serious, I thought this was a nice
A tester sent this crash file: Not sure what an EXC_CRASH is. UPDATE: I
I cannot get my addslashes function and html option value to play nice together.
I sent this question to ServerFault, but I'm wondering if it's really a programming
Someone just sent me a decompile of a program into C. It was a
File is sent to the client using Response.writefile and content diposition as inline. When
I am unable to consume messages sent via ActiveMQ from my Flex client. Sending
I am regularly required to compare data sent to me in Excel spreadsheets with

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.