Skip to content

Instantly share code, notes, and snippets.

@whitehotlogic
Created June 2, 2018 00:36
Show Gist options
  • Save whitehotlogic/41e3b23c23cf3e156b56054bf149fc12 to your computer and use it in GitHub Desktop.
Save whitehotlogic/41e3b23c23cf3e156b56054bf149fc12 to your computer and use it in GitHub Desktop.
public int TrailingZerosInNFactorial(int n)
{
var factorialCalulation = calculateFactorial(n);
var trailingZerosInFactorial = getTrailingZeros(factorialCalculation);
return trailingZerosInFactorial;
}
private int calculateFactorial(int n)
{
var initialValue = n; // store the initial value, so we can manipulate n
var finalValue = 1; // start with nothing
for (int i = 0; i < initialValue; i++)
{
finalValue = finalValue * n;
n--;
}
}
private int getTrailingZeros(int n)
{
var baseTen = 10;
var zeroCount = default(int); // start with nothing
while (n % baseTen == 0)
{
n /= baseTen;
zeroCount++;
}
return zeroCount;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment