Learn Computer Programming Here ... and maybe represent Ireland?
You are not logged in.
The Fibonacci numbers are a sequence of numbers with lots in interesting properties, and have been used in many disciplines, including art, architecture, literature and music. It is also related to the Golden Ratio, a proportion used in Renaissance works by many artists and architects who believed this proportion to be
aesthetically pleasing.
The first number of the sequence is 0, the second number is 1, and each subsequent number is equal to the sum of the previous two numbers in the sequence.
The sequence begins: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
Write a computer program to compute the sum of the first N Fibonacci numbers.
(hint: to check your answer, you can use an interesting property of the sequence. If F(k) is the kth Fibonacci number, then F(k+2)-1 is equal to the sum of the first k Fibonacci numbers)
Offline
If asked for F(1) should it be 0, F(0) being undefined, or should F(1) be the first 1, leaving F(0) being the 0.
(The latter notation is perhaps more consistent with most references I've seen to the sequence, but it wasn't clear here)
Offline
Just wondering in what range is N, eg 1-100/1000 etc...?
Offline
hi guys,
to answer your questions...
David:
As stated
>> The first number of the sequence is 0
and
>> F(k) is the kth Fibonacci number
therefore F(1)=0
F(6)=5, etc
Donncha:
You are right, a range should have been stated.
Assume N <= 40
(I will update the task statement)
Ciarán
Offline
So you don't ask the person for the number of fibonacci numbers they want displayed?
Offline
Yes, you will need to input an integer (N) and output the sum of the first N Fibonacci numbers. You do not have to print out all these numbers, merely the sum.
Offline
Do I need to worry about whether the user inputs an integer or not and prevent crash, or can I just assume he'll enter an integer and not a character?
EDIT: This could basically apply to all programming challenges here so just in general should we worry about errors like these or assume input will be valid?
Last edited by HappySmileMan (2008-11-21 22:55:15)
Offline
Thanks for your question.
For all tasks, you may assume the input is valid and will not be out of the stated range.
Offline
Didn't do it the way specified but...
My answer in C++:
#include <iostream>
using namespace std;
int main(void)
{
unsigned long int fib[41];
fib[0]=0;
fib[1]=1; //Set-up
int input=0;
unsigned long int sum=0;
cin>>input;
for(int i=1; i!=input; i++)
{
sum = fib[i] + fib[i-1]; //Finding the fibonnacci series
fib[i+1] = sum;
}
cout<<sum; //Print the result
}
Last edited by NotMyFault (2010-04-25 13:05:53)
Offline
I'm a new member, so I'm just looking around.
Offline
The natural world is loaded with patterns. From leaves on plants to the shape of a nautilus shell, numbers can help us to describe and understand these patterns.
In 1202 A.D., Leonardo Fibonacci discovered a unique mathematical sequence that has importance today. It is one way in which many of nature's most beautiful, strange, and seemingly unrelated objects show similarities.
Offline