Created
June 2, 2018 00:36
-
-
Save whitehotlogic/41e3b23c23cf3e156b56054bf149fc12 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
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