C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C# Expert!

Join 300,406 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,599 people online right now. Registration is fast and FREE... Join Now!




Trouble generating Fibonacci sequence.

 

Trouble generating Fibonacci sequence., Could you check out my method?

papuccino1

1 Jul, 2009 - 05:00 PM
Post #1

C# Programmer
Group Icon

Joined: 2 Mar, 2008
Posts: 943



Thanked: 33 times
Dream Kudos: 50
My Contributions
Here's what I got so far:

csharp
private string GenerarFibonacci(int Cantidad)
{
string StringNumerosGenerados = "";
int[] NumerosGenerados = new int[Cantidad];

if (Cantidad == 0)
{
NumerosGenerados[0] = 0;
StringNumerosGenerados = NumerosGenerados[0].ToString();
return StringNumerosGenerados;
}
else if (Cantidad == 1)
{
NumerosGenerados[0] = 0;
NumerosGenerados[1] = 1;
StringNumerosGenerados = NumerosGenerados[0].ToString() + NumerosGenerados[1].ToString();
return StringNumerosGenerados;
}

for (int i = 3; i <= Cantidad; i++)
{
NumerosGenerados[i] = NumerosGenerados[i--] + NumerosGenerados[(i--)];
}

foreach (int X in NumerosGenerados)
{

}
}


My trouble is in the For-Loop. How could I go: "Add the previous i index and the previous PREVIOUS i index."

I can't seem to figure it out.

User is offlineProfile CardPM
+Quote Post


Dantheman

RE: Trouble Generating Fibonacci Sequence.

1 Jul, 2009 - 05:10 PM
Post #2

D.I.C Regular
***

Joined: 27 May, 2009
Posts: 445



Thanked: 25 times
My Contributions
Declare 2 variables that will hold those 2 numbers and the current sum, so something like:

CODE

int sum;
int prev;
int prev_prev;


The whole problem is centered on figuring out what to assign those variables to. I will leave that to you. But the problem is extremely simple so I'm not even sure why you're having a difficulty solving it.

This post has been edited by Dantheman: 1 Jul, 2009 - 05:13 PM
User is offlineProfile CardPM
+Quote Post

Bocard

RE: Trouble Generating Fibonacci Sequence.

1 Jul, 2009 - 10:09 PM
Post #3

D.I.C Head
**

Joined: 24 Sep, 2008
Posts: 137



Thanked: 4 times
My Contributions
well it depends what you need to do with the numbers, but if you just need the numbers and not store the values you only need 3 vars.

CODE

int a = 1;
int b = 2;
int c = 3;

while (sth happens) // how far do you want to get the values
{
a = b+ c;
b = a+ c;
c = a+ b;
console.writeline("a = " + a);
console.writeline("b = " + b);
console.writeline("c = " + c);
}



OFF: From your latests posts I figured out you are either trying to create some projects from Martyr2's list or you are trying to solve some Project Euler problems. If I'm right please PM me and maybe we can do these things together smile.gif

This post has been edited by Bocard: 1 Jul, 2009 - 10:12 PM
User is offlineProfile CardPM
+Quote Post

papuccino1

RE: Trouble Generating Fibonacci Sequence.

2 Jul, 2009 - 05:38 AM
Post #4

C# Programmer
Group Icon

Joined: 2 Mar, 2008
Posts: 943



Thanked: 33 times
Dream Kudos: 50
My Contributions
Actually I am trying to do the project list Martyr made. How far along are you?
User is offlineProfile CardPM
+Quote Post

Bocard

RE: Trouble Generating Fibonacci Sequence.

2 Jul, 2009 - 05:42 AM
Post #5

D.I.C Head
**

Joined: 24 Sep, 2008
Posts: 137



Thanked: 4 times
My Contributions
i haven't started Martyr's list yet, I solved though 3 problems on the Project euler site...and some stuff matches. That's why I wasn't sure which one you are working on.
User is offlineProfile CardPM
+Quote Post

papuccino1

RE: Trouble Generating Fibonacci Sequence.

2 Jul, 2009 - 05:51 AM
Post #6

C# Programmer
Group Icon

Joined: 2 Mar, 2008
Posts: 943



Thanked: 33 times
Dream Kudos: 50
My Contributions
Ah, I've worked on project euler before but I didn't really like it. Maybe I'll give it a shot one of these days. What I'm really trying to do is build as many programs as I can using as many different things as I can. Martyrs list is just god sent!
User is offlineProfile CardPM
+Quote Post

indrora

RE: Trouble Generating Fibonacci Sequence.

2 Jul, 2009 - 10:15 PM
Post #7

D.I.C Head
Group Icon

Joined: 25 Jul, 2008
Posts: 76



Thanked: 1 times
Dream Kudos: 75
My Contributions
Hmmm....
csharp


// generate an array of the first Nth Fibonacci numbers
int[] fibs(int num)
{

// fib has 3 base cases, and 3 starting numbers.
// Shouldnt be too hard.

// Lets handle the first three...
int[] fibs = new int[num];
for(int fibn = 0; fibn < num; fibn++)
{
// Lets make sure fibn isnt a special case:
switch(fibn)
{
// first Fibonacci is 0.
case 0:
fibs[fibn] = 0;
break;
// 1 and 2 are always 1;
case 1:
case 2:
fibs[fibn] == 1;
break;
default:
fibs[fibn] = fibs[fibn-1]+fibs[fibn-2];
break;
}
return fibs[];
}

}

.. Do what you wish with the results of fib(int);

Another cheap hack is to get the Nth fib:
csharp

function nthFib(int n)
{
// registers we'll use. Forgive me for thinking like a
int eaa,eab,eac;
// funky cases.
if(n == 0) {return 0; }
elseif(n == 1 && n ==2) {return 1; }
// the normal case.
else
{
// set our initial values.
eaa=1;eab=2;eac=0;
// go through the loop. We're assuming the 3rd fib was asked for (or where n > 2)
for(int x = 3; x<=n; x++)
{
// set our output register to the sum of the past 2
eac = eab+eaa;
// eab is the Last Computed value, so we push that into eaa
eaa=eab;
// and since we want to hold onto our last computed value, we put it into eab.
eab = eac;

}
// we've calculated enough iterations. RETURN TO BASE, Y'ALL!
return eac;
}
}

... dont even ask me to indent that.
User is offlineProfile CardPM
+Quote Post

Renagado

RE: Trouble Generating Fibonacci Sequence.

3 Jul, 2009 - 12:54 AM
Post #8

D.I.C Head
Group Icon

Joined: 14 Jun, 2009
Posts: 180



Thanked: 16 times
Dream Kudos: 100
My Contributions
Here's my quick attempt:
CODE
static void Main(string[] args)
        {
            int n = 100;
            Double[] fibonacci=new Double[n];
            fibonacci[0] = 0; fibonacci[1] = 1;
            for(int i=0;i<(n-2);i++)
            {
                fibonacci[(i + 2)] = fibonacci[i] + fibonacci[(i + 1)];
            }
            foreach (Double i in fibonacci)
                Console.WriteLine(i);
        }


Btw am also trying some things of Martyr's list so if you're stuck I'll try and give you some aid biggrin.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 10:43PM

Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month