Click here to Skip to main content
14,174,816 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
I am trying to generate a Fibonacci sequence computed by starting with the array [ 0, 1 ] and each subsequent number is computed by adding the two numbers before it. // E.g. 0, 1, [0 + 1 =] 1, [1 + 1 =] 2, [1 + 2 =] 3, [2 + 3 =] 5, and so on.

Two methods I am trying to implement are below however i am badly stuck in generating a subsets(GenerateSubset(params)). Any help would be really appreciable.

What I have tried:

public IEnumerable<long> Generate()
{
int i, count, f1 = 0, f2 = 1, f3 = 0;
Console.Write("Enter the Limit : ");
count = int.Parse(Console.ReadLine());
Console.WriteLine(f1);
Console.WriteLine(f2);
for (i = 0; i <= count; i++)
{
f3 = f1 + f2;
Console.WriteLine(f3);
f1 = f2;
f2 = f3;
}
Console.ReadLine();
}



public Task<IEnumerable<long>> GenerateSubset(int fromIndex, int toIndex)
{
throw new NotImplementedException();
}
Posted
Updated 15-May-17 21:25pm
Comments
Patrice T 15-May-17 23:59pm
   
define 'i am badly stuck'
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Full solution, if this is what you want

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication
{
    class Program
    {
        static IEnumerable<long> fibs = Enumerable.Empty<long>();
        static int count, fromIndex, toIndex;
        static void Main(string[] args)
        {
            while (true)
            {
                Generate();
                Console.Write(string.Format("Enter index range between 0 and {0}\n", count));
                Console.Write("Enter first index : ");
                fromIndex = int.Parse(Console.ReadLine());
                Console.Write("Enter last index : ");
                toIndex = int.Parse(Console.ReadLine());
                GenerateSubset(fromIndex, toIndex);
            }
        }
        public static void Generate()
        {
            int i;
            long f1 = 0, f2 = 1, f3 = 0;
            Console.Write("Enter the Limit : ");
            count = int.Parse(Console.ReadLine());
            //Console.WriteLine(f1);
            //Console.WriteLine(f2);
            fibs = fibs.Concat(new[] { f2 });
            Console.Write("Full sequence :\n");
            Console.WriteLine(f2);
            for (i = 0; i < count; i++)
            {
                f3 = f1 + f2;
                fibs = fibs.Concat(new[] { f3 });
                Console.WriteLine(f3);
                f1 = f2;
                f2 = f3;
            }
        }

        static public void GenerateSubset(int fromIndex, int toIndex)
        {
            for (int i = fromIndex; i <= toIndex; i++)
            {
                Console.WriteLine(fibs.ElementAt(i));
            }
            Console.WriteLine("\n");
        }
    }
}


Program screenshot: http://imgur.com/ogER8oQ
   
Comments
Rajeshyadav12 16-May-17 7:19am
   
Hi Zunayed, Thank you for your code. But I want to write a code in such way that it should pass the test cases. I will send you the link which gives you clearer idea about the task. Please find the link: drive.google.com/open?id=0B_6Eur5JYu9_MDNfelVKOWswRGs and drive.google.com/open?id=0B_6Eur5JYu9_Y3BhZFdSbkpiWVE. Please go through it once. Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web04 | 2.8.190524.3 | Last Updated 16 May 2017
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100