Algorithm for fibonacci sequence

  • Thread starter Thread starter 5ymmetrica1
  • Start date Start date
  • Tags Tags
    Algorithm Sequence
Click For Summary
SUMMARY

The discussion focuses on creating an algorithm for generating the Fibonacci sequence, specifically addressing the formula f(n) = f(n-1) + f(n-2). A user attempted to implement this algorithm but encountered issues resulting in incorrect outputs, such as doubling values instead of following the Fibonacci pattern. The conversation highlights the need for understanding recursive functions to correctly implement the Fibonacci sequence.

PREREQUISITES
  • Basic understanding of algorithm design
  • Familiarity with recursive functions
  • Knowledge of programming syntax (e.g., function definitions)
  • Introductory programming concepts
NEXT STEPS
  • Study recursive function implementation in programming languages
  • Learn about base cases and recursive cases in algorithms
  • Explore the use of memoization to optimize Fibonacci calculations
  • Practice writing algorithms in a specific programming language (e.g., Python, JavaScript)
USEFUL FOR

Beginner programmers, computer science students, and anyone interested in algorithm development and recursive programming techniques.

5ymmetrica1
Messages
88
Reaction score
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
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:

Similar threads

  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 11 ·
Replies
11
Views
2K
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
33
Views
5K
  • · Replies 2 ·
Replies
2
Views
3K