Page 1 of 1

Rabbits everywhere

Posted: Mon Aug 17, 2009 3:17 pm
by Nihlaeth
I think there is a mistake in this challenge.

the first number of the fibonacci sequence is zero, not one.

Posted: Mon Aug 17, 2009 3:25 pm
by doomi
I used the numbers on wikipedia and it worked:
http://en.wikipedia.org/wiki/Fibonacci_ ... ci_numbers

Posted: Mon Aug 17, 2009 5:17 pm
by Nihlaeth
Yeah, but you have a 0th number than. Usually you start counting from one.

I just selected one as the first number. But that's why I posted this. The answer I gave was incorrect, but counted as correct.

Posted: Tue Apr 01, 2014 8:47 pm
by Grusewolf
I solved it in this way (groovy):

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)
    }
}
Since the fibonacci for 1 and 2 are defined as 1 I had no trouble with fibonacci of 0.

Posted: Tue Mar 29, 2016 1:13 pm
by th4wri
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...






:roll:

Posted: Wed Mar 30, 2016 5:42 pm
by Hippo
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...






:roll:
Computing fibonacci recursively is not the best method unless you use "transposition table" to compute each value just once.

Posted: Fri Apr 22, 2016 8:22 am
by Tragrirk
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.

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
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.

Posted: Mon Apr 25, 2016 12:18 pm
by Hippo
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.
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).

With a bit of math this could be done in constant time complexity (but it definitely was not reqired here).