Last active
April 29, 2020 13:10
-
-
Save AspenForester/018fec337ff494638d77555a745c4846 to your computer and use it in GitHub Desktop.
Factorial with PowerShell
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
function Factorial ([bigint]$x) | |
{ | |
if ($x -ge 1) | |
{ | |
return $x * (Factorial ($x = $x - 1)) | |
} | |
elseif ($x -eq 0) | |
{ | |
return 1 | |
} | |
else | |
{ | |
throw "NaN" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Based on a Gist that @dfinke once posted, but correctly returns 0! = 1 and throws an error for inputs less than 0
Inspired by https://www.youtube.com/watch?v=ZxYOEwM6Wbk and Grant Sanderson, even though .Net provides an exp() method, it was fun to recreate it with this Factorial function in Powershell