Skip to content

Instantly share code, notes, and snippets.

@jamierumbelow
Forked from jcf/Math.factorial.rb
Created February 19, 2010 22:01
Show Gist options
  • Save jamierumbelow/309264 to your computer and use it in GitHub Desktop.
Save jamierumbelow/309264 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 (num) {
if(num == 0) return 1;
return (num * factorial (num - 1) );
}
<?php
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