Last active
February 9, 2018 07:35
-
-
Save Beraliv/c7d31ffe3664f508042f14c17e64055e to your computer and use it in GitHub Desktop.
Task 69. UniLecs. Count the number of zeros at the end of the factorial of N, 1 <= N <= 10^9
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
const fact = n => n < 1 | |
? 1 | |
: Array(n) | |
.fill(0) | |
.map((_, i) => i + 1) | |
.reduce((a, v) => a * v, 1); | |
const countOf5 = n => Math.floor((n + 5) / 10); | |
const countOf10 = n => Math.floor(n / 10); | |
// step-by-step way | |
const countOfZeros = n => countOf5(n) + countOf10(n); | |
// the easiest way, count 5 and 10 | |
// const countOfZeros2 = n => Math.floor(n / 5); | |
// prevent incorrect e-typed numbers | |
for (let i = 0; i < 21; i++) { | |
const zeros = countOfZeros(i); | |
console.log(`factorial(${i}) = ${fact(i)} // ${zeros} zero${zeros % 10 === 1 ? '' : 's'}`); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output