Skip to content

Instantly share code, notes, and snippets.

@httpJunkie
Last active February 26, 2017 09:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save httpJunkie/ae975a587289c2e3efb3ee99f0ff7847 to your computer and use it in GitHub Desktop.
Save httpJunkie/ae975a587289c2e3efb3ee99f0ff7847 to your computer and use it in GitHub Desktop.
Fibbonacci Generator
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FibonacciGenerator
{
internal class Program
{
private static void Main(string[] args)
{
WriteFibonacci(15);
}
public static void WriteFibonacci(int length)
{
int[] fibSequence = ComputeFibonacci(length);
foreach (var item in fibSequence)
Console.Write(item == fibSequence.Last()
? "{0}"
: "{0}, ", item);
}
public static int[] ComputeFibonacci(int length)
{
var initializer = 1;
var sequence = new int[length]; sequence[0] = initializer;
for (var i=0; i<=length-2; i++)
sequence[i+1] = (i==0)
? sequence[i]
: sequence[i+1] = sequence[i] + sequence[i-1];
// nextNumber = (thisNumber + lastNumber)
return sequence;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment