Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jcf
Forked from jamierumbelow/Math.factorial.rb
Created February 20, 2010 17:03
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 jcf/309771 to your computer and use it in GitHub Desktop.
Save jcf/309771 to your computer and use it in GitHub Desktop.
int factorial( int num ) {
if ( num <= 1 ) {
return 1;
} else {
return num * factorial( num-1 );
}
}
func Factorial(num int) int {
if num == 0 {
return 1;
} else {
return num * Factorial(num - 1);
}
}
factorial n = product [1..n]
public class Factorial {
public static int factorial(int num) {
if (num == 0) {
return 1;
} else {
return num * factorial(num - 1);
}
}
}
function factorial(n) {
return (n == 0 ? 1 : n * factorial(n - 1));
}
function factorial($num) {
return ($num == 0) ? 1 : $num * factorial($num - 1);
}
sub factorial {
my $arg = shift;
return $arg == 1 ? 1 : $arg * factorial($arg - 1);
}
def factorial(num):
return (1 if num == 0 else num * factorial(num - 1))
module Math
def self.factorial(f)
f == 0 ? 1 : f * factorial(f - 1)
end
end
# Or we can extend an instance of Fixnum so we can do 5.factorial :)
class Fixnum
def factorial
self == 0 ? 1 : self * (self - 1).factorial
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment