c#


Generating Fibonacci Sequence and creating subsets for it


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.
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();
}
Below is the file containing the test cases which i am trying to pass by implementing those methods.
Test cases file:
[ drive.google.com/open?id=0B_6Eur5JYu9_MDNfelVKOWswRGs]
I think you want a logic for generating a subset of Fibonacci series. here is the logic i write in java. you can convert into c#.
int fibonacci(int x) {
if (x == 0)
return 0;
if (x == 1)
return 1;
return fibonacci(x-1)+fibonacci(x-2);
}
and second function which generates the subset is
List<Integer> GenerateSubset(int fromIndex, int toIndex) {
int first=fibonacci(fromIndex);
int second= fibonacci(fromIndex+1);
int third;
List<Integer> result= new ArrayList<Integer>();
result.add(first);
result.add(second);
for(int i= fromIndex+2;i<=toIndex-1;i++) {
third= first+second;
result.add(third);
first=second;
second=third;
}
return result;
}
It returs the list which contain the subset of fibonacci series.

Related Links

C# String operator '+' overloading [duplicate]
unhandled exceptions at startup in my c# project
C# Comparing 2 data tables and changing font color to red for matching columns in data table 1
How to query EntityFramework using generic property type?
C#: Read data of Excel's Form Controls
How to make a nested List form a List
Use a service account to read/update calendars of O365 users
DataGridView: What is the best approach to handle Editable Data in DataGridView?
Export only grid view data on excel from page through C#
'Conversion failed' error is not captured while executing in C# or in stored procedure
c# async await and exception
Generic caching in c#
Parse - Resetting Passwords on ASP.NET MVC
Not all code paths return a value for boolean method in class
Unit of work and general repository package
Getting version number from a running proccess ( With NO access to the .exe path)

Categories

HOME
netty
ns2
postgresql-9.3
grpc
jquery-plugins
spotfire
lithium
singleton
google-docs-api
multipartform-data
psexec
pdf-generation
google-api-oauth
revit-api
opengrok
eclipse-orion
dompdf
samba
inline-assembly
oracle10g
android-6.0-marshmallow
typeclass
key-bindings
ibm-bpm
visual-studio-debugging
restler
bootcamp
pymunk
mkdir
multi-user
oracle-bpm-suite
mybatis-generator
julia-jump
azure-web-roles
definition
vpython
wufoo
bootstrap-carousel
amazon-fire-tv
reqif
google-cloud-shell
intermediate-language
lattice
init
radar-chart
wizard
jersey-test-framework
bluetooth-lowenergy-4.2
dday
jquery-draggable
dynamics-nav
matlab-engine
mojolicious
database-project
personality-insights
eggplant
jeet-grid
service-broker
jad
clearcanvas
fscanf
visual-studio-setup-proje
sendy
complete
azure-pack
wpa
infinity.js
proj4
pudb
junction-table
ftpwebrequest
system.transactions
django-tinymce
algol
m3u
report-viewer2010
derived-class
custom-cursor
google-email-audit-api
beep
django-1.4
animationdrawable
inserthtml
mcpd
soapexception
jexcelapi
auto-compile
chars
nscharacterset
xslt-grouping
large-data-volumes
wtsapi32

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App