Click here to Skip to main content
11,495,371 members (67,168 online)
Click here to Skip to main content

Tagged as

Fibonacci Without Loops or Recursion

, 17 Dec 2012 CPOL 10.4K 8
Rate this:
Please Sign up or sign in to vote.
A method for calculating a Fibonacci number without using loops or recursion.

Introduction

While reading one of our Insider News posts which linked to Evan Miller's site,  he mentioned a mathematical means of producing a Fibonacci number without using loops or recursion.   I decided to post the C# version of it here, but in no way do I claim credit to creating this.   I thought it was interesting enough to share for those who might not read the Insider News articles.  

You can read more about this closed-form solution on wiki.

The Code

public static long Fibonacci(long n)
{
    return (long)Math.Round(0.44721359549995682d * Math.Pow(1.6180339887498949d, n));
}   

NOTE: Due to limits of precision, the preceding formula is only accurate up to n = 77. 

UPDATE

Based on YvesDaoust's recommendation, I've updated the formula to use a simpler version of the closed form solution (also found on Wiki), as it proves to be faster and more compact.

Furthermore, I've adjusted the constants slightly to improve the function's accuracy.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Andrew Rissing
Software Developer (Senior)
United States United States
Since I've begun my profession as a software developer, I've learned one important fact - change is inevitable. Requirements change, code changes, and life changes.

So..If you're not moving forward, you're moving backwards.

Comments and Discussions

 
GeneralThoughts Pin
PIEBALDconsult8-Sep-13 14:55
professionalPIEBALDconsult8-Sep-13 14:55 
GeneralRe: Thoughts Pin
Andrew Rissing10-Sep-13 6:08
memberAndrew Rissing10-Sep-13 6:08 
Questionformula's missing part Pin
Member 33856988-Sep-13 12:30
memberMember 33856988-Sep-13 12:30 
AnswerRe: formula's missing part Pin
Andrew Rissing10-Sep-13 6:06
memberAndrew Rissing10-Sep-13 6:06 
GeneralRe: formula's missing part Pin
Yuksel YILDIRIM14-Oct-13 16:56
memberYuksel YILDIRIM14-Oct-13 16:56 
GeneralMy vote of 2 Pin
YvesDaoust17-Dec-12 2:57
memberYvesDaoust17-Dec-12 2:57 
QuestionCan do better Pin
YvesDaoust17-Dec-12 2:51
memberYvesDaoust17-Dec-12 2:51 
AnswerRe: Can do better Pin
Andrew Rissing17-Dec-12 5:48
memberAndrew Rissing17-Dec-12 5:48 
AnswerRe: Can do better Pin
Andrew Rissing17-Dec-12 6:04
memberAndrew Rissing17-Dec-12 6:04 
GeneralRe: Can do better [modified] Pin
YvesDaoust17-Dec-12 6:32
memberYvesDaoust17-Dec-12 6:32 
GeneralRe: Can do better Pin
Andrew Rissing17-Dec-12 13:04
memberAndrew Rissing17-Dec-12 13:04 
GeneralRe: Can do better Pin
YvesDaoust17-Dec-12 21:34
memberYvesDaoust17-Dec-12 21:34 
GeneralRe: Can do better Pin
Andrew Rissing18-Dec-12 5:24
memberAndrew Rissing18-Dec-12 5:24 
GeneralMy vote of 5 Pin
Manish Choudhary .NET expert14-Dec-12 19:50
memberManish Choudhary .NET expert14-Dec-12 19:50 
BugThis is correct only for n up to 70 Pin
Matt T Heffron13-Dec-12 13:12
memberMatt T Heffron13-Dec-12 13:12 
GeneralRe: This is correct only for n up to 70 Pin
Andrew Rissing13-Dec-12 13:45
memberAndrew Rissing13-Dec-12 13:45 
Interesting. I tested the values only up to 50, guess I was shooting too low there.

I'll double check somethings on the timing, but it looks like you're dead on.

I was initially worried that the overuse of Pow/Division/etc. would outweigh the cost of just looping through additions quickly. I guess my fears were valid and should have been checked.

I'll let you know otherwise if my timing shows something different.

Thanks for the post. Smile | :)
GeneralRe: This is correct only for n up to 70 Pin
Andrew Rissing17-Dec-12 13:05
memberAndrew Rissing17-Dec-12 13:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 17 Dec 2012
Article Copyright 2012 by Andrew Rissing
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid