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

How to get one datatable with two columns from another datatable with many columns in C#
C# C++ Interop program can not load assembly in IDE, but works when directly launching
How to Hydrate a poco from his ToString
Passing a list from Controller to View in ASP.NET MVC 4
Issue in password field validation asp.net C#
Process.Start leaves streams empty
Separate one process into several processes in C#
How to delete dynamically created textbox in asp.net
How to model relationship between multiple many to many object
Mock abstract protected method
How to add a looping selector to a Windows Phone 8.1 app?
AccessViolationException after copying a file from inside a zip archive to the clipboard and handling its data
How can I easily create a Func<T> from a method name?
C# Generic Passing different objects with same properties
Issue with Photometric Interpretation tag
Fixing possible memory leak in RelayCommand

Categories

HOME
msbuild
kendo-ui-angular2
system-calls
pyspark
c++builder
liquibase
skypedeveloper
conceptual
magento-1.9.2.1
where
query-string
localhost
mediarecorder
richtextbox
python-3.4
yacc
openbsd
angular-universal
point-of-sale
resharper
luis
openam
passbook
project-structure
klipfolio
email-client
rvm
akavache
expressionengine
cloudformation
r-factor
concrete5
ng2-bootstrap
oracle-nosql
traitsui
javax
strategy-pattern
jibx
area
amazon-rds-aurora
beanshell
jenkins-docker
petapoco
rmq
qt3d
android-music-player
xcode7.1
altova
csc
hfp
system-integration
jsondoc
markerspiderfier
jquery-draggable
recode
mojolicious
database-project
vanilla-forums
waterline
perfect-scrollbar
conkeror
crystal-reports-7
formbuilder
parsefloat
worklight-appcenter
oxygenxml
rpt
angular-file-upload
dateadd
class-library
mencoder
objectdatasource
angularjs-select2
extensibility
proj4
bho
nsautoreleasepool
nx
pymel
m3u
databus
bash4
parsec
inserthtml
nsundomanager
ruby-1.9
discussion-board
object-tag
javah
lgpl
mediastreamsource
activescaffold
cross-cutting-concerns

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