Last active
November 16, 2023 12:18
-
-
Save lrlucena/31aeecb46b8dc569a42795432cf4044c to your computer and use it in GitHub Desktop.
Recursão
This file contains hidden or 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
# Análise Combinatória | |
comb(n, k: Inteiro): Inteiro = escolha k | |
caso 1 => n | |
caso k se k == n => 1 | |
caso k => comb(n-1, k-1) + comb(n-1, k) | |
fim | |
escreva comb(6, 2) |
This file contains hidden or 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
comb(n, k: Inteiro): Inteiro = | |
se k == n | |
então 1 | |
senãose k == 1 | |
então n | |
senão comb(n-1, k-1) + comb(n-1, k) | |
fim | |
escreva comb(10, 4) |
This file contains hidden or 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
divisores(n: Inteiro) = d(n, n) | |
d(n, i: Inteiro): Lista[Inteiro] = | |
se i < 1 então | |
[] | |
senão | |
divisivel = n mod i == 0 | |
se divisivel | |
então d(n, i-1) + [i] | |
senão d(n, i-1) | |
fim | |
fim | |
escreva divisores(12) |
This file contains hidden or 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
divisores(n: Inteiro) = d(n, n) | |
d(n, i: Inteiro): Lista[Inteiro] = escolha i | |
caso 0 => [] | |
caso i se n mod i == 0 => d(n, i-1) + [i] | |
caso i => d(n, i-1) | |
fim | |
escreva divisores(12) |
This file contains hidden or 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
divisores(n: Inteiro) = d(n, n) | |
d(n, i: Inteiro): Lista[Inteiro] = escolha i | |
caso 0 => [] | |
caso i => se n mod i == 0 | |
então d(n, i-1) + [i] | |
senão d(n, i-1) | |
fim | |
fim | |
escreva divisores(12) |
This file contains hidden or 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
fatorial(n: Inteiro): Inteiro = escolha n | |
caso 1 => 1 | |
caso n => n * fatorial(n-1) | |
fim | |
escreva fatorial(5) |
This file contains hidden or 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
mdc(a, b: Inteiro): Inteiro = escolha (a, b) | |
caso (a, b) se a < b => mdc(a, b-a) | |
caso (a, b) se a > b => mdc(a-b, a) | |
caso (a, b) => a | |
fim | |
escreva mdc(10, 12) |
This file contains hidden or 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
mdc(a, b: Inteiro): Inteiro = escolha a - b | |
caso n se n > 0 => mdc(b, n) | |
caso n se n < 0 => mdc(-n, a) | |
caso _ => a | |
fim | |
escreva mdc(10, 12) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment