Skip to content

Instantly share code, notes, and snippets.

@reu
Created June 12, 2010 17:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reu/435914 to your computer and use it in GitHub Desktop.
Save reu/435914 to your computer and use it in GitHub Desktop.
The famous factorials (aula de introdução a programação)
#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;
}
#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);
}
class Integer
def factorial
if self == 0
1
else
self * (self - 1).factorial
end
end
end
puts ARGV[0].to_i.factorial
# 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