Created
June 12, 2010 17:37
-
-
Save reu/435914 to your computer and use it in GitHub Desktop.
The famous factorials (aula de introdução a programaçã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
#include <stdio.h> | |
#include <stdlib.h> | |
int main(int argc, char *argv[]) | |
{ | |
printf("%i\n", factorial(atoi(argv[1]))); | |
} | |
int factorial(int number) | |
{ | |
int result = 1; | |
while(number > 1){ | |
result = result * number; | |
number = number - 1; | |
} | |
return result; | |
} |
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
#include <stdio.h> | |
#include <stdlib.h> | |
int main(int argc, char *argv[]) | |
{ | |
printf("%i\n", factorial(atoi(argv[1]))); | |
} | |
int factorial(int number) | |
{ | |
if(number <= 1) | |
return 1; | |
else | |
return number * factorial(number - 1); | |
} |
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
class Integer | |
def factorial | |
if self == 0 | |
1 | |
else | |
self * (self - 1).factorial | |
end | |
end | |
end | |
puts ARGV[0].to_i.factorial |
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
# Adeus stack overflow | |
class Integer | |
def factorial | |
1.upto(self).inject(1){ |result, current_number| result * current_number } | |
end | |
end | |
puts ARGV[0].to_i.factorial |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment