fibonacci sequence help!!

Pages: 12
hi so the fibonacci sequence is
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233... so on

however i only want every other number which is
1, 2, 5, 13, 34, 89...etc

i have the equation to get all the fibonacci numbers but not every other

int i, fib[30];
fib[0]=1;
fib[1]=1;
for (i=2;i<30;i++)
{ fib[i]=fib[i-1]+fib[i-2];}

but how do i do it so that it is only every other number?
Well frankly speaking, it seems that your asking to break the rules, or modify, the Fibonacci sequence and algorithm in order to retrieve every other number??

And btw your calculation of the Fibanacci-numbers is wrong.

n= n+1 + n+2 is not the propper calculation

The next Fibanacci number is the sum of the previous nuber and the one previous to that.
So, in terms of c++:

Fib_num[n]=Fib_num[n-1]+Fib_num[n-2] with:
Fib_num[1] =Fib_num[2] = 1

Hope this helps with progress....

im confused i had this:

fib[i]=fib[i-1]+fib[i-2]

which is essentially the same thing as you wrote but

Fib_num[1] =Fib_num[2] = 1 <--- this part, where is the variable?
sorry that second line of input shouldnt have been there.

gotta question.....exactly what are you trying to do?? adjust the algorithm? or retrieve every other number?
im just trying to find what equation to put in the for block thatll return every other number in the fibonacci sequence? so adjust the algorithm
to be honest ive never tried adjusting to equation..its kind of a stable in this problem.

however...mayb you could put for loop that simply removes every other token in your fib[] array....theoretically that would produce the same output.

Psuedo;
if (fib at index) == even(i%2 == true || 0)
remove i

Come on, it's easy!

Just store the values you don't want elsewhere.

I do think you should try and figure it out for yourself. If you want to be a programmer it's the kind of thing you should be (will be!) able to just write down!

Removing the values is lame!

Andy

P.S. You just need a single extra int, and a little bit more code...
Last edited on
haha yeah that was my initial thought, but the kidd wanted to rearrange the Fib algorithm itself...and ive never seen that done lol
Well, you do have to rearrange the alg a little bit.

So it goes twice as fast!
yeah just a tad...lol one of the first programming task i ever tried
closed account (D80DSL3A)
A few algebraic substitutions can help here.
If I write:

1)
Fn = Fn-1 + Fn-2

and 2)
Fn+1 = Fn + Fn-1


Then I can make substitutions into:
Fn+2 = Fn+1 + Fn


Use the first 2 identities to eliminate Fn+1 in the last.
Using 2)
Fn+2 = ( Fn + Fn-1 ) + Fn

Then, from 1) solved for Fn-1

Fn+2 = Fn + ( Fn - Fn-2 ) + Fn

Or finally:

Fn+2 = 3*Fn - Fn-2


Example: Given F0 = 1 and F2 = 2 this can be used to obtain F4 directly.
For n=2: F4 = 3*F2 - F0 = 3*2-1 = 5.
and so on for F6, F8, etc...
To get The odd numbered ones start from F1 and F3 instead.

Is that what you're looking for?
that equation does work for the first two numbers, but im getting an output of

1, 2, 1653737964, 1653737964...
actually im not getting anything close to the sequence at all
Hmmm...

Not if this will help, but it might.

Try to recode so that the loop increments two at a time.

1
2
3
4
5
int i, fib[30];
fib[0]=1;
fib[1]=1;
for (i=2;i<30;i+=2)
{ ??? }


Then eliminate the handling of the odd numbers.
hm changing the parametes didnt work either
i think it has to do with the equation ...im not so sure

i dont know howat all to store the values i dont want elsewhere either
int elsewhere = value_that_i_do_not_want;
Show us your implementation. You will need to provide F[2] as a base case.
My solution uses the standard equation, pretty ,much exactly like your original code.

But my loop calculate two new values at a time. I keep the first and "skip" the second (you can't totally skip the value, but it doesn't go in the array).
Last edited on
how do you skip?
I can't say more without telling you the answer. When you get the solution, you'll see what I mean.

Maybe set it aside for now and have another go later.
Hey man.. i've the example that you are requesting below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <vector>	//STL cointainer of self escalable vector

class Fibonacci
{
public:
	void fibonacci(int x)
	{
		int y = x+1;	//'x' = desired number of terms
		
		//vector  of integers named 'seq'
		std::vector< int > seq;
		//resize the vector for y elements
		seq.resize(y);
		seq[0]=0;
		seq[1]=1;

		std::cout << "Fibonacci Sequence\n{ " << seq[1] << ",";

		//traverses the vector until the last element
		for (int i = 2; i < seq.size(); i++)
		{
			//evaluate of fibonacci sequence
			seq[i] = seq[i-1] + seq[i-2];
			if (i==(seq.size()-1))
			{
				std::cout << " " << seq[i];
			}
			else
			{
				std::cout << " " << seq[i] << ",";
			}
		}
		std::cout << "... }";
	}
}
Last edited on
Pages: 12