Yes, I know Fibonacci(=sun of a good man) again.
Most famous for his series, but who among you all, know that he was the man who introduced to the Western world, the Arabic numeral system(including zero) in 1202 A.D.? The Italian merchands of those days adored it. It was far more easy to work with than the roman numeral system of course. And it is still in use today.
I did the series a little different with a calculation that gives you any Fibonacci number from zero up to the 93th one. I guess the Convert method does the rounding. Also included here in the code, is a little test loop.
Enjoy!
Fibonacci in C#
using System;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
for (Byte n = 0; n < 20; n++)
{
Console.WriteLine("F({0}) = {1}", n, Fib(n));
}
Console.ReadKey();
}
/// <summary>
/// Calculates the Nth(starting from zero) Fibonacci number
/// no overflow error checking
/// </summary>
/// <param name="n">n can range from 0 to 93</param>
/// <returns>F(n)</returns>
public static UInt64 Fib(Byte n)
{
double sqrt5 = Math.Sqrt(5);
double phi = (sqrt5 + 1) / 2;
return Convert.ToUInt64((1 / sqrt5) * Math.Pow(phi, n));
}
}
}
ddanbe 2,724 Professional Procrastinator Featured Poster
Triryche 0 Newbie Poster
ddanbe 2,724 Professional Procrastinator Featured Poster
Triryche 0 Newbie Poster
Triryche 0 Newbie Poster
sepp2k 378 Practically a Master Poster
Triryche 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.