I think there is a mistake in this challenge.
the first number of the fibonacci sequence is zero, not one.
Rabbits everywhere
I used the numbers on wikipedia and it worked:
http://en.wikipedia.org/wiki/Fibonacci_ ... ci_numbers
http://en.wikipedia.org/wiki/Fibonacci_ ... ci_numbers
I solved it in this way (groovy):
Since the fibonacci for 1 and 2 are defined as 1 I had no trouble with fibonacci of 0.
Code: Select all
def rabbits=0
for(i in 10..17) {
rabbits += fib(i)
}
println rabbits
def fib(n) {
switch(n) {
case 1:
case 2:
return 1
default:
return fib(n-2) + fib(n-1)
}
}
i solved it in this way (C++ recursive..)
result :

Code: Select all
#include <iostream>
using namespace std;
int fibonacci(int n)
{
return (n <= 2 ? 1 : fibonacci(n - 1) + fibonacci(n - 2));
}
void main()
{
int i,som;
som = 0;
//affichage des nombres de Fibonacci
for (i = 10; i < 18; i++)
{
cout << fibonacci(i) << endl;
}
//La somme
for (i = 10; i < 18; i++)
{
som = som + fibonacci(i);
}
cout << "La somme de la suite de Fibonacci de F(10) a F(17) est : " <<som<<endl;
system("PAUSE");
}
result :
Code: Select all
55
89
144
233
377
610
987
1597
La somme de la suite de Fibonacci de F(10) a F(17) est : 4092
Appuyez sur une touche pour continuer...

Computing fibonacci recursively is not the best method unless you use "transposition table" to compute each value just once.th4wri wrote:i solved it in this way (C++ recursive..)
Code: Select all
#include <iostream> using namespace std; int fibonacci(int n) { return (n <= 2 ? 1 : fibonacci(n - 1) + fibonacci(n - 2)); } void main() { int i,som; som = 0; //affichage des nombres de Fibonacci for (i = 10; i < 18; i++) { cout << fibonacci(i) << endl; } //La somme for (i = 10; i < 18; i++) { som = som + fibonacci(i); } cout << "La somme de la suite de Fibonacci de F(10) a F(17) est : " <<som<<endl; system("PAUSE"); }
result :
Code: Select all
55 89 144 233 377 610 987 1597 La somme de la suite de Fibonacci de F(10) a F(17) est : 4092 Appuyez sur une touche pour continuer...
My solution is a little crufty right now, as I'm running on a very limited knowledge of Bash. I hope to improve it and trim it down a bit latter.
Not too bad for someone who just got done reading a short tutorial on special characters in Bash, if I may allow myself a moment of undeserved arrogance.
Code: Select all
#!/bin/bash
result=0
num1=0
num2=0
next=1
for count in {1..9}
do
if [ "$num2" -lt 1 ]
then
num2=$next
let next=$num1+$num2
else
num1=$num2
num2=$next
let next=$num1+$num2
fi
done
for count in {10..17}
do
num1=$num2
num2=$next
let next=$num1+$num2
let result=$result+$num2
done
echo $result
It's definitely good start ... to use algorithm with linear time complexity rather to quadratic (for the generalised case \sum_{i=K}^{2K} F_i).Tragrirk wrote: Not too bad for someone who just got done reading a short tutorial on special characters in Bash, if I may allow myself a moment of undeserved arrogance.
With a bit of math this could be done in constant time complexity (but it definitely was not reqired here).