Last active
February 26, 2017 09:01
-
-
Save httpJunkie/ae975a587289c2e3efb3ee99f0ff7847 to your computer and use it in GitHub Desktop.
Fibbonacci Generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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