Skip to content

Instantly share code, notes, and snippets.

@williaanlopes
Last active August 31, 2018 20:16
Show Gist options
  • Save williaanlopes/98845b9b68a7e4197fe329035ab28a98 to your computer and use it in GitHub Desktop.
Save williaanlopes/98845b9b68a7e4197fe329035ab28a98 to your computer and use it in GitHub Desktop.
Invert a number using recursion in various languages. (Inverter um numero usando recursividade em várias linguagens.)
// Linguagem C
// funcao recursiva para expoente
int expoente(int e)
{
if(e == 0) {
return 0;
} else {
return 1 + expoente (e/10);
}
}
// funcao recursisa que inverte o numero
int inverte(int n)
{
if(n == 0) {
return 0;
} else {
return ((n%10) * pow(10, expoente (n/10))) + inverte (n/10);
}
}
-- Linguagem HASKELL
-- funcao recursiva para expoente
expoente :: Int -> Int
expoente e
| e == 0 = 0
| otherwise = 1 + expoente (div e 10)
-- funcao recursisa que inverte o numero
inverte :: Int -> Int
inverte n
| n == 0 = 0
| otherwise = ((mod n 10) * (10 ^ expoente (div n 10))) + inverte (div n 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment