Skip to content

Instantly share code, notes, and snippets.

@pietrom
Created December 20, 2017 08:09
Show Gist options
  • Save pietrom/4a6665a083ba84ae5f054ff3a0854db9 to your computer and use it in GitHub Desktop.
Save pietrom/4a6665a083ba84ae5f054ff3a0854db9 to your computer and use it in GitHub Desktop.
Calculate the number of leading zeroes in n!
function range(first, last) {
const length = last - first + 1
return new Array(length).fill(-1, 0, length).map(function(v, i) { return i + first; })
}
function factZeros(n) {
return range(1, Math.floor(Math.log(n) / Math.log(5))).map(x => Math.pow(5, x)).map(x => Math.floor(n / x)).reduce((acc, curr) => acc + curr, 0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment