# Fibonacci Without Loops or Recursion

By , 17 Dec 2012
 Rate this:

## 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.

## 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.

## 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

 First PrevNext
 Thoughts PIEBALDconsult 8-Sep-13 13:55
 Re: Thoughts Andrew Rissing 10-Sep-13 5:08
 formula's missing part Member 3385698 8-Sep-13 11:30
 Re: formula's missing part Andrew Rissing 10-Sep-13 5:06
 Re: formula's missing part Yuksel YILDIRIM 14-Oct-13 15:56
 My vote of 2 YvesDaoust 17-Dec-12 1:57
 Can do better YvesDaoust 17-Dec-12 1:51
 Re: Can do better Andrew Rissing 17-Dec-12 4:48
 True enough. I didn't see the other form down below. Unfortunately, it doesn't resolve the issue of 'n = 71' being wrong due to precision limits.   I'll update the tip with your recommendation (and credit) since it is indeed faster and simpler.
 Re: Can do better Andrew Rissing 17-Dec-12 5:04
 Re: Can do better [modified] YvesDaoust 17-Dec-12 5:32
 Last Visit: 31-Dec-99 18:00     Last Update: 8-Mar-14 1:59 Refresh 12 Next »

General    News    Suggestion    Question    Bug    Answer    Joke    Rant    Admin

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140305.2 | Last Updated 17 Dec 2012
Article Copyright 2012 by Andrew Rissing