14,174,816 members
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 : ");
Console.WriteLine(f1);
Console.WriteLine(f2);
for (i = 0; i <= count; i++)
{
f3 = f1 + f2;
Console.WriteLine(f3);
f1 = f2;
f2 = f3;
}
}

public Task<IEnumerable<long>> GenerateSubset(int fromIndex, int toIndex)
{
throw new NotImplementedException();
}
Posted
Updated 15-May-17 21:25pm
Patrice T 15-May-17 23:59pm

## Solution 1

Full solution, if this is what you want

```using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

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 : ");
Console.Write("Enter last index : ");
GenerateSubset(fromIndex, toIndex);
}
}
public static void Generate()
{
int i;
long f1 = 0, f2 = 1, f3 = 0;
Console.Write("Enter the Limit : ");
//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

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

Top Experts
Last 24hrsThis month
 TheRealSteveJudge 215 0x01AA 165 OriginalGriff 150 phil.o 150 Gerry Schmitz 130
 OriginalGriff 4,566 Richard MacCutchan 1,920 Patrice T 1,417 phil.o 1,238 Gerry Schmitz 1,187