Last active
August 27, 2017 21:40
-
-
Save marvinferreira/31ace758e5b2bdfb82b6a733f3566fc2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Recursividade { | |
public static void main(String[] args){ | |
} | |
public static int fatorialRecursivo(int num) { | |
/** | |
* Este é o caso base, se o número passado por parametro for 0 ou 1, | |
* ele retorna o resultado 1 e finaliza o método. | |
*/ | |
if (num <= 1) { | |
return 1; | |
} else { | |
/** | |
* chama o método fatorial novamente, mas dessa vez enviando como | |
* parametro (n - 1). | |
*/ | |
return fatorial(num - 1) * num; | |
} | |
} | |
public static int factorialIterativo(int n) { | |
if (n == 0) { | |
return 1; | |
} | |
int fact = 1; | |
for (int i = 1; i <= n; i++) { | |
fact *= i; | |
} | |
return fact; | |
} | |
public static int FibIterativo (int n) | |
{ | |
int i = 0; | |
int j = 1; | |
for (int k = 1; k < n; k++) | |
{ | |
int t; | |
t = i + j; | |
i = j; | |
j = t; | |
} | |
return j; | |
} | |
public static int FibRecursivo(int n) | |
{ | |
if (n < 2) | |
return n; | |
else | |
return fibonacci_recursivo(n-2) + fibonacci_recursivo(n-1); | |
} | |
public static void DecimalParaBinarioRec(int num) { | |
if (num>0) { | |
DecimalParaBinarioRec(num/2); | |
System.out.print(num%2 + " "); | |
} | |
} | |
public static String DecimalParaBinarioIterativo(int result){ | |
String binario = ""; | |
while (result > 0) | |
{ | |
binario = (result % 2) + binario; | |
result = result / 2; | |
} | |
return binario; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment