Ver Mensaje Individual
  #1 (permalink)  
Antiguo 18/05/2012, 05:50
Gec
 
Fecha de Ingreso: mayo-2012
Mensajes: 5
Antigüedad: 12 años
Puntos: 0
quien me explica estos codigos de fibonacci

tenla secuencia fibonacci en los siguientes lenguajes perl, python, haskell, lisp y c/c++
necesito persona dispuesta a explicarme como hacer que corran y me digan que compilador se debe descargar toda informacion que puedan darme a entender sobre esta programa cion gracias 5 estrellas mejor respuesta.

Python
Recursive
import sys

def fib(n):
if n < 2:
return n
else:
return fib(n - 1) + fib(n - 2)

def main():
limit = int(sys.argv[1])
print(fib(limit))
main()
With generator
def fib():
a, b = 0, 1
while True:
yield a
a, b = b, a + b


Perl
Iterative using bigint
#! /usr/bin/perl
use bigint;

my ($a, $b) = (0, 1);
for (;;)
{
print "$a\n";
($a, $b) = ($b, $a+$b);
}
Recursive
sub fibo;
sub fibo {$_ [0] < 2 ? $_ [0] : fibo ($_ [0] - 1) + fibo ($_ [0] - 2)}
Iterative
sub fibo
{
my ($n, $a, $b) = (shift, 0, 1);
($a, $b) = ($b, $a + $b) while $n-- > 0;
$a;
}


Lisp
(defun fibonacci (x)
"
Calcule le nombre de fibonacci pour x
"
(if (<= x 2)
1
(+ (fibonacci (- x 2))(fibonacci (1- x)))))

(loop for i from 1 to x do
(print (fibonacci i)))


Haskell
module Main where
import System.Environment

fibo = 1 : 1 : zipWith (+) fibo (tail fibo)

main = do
args <- getArgs
print (fibo !! (read(args!!0)-1))


C
Recursive
int fib(int n)
{
if (n < 2)
return n;
else
return fib(n-1) + fib(n-2);
}

printf("%d\n", fib(10));
Iterative
int fib(int n)
{
int first = 0, second = 1;

int tmp;
while (n--)
{
tmp = first+second;
first = second;
second = tmp;
}
return first;
}


C++
Recursive
int fib(int n)
{
if (n < 2)
return n;
else
return fib(n-1) + fib(n-2);
}
cout << fib(10) << endl;
Iterative
int fibonacci(int n)
{
int u = 0;
int v = 1;
int i, t;

for(i = 2; i <= n; i++)
{
t = u + v;
u = v;
v = t;
}
return v;
}