Skip to content

Instantly share code, notes, and snippets.

@victorhtorres
Created July 26, 2016 15:41
Show Gist options
  • Save victorhtorres/777dcd9c47555f1eb2a1d6aa16d0a883 to your computer and use it in GitHub Desktop.
Save victorhtorres/777dcd9c47555f1eb2a1d6aa16d0a883 to your computer and use it in GitHub Desktop.
import java.util.Scanner;
/**
*
* @author vtorr_000
*/
public class Main {
// Fibonacci recursivo por pila
public static int fiboPila(int n) {
switch (n) {
case 0:
return 0;
case 1:
return 1;
default:
return fiboPila(n - 1) + fiboPila(n - 2);
}
}
// fibonacci recursivo por cola
public static int fiboCola(int n) {
int r = 1, k = 0;
return fiboCola(n, r, k);
}
private static int fiboCola(int n, int r, int k) {
switch (n) {
case 0:
return k;
case 1:
return r;
default:
return fiboCola(n-1, r + k, r);
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Digite número:");
int input = scanner.nextInt();
System.out.printf("El número fibonacci para %d por método pila es %d\n", input, fiboPila(input));
System.out.printf("El número fibonacci para %d por método cola es %d\n", input, fiboCola(input));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment