Rabbits everywhere

Discussion of challenges you have already solved
Post Reply
Nihlaeth
Posts: 7
Joined: Sun Aug 16, 2009 1:07 am
Contact:

Rabbits everywhere

Post by Nihlaeth »

I think there is a mistake in this challenge.

the first number of the fibonacci sequence is zero, not one.
doomi
Posts: 7
Joined: Mon Aug 17, 2009 5:42 am

Post by doomi »

I used the numbers on wikipedia and it worked:
http://en.wikipedia.org/wiki/Fibonacci_ ... ci_numbers
Nihlaeth
Posts: 7
Joined: Sun Aug 16, 2009 1:07 am
Contact:

Post 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.
User avatar
Grusewolf
Posts: 16
Joined: Sun May 29, 2011 7:58 pm
Location: Munich

Post 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.
th4wri
Posts: 8
Joined: Tue Mar 29, 2016 10:27 am
Location: TuNiSiA

Post 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:
User avatar
Hippo
Posts: 339
Joined: Sat Feb 01, 2014 12:05 am
Location: Praha 5

Post 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.
Tragrirk
Posts: 1
Joined: Tue Apr 19, 2016 6:05 pm

Post 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.
User avatar
Hippo
Posts: 339
Joined: Sat Feb 01, 2014 12:05 am
Location: Praha 5

Post 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).
Post Reply