Skip to content

Instantly share code, notes, and snippets.

@AspenForester
Last active April 29, 2020 13:10
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 AspenForester/018fec337ff494638d77555a745c4846 to your computer and use it in GitHub Desktop.
Save AspenForester/018fec337ff494638d77555a745c4846 to your computer and use it in GitHub Desktop.
Factorial with PowerShell
function Factorial ([bigint]$x)
{
if ($x -ge 1)
{
return $x * (Factorial ($x = $x - 1))
}
elseif ($x -eq 0)
{
return 1
}
else
{
throw "NaN"
}
}
@AspenForester
Copy link
Author

AspenForester commented Apr 28, 2020

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

@dfinke
Copy link

dfinke commented Apr 28, 2020

Thanks for posting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment