7

Can that be done with no while loops?

static void Main(string[] args)
{
    Console.WriteLine("Please enter a number");
    int number = Convert.ToInt32(Console.ReadLine());
    Console.WriteLine(" #" + Fibonacci(number));
}

public static int Fibonacci(int number)
{
    if (number <= 1)
    {
        return 1;
    }
    else
    {
        return Fibonacci(number - 2) + Fibonacci(number - 1);
    }
}

I can't even add a Console.WriteLine in the body of base case since it gets executed [number] number of times; Not sure how to do this without loops...

Ghost4Man
  • 1,040
  • 1
  • 12
  • 19
Marin
  • 12,531
  • 17
  • 56
  • 80

12 Answers12

24
static void Main(string[] args)
{
    Console.WriteLine("Please enter a number");
    int number = Convert.ToInt32(Console.ReadLine());
    Fibonacci(0, 1, 1, number);
}   

public static void Fibonacci(int a, int b, int counter, int number)
{
    Console.WriteLine(a);
    if (counter < number) Fibonacci(b, a+b, counter+1, number);
}
Gus
  • 25,839
  • 2
  • 51
  • 76
  • 3
    `public static void Fibonacci(int a, int b, int number) { Console.WriteLine(a); if (number > 1) Fibonacci(b, a+b, --number); }` – adityap Jul 22 '18 at 20:55
10
public static int Fibonatchi(int position) {

    if(position == 0) {
        return 1;
    }
    if(position == 1) {
        return 1;
    } else {
        return Fibonatchi(position - 2) + Fibonatchi(position - 1);
    }
}
V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50
igal sidoy
  • 101
  • 1
  • 2
1

I didn't find a way to do it closest way is it to combine both loops + recursion

    static void Main(string[] args)
    { 
        Console.WriteLine("Please enter a number");
              int number = Convert.ToInt32(Console.ReadLine());
        for(int counter=0;counter<number;counter++)      
        Console.WriteLine(" \n" + Fibonacci(counter) );

    }

    public static int Fibonacci(int number)
    {

        if (number == 0)
            return 0;
        else if(number ==1)
          return 1;
        else
        {
         return Fibonacci(number - 2) + Fibonacci(number - 1);
        }

    }
Marin
  • 12,531
  • 17
  • 56
  • 80
0
namespace Algorithms
{
    class Program
    {
        static void Main(string[] args)
        {
            string fibResult = "";
            fibResult =  FibCal(10);
            Console.WriteLine(fibResult);
            Console.ReadLine();
        }

        public static string FibCal(int n)
        {
            string series = "";
            int k, f1, f2 , f = 0;
            f1 = f2 = 1;
            if (n < 2) 
                return n.ToString();
            else
                for (k = 0; k < n; k++)
                {
                    f = f1 + f2;
                    f2 = f1;
                    f1 = f;
                    series += f.ToString() + ",";
                }

            return series;
        }

    }
}

Hope this helps

daniula
  • 6,898
  • 4
  • 32
  • 49
0

Using recursion in this fashion is a very bad idea. It will cause memory problems very quickly. I know you want to avoid using while/for loops, but an array is really the best way to go.

0

Using LINQ

   public static void fibSeriesEx3()
    {
        List<int> lst = new List<int> { 0, 1 };
        for (int i = 0; i <= 10; i++)
        {
            int num = lst.Skip(i).Sum();
            lst.Add(num);

            foreach (int number in lst)
                Console.Write(number + " ");
            Console.WriteLine();
        }
    }
Sivakishore Teru
  • 216
  • 1
  • 2
  • 9
0

That's a way to do it by returning a value into the main.

public static void Main() {

    Console.WriteLine("Introduce the number");
    int num = Convert.ToInt32(Console.ReadLine());

    int num1 = 1, num2 = 1, counter = num-2; 

//Take 2 out to match the list as first 2 numbers doesn't count in the function.

    Console.WriteLine(Fibo(num1, num2, counter));
}

public static int Fibo(int num1, int num2, int counter)    {

    int temp = num1;

    if (counter <= 0)
        return num2;
    else
        return Fibo(num1 = num2, num2 += temp, counter-1);
}
Renom
  • 508
  • 4
  • 7
0
public static class Golden
{
    public static IEnumerable<long> Fibonacci()
    {
        var a = 0L;
        var b = 1L;
        var s = 0L;
        yield return a;
        while (a < long.MaxValue - b)
        {
            yield return b;
            s = a + b;
            a = b;
            b = s;
        }
    }

    public static IEnumerable<long> FibonacciR()
    {
        IEnumerable<long> Fibo(long a, long b)
        {
            yield return a;
            if (a < long.MaxValue - b)
            {
                foreach (var v in Fibo(b, a + b))
                {
                    yield return v;
                }
            }
        }
        return Fibo(0, 1);
    }
}
Fabio Maulo
  • 427
  • 4
  • 3
0

I realize this may be an old thread, but oh well I think this kinda question is good in its nature.

Using while loop/or recursive way of doing is not the optimal way of doing as it takes a O(2^n) times. A better way to do this is using what is already in memory like below. This should take at most O(n) time.

Cheers!

 static double fibDynamic(int n)
        {
            double[] array = new double[n];
            array[0] = array[1] = 1;

            for(int i = 2; i < n; i++)
            {
                array[i] = array[i - 1] + array[i - 2];
            }
            return array[n-1];
        }
CB4
  • 690
  • 3
  • 13
  • 25
0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static int Main(string[] args)
        {

            int n, i = 0, c;
            Console.WriteLine("Enter the number of terms:");
            n = Convert.ToInt16(Console.ReadLine());

            Console.WriteLine("Fibonacci series\n");

            for (c = 1; c <= n; c++)
            {
                int result = FibonacciFunction(i);
                Console.Write(result + " " );
                i++;
            }
            Console.WriteLine();
            return 0;
        }

        public static int FibonacciFunction(int n)
        {
            if (n == 0)
            {
                return 0;
            }
            else if (n == 1)
            {
                return 1;
            }
            else
            {
                return (FibonacciFunction(n - 1) + FibonacciFunction(n - 2));
            }
        }

    }
}
Yevhen_Radchenko
  • 965
  • 8
  • 15
0

Simple and easy solution :

static void Main(string[] args)
{
     int number;

     Console.WriteLine("enter number");

     number = int.Parse(Console.ReadLine());

     Console.WriteLine(Recursive(number));

     Console.ReadLine();
}

public static int Recursive(int number)
{
    if (number <= 2)
    {
        return 1;
    }
    else
    {
        return Recursive(number - 1) + Recursive(number - 2);
    }
}
Thomas Rollet
  • 1,573
  • 4
  • 19
  • 33
0

Using one line code:

public static int Fibonacci(int i)
{
    return i <= 2 ? 1 : Fibonacci(i - 1) + Fibonacci(i - 2);
}