12,405,088 members (68,303 online)
Tip/Trick
Add your own
alternative version

14.4K views
8 bookmarked
Posted

# Fibonacci Without Loops or Recursion

, 17 Dec 2012 CPOL
 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)

## About the Author

 Software Developer (Senior) 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

 View All Threads First Prev Next
 My vote of 5 Manish Choudhary .NET expert14-Dec-12 18:50 Manish Choudhary .NET expert 14-Dec-12 18:50
 Last Visit: 31-Dec-99 18:00     Last Update: 29-Jul-16 1:35 Refresh 1

General    News    Suggestion    Question    Bug    Answer    Joke    Praise    Rant    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
Web01 | 2.8.160721.1 | Last Updated 17 Dec 2012
Article Copyright 2012 by Andrew Rissing
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid