Feb 01

JavaScript

function fibo(n)
{
if (n < 2) return 1;
return fibo(n-2) + fibo(n-1);
}

for(var i = 1; i < x ; i++)
{
document.write(i, " = ", fibo(i), "
");
}

Pascal

program fibo;
uses SysUtils;

function fib(N : integer) : longint;
begin
if N < 2 then fib := 1
else fib := fib(N-2) + fib(N-1);
End;

var
NUM : integer;
f : longint;

begin
if ParamCount = 0 then
NUM := 1
else
NUM := StrToInt(ParamStr(1));

if NUM < 1 then NUM := 1;
f := fib(NUM);
WriteLn( IntToStr(f) );
PHP
function fibo($n)
{
return(($n < 2) ? 1 : fibo($n - 2) + fibo($n - 1));
}
$n = ($argc == 2) ? $argv[1] : 1;
$r = fibo($n);
print "$rn";
 
Python

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()
Sphere: Related Content

Feb 01

Leonard of Pisa or Fibonacci played an important role in reviving ancient mathematics and made significant contributions of his own. Liber abaci introduced the Hindu-Arabic place-valued decimal system and the use of Arabic numerals into Europe.

The golden ratio, usually denoted \varphi, expresses the relationship that the sum of two quantities is to the larger quantity as the larger is to the smaller. The golden ratio is the following algebraic irrational number with its numerical approximation:

\varphi = \frac{1 + \sqrt{5}}{2}\approx 1.618\,033\,989\,.

The figure of a golden section on the right illustrates the defining geometric relationship. Expressed algebraically:

\frac{a+b}{a} = \frac{a}{b} = \varphi\,.

A Quotation.

How many pairs of rabbits can be bred from one pair in a year?
A man has one pair of rabbits at a certain place entirely surrounded by a wall. We wish to know how many pairs will be bred from it in one year, if the nature of these rabbits is such that they breed every month one other pair and begin to breed in the second month after their birth. … Liber abaci (1202)

Sphere: Related Content