Advertisement
Guest User

Untitled

a guest
Oct 25th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.25 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Fibonacci {
  8.     public class Fibo {
  9.         private static int counter;
  10.         static string _fiboReturn = string.Empty;
  11.         static Stack<int> _temp = new Stack<int>();
  12.  
  13.         public static string FibonacciSequence(int lengthOfSequence) {
  14.                 if (_temp.Count == 0) {
  15.                     _temp.Push(0);
  16.                     _fiboReturn += WriteToReturn(0);
  17.                 }
  18.                 else if (_temp.Peek() == 0) {
  19.                     _temp.Push(1);
  20.                     _fiboReturn += WriteToReturn(1);
  21.                 }
  22.                 else {
  23.                     var num2 = _temp.Pop();
  24.                     var num1 = _temp.Pop();
  25.                     _temp.Push(num2);
  26.                     _temp.Push(num1+num2);
  27.                     _fiboReturn += WriteToReturn(num1 + num2);
  28.             }
  29.             counter++;
  30.             if (counter <= lengthOfSequence) {
  31.                 FibonacciSequence(lengthOfSequence);
  32.             }
  33.             return _fiboReturn;
  34.         }
  35.  
  36.         private static string WriteToReturn(int input) {
  37.            
  38.             return $"{input}, ";
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement