The Fibonacci sequence is a series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
The first two numbers of the sequence are 0 and 1. An interesting fact about these numbers is that every number after the first two is the sum of the two preceding ones.
- 0 and 1 are the first two numbers.
- The 3rd number is found by adding the last two ie. 0 + 1 = 1
- The 4th number is found by adding the last two ie. 1 + 1 = 2
- The 5th number is found by adding the last two ie. 1 + 2 = 3
- and so on!
Program
Write a program to compute the first N Fibonacci numbers.
Input
- The number(N) of Fibonacci numbers to be computed.
Output
- The sum of N-1 and Nth Fibonacci numbers.
Assumptions
- N is whole number > 0
Test cases
No | Input N (How many numbers) | Intermediate (Fibonacci Numbers) | Intermediate (Sum of N-1 and N ) | Output (Sum) |
---|---|---|---|---|
1 | 5 | 0, 1, 1, 2, 3 | 2 + 3 | 5 |
2 | 10 | 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 | 21 + 34 | 55 |
3 | 15 | 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,55,89, 144, 233, 377 | 233 + 377 | 610 |
Instructions
- Accept the number of Fibonacci numbers to create(N) as input via the command line arguments.
- Write the logic to compute the triangle numbers and add them up.
- Display the resulting sum.