Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Fibonacci {
- public class Fibo {
- private static int counter;
- static string _fiboReturn = string.Empty;
- static Stack<int> _temp = new Stack<int>();
- public static string FibonacciSequence(int lengthOfSequence) {
- if (_temp.Count == 0) {
- _temp.Push(0);
- _fiboReturn += WriteToReturn(0);
- }
- else if (_temp.Peek() == 0) {
- _temp.Push(1);
- _fiboReturn += WriteToReturn(1);
- }
- else {
- var num2 = _temp.Pop();
- var num1 = _temp.Pop();
- _temp.Push(num2);
- _temp.Push(num1+num2);
- _fiboReturn += WriteToReturn(num1 + num2);
- }
- counter++;
- if (counter <= lengthOfSequence) {
- FibonacciSequence(lengthOfSequence);
- }
- return _fiboReturn;
- }
- private static string WriteToReturn(int input) {
- return $"{input}, ";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement