Commit 1940236e authored by kenkeiras's avatar kenkeiras

Corrected fibonacci calculation

parent 74080b9b
$mode ascii
fibonacci n: if (n < 2) 1 else (fibonacci(n - 1)) + (fibonacci(n - 2))
fibonacci n: if (n < 2) n else (fibonacci(n - 1)) + (fibonacci(n - 2))
numero := 10
resultado := fibonacci numero
......
......@@ -5,7 +5,7 @@ procedure Fibonacci_Recursivo is
function Fibonacci(n : in Integer) return Integer is
begin
if n < 2 then
return 1;
return n;
else
return Fibonacci(n - 1) + Fibonacci(n - 2);
end if;
......
fibonacci(){
if [ $1 -lt 2 ];then
echo 1;
echo $1;
else
echo $(( `fibonacci $(( $1 - 1 ))` + `fibonacci $(( $1 - 2 ))` ));
fi
......
PUBLIC SUB fibonacci(n AS Integer) AS Integer
IF n < 2 THEN
RETURN 1
RETURN n
ELSE
RETURN (fibonacci(n - 1) + fibonacci(n - 2))
ENDIF
......
......@@ -4,7 +4,7 @@ class Program{
static int fibonacci(int n){
if (n < 2){
return 1;
return n;
}
else{
return fibonacci(n - 1) + fibonacci(n - 2);
......
......@@ -2,7 +2,7 @@
int fibonacci(int n){
if (n < 2){
return 1;
return n;
}
else{
return fibonacci(n - 1) + fibonacci(n - 2);
......
(defun fibonacci (n)
(if (< n 2)
1
n
(+ (fibonacci (- n 1)) (fibonacci (- n 2)))
)
)
......
-module(fibonacci_recursivo).
-export([start/0, fibonacci/1]).
fibonacci(0) -> 1;
fibonacci(0) -> 0;
fibonacci(1) -> 1;
fibonacci(N) -> fibonacci(N - 1) + fibonacci(N - 2).
......
......@@ -12,7 +12,7 @@ RECURSIVE FUNCTION fibonacci(n) RESULT (r)
INTEGER :: n, r
IF (n < 2) THEN
r = 1
r = n
ELSE
r = fibonacci(n - 1) + fibonacci(n - 2)
END IF
......
......@@ -4,7 +4,7 @@ import "fmt"
func fibonacci(n int)(r int){
if (n < 2){
r = 1
r = n
}else{
r = fibonacci(n - 1) + fibonacci(n - 2)
}
......
import Text.Printf
fibonacci 0 = 1
fibonacci 0 = 0
fibonacci 1 = 1
fibonacci n = fibonacci(n - 1) + fibonacci (n - 2)
......
class fibonacci_recursivo{
static int fibonacci(int n){
if (n < 2){
return 1;
return n;
}
else{
return fibonacci(n - 1) + fibonacci(n - 2);
......
function fibonacci(n){
if (n < 2){
return 1;
return n;
}
else{
return fibonacci(n - 1) + fibonacci(n - 2);
......@@ -14,5 +14,5 @@ var resultado = fibonacci(numero);
//document.write("Elemento nº "+ numero +" de fibonacci: "+resultado+"<br />");
// Para nodejs
//process.stdout.write("Elemento nº "+ numero +" de fibonacci: "+resultado+"\n");
process.stdout.write("Elemento nº "+ numero +" de fibonacci: "+resultado+"\n");
TO fibonacci :n
if :n < 2 [output 1]
if :n < 2 [output :n]
output sum fibonacci difference n 2 fibonacci difference n 1
end
......
function fibonacci(n)
if n < 2 then
return 1
return n
else
return fibonacci(n - 1) + fibonacci(n - 2)
end
......
......@@ -2,7 +2,7 @@
int fibonacci(int n){
if (n < 2){
return 1;
return n;
}
else{
return fibonacci(n - 1) + fibonacci(n - 2);
......
let rec fibonacci n = if n < 2 then 1
let rec fibonacci n = if n < 2 then n
else fibonacci (n - 1) + fibonacci(n - 2)
in
let numero = 10 in
......
function ret = fibonacci(n)
if (n < 2)
ret = 1;
ret = n;
else
ret = fibonacci(n - 1) + fibonacci(n - 2);
end
......
......@@ -4,7 +4,7 @@ function fibonacci(n: integer): integer;
var r: integer;
begin
if (n < 2) then
r := 1
r := n
else
r := fibonacci(n - 1) + fibonacci(n - 2);
fibonacci := r;
......
sub fibonacci{
my $n = $_[0];
if ($n < 2){
return 1;
return $n;
}
else{
return fibonacci($n - 1) + fibonacci($n - 2);
......
<?php
function fibonacci($n){
if ($n < 2){
return 1;
return $n;
}
else{
return fibonacci($n - 1) + fibonacci($n - 2);
......
def fibonacci(n):
if n < 2:
return 1
return n
else:
return fibonacci(n - 1) + fibonacci(n - 2)
......
def fibonacci(n):
if n < 2:
return 1
return n
else:
return fibonacci(n - 1) + fibonacci(n - 2)
......
fibonacci <- function(n){
return (ifelse (n < 2, 1, fibonacci(n - 2) + fibonacci(n - 1)))
return (ifelse (n < 2, n, fibonacci(n - 2) + fibonacci(n - 1)))
}
numero <- 10
......
def fibonacci(n)
if n < 2
return 1
return n
else
return fibonacci(n - 1) + fibonacci(n - 2)
end
......
def fibonacci(n: Int): Int = if (n < 2) 1
def fibonacci(n: Int): Int = if (n < 2) n
else fibonacci(n - 1) + fibonacci(n - 2)
val numero = 10
......
(define (fibonacci n)
(if (< n 2)
1
n
(+ (fibonacci (- n 2)) (fibonacci (- n 1)))
)
)
......
proc fibonacci n {
if { $n < 2 } {
return 1
return $n
} else {
return [expr [fibonacci [expr $n - 1]] + [fibonacci [expr $n - 2]]]
}
......
procedure fibonacci(n)
if (n < 2) then
return 1
return n
else
return fibonacci(n - 1) + fibonacci(n - 2);
end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment