-
-
Save jcf/309194 to your computer and use it in GitHub Desktop.
This file contains 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
func Factorial(num int) int { | |
if num == 0 { | |
return 1; | |
} else { | |
return num * Factorial(num - 1); | |
} | |
} |
This file contains 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
factorial n = product [1..n] |
This file contains 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
<?php | |
function factorial($num) { | |
return ($num == 0) ? 1 : $num * factorial($num - 1); | |
} |
This file contains 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
sub factorial { | |
my $arg = shift; | |
return $arg == 1 ? 1 : $arg * factorial($arg - 1); | |
} |
This file contains 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
def factorial(num): | |
return (1 if num == 0 else num * factorial(num - 1)) |
This file contains 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
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