Algorithm for fibonacci sequence

In summary, the conversation is about creating an algorithm for a Fibonacci sequence that returns the f value in f(n) = f(n-1)+f(n-2). The person has tried several ways but always end up with a sequence of 0, 1, 1, 2, 4, 8, 16, and needs help understanding recursive functions.
  • #1
5ymmetrica1
88
0

Homework Statement


create an algorithm for a Fibonacci sequence that will return the f value in f(n) = f(n-1)+f(n-2)

Homework Equations



f(n) = f(n-1)+f(n-2)

The Attempt at a Solution



I have tried several ways to create an algorithm that will sum the two previous numbers but always end up with the sequence 0,1,1,2,4,8,16 so the numbers end up double.

please keep in mind I have only just started my intro to programming course and am just learning to write algorithms, I have no experience with programming languages at the present time.

I've got something like this

1 function fibonacci(a,b)
2 if n=0,
3 a<- 0
4 b<- 1
5 print a
6 if n=1
7 a<- (a+b) = (1+0) = 1
8 print a
9 if n=2
10 b<- a = 1
11 a<- (a+b) = (1+1) =2
12 print abut as you can see, continuing on this path with only double the value each time. I need to find a way to sum numbers, depending on the value 'n' but can't seem to think of how to do this.

any help is greatly appreciated.
 
Physics news on Phys.org
  • #2
I see that you need to learn more about recursive functions. Here, this will help you out:
https://www.khanacademy.org/science/computer-science-subject/computer-science/v/recursive-fibonacci-example
 
Last edited by a moderator:

1. What is the Fibonacci sequence?

The Fibonacci sequence is a mathematical sequence in which each number is the sum of the two preceding numbers, starting with 0 and 1. It is represented as 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, and so on.

2. What is the algorithm for generating the Fibonacci sequence?

The algorithm for generating the Fibonacci sequence involves starting with the first two numbers (0 and 1), and then adding them together to get the third number. This process is repeated to get the next number in the sequence, and so on.

3. How is the Fibonacci sequence used in computer science?

The Fibonacci sequence is used in computer science for various applications such as generating random numbers, coding and encryption, and analyzing algorithms and data structures.

4. What is the time complexity of the Fibonacci sequence algorithm?

The time complexity of the Fibonacci sequence algorithm is O(n), meaning that the time it takes to compute the sequence increases linearly with the size of the input.

5. Are there any other algorithms for generating the Fibonacci sequence?

Yes, there are other algorithms for generating the Fibonacci sequence, such as the Binet's formula and the matrix method. These algorithms may have different time complexities and are used for specific purposes.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
806
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
911
  • Engineering and Comp Sci Homework Help
Replies
2
Views
948
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
Back
Top