Guest User

Untitled

a guest
Feb 21st, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.55 KB | None | 0 0
  1. const fact = n => n < 1
  2. ? 1
  3. : Array(n)
  4. .fill(0)
  5. .map((_, i) => i + 1)
  6. .reduce((a, v) => a * v, 1);
  7.  
  8. const countOf5 = n => Math.floor((n + 5) / 10);
  9. const countOf10 = n => Math.floor(n / 10);
  10.  
  11. // step-by-step way
  12. const countOfZeros = n => countOf5(n) + countOf10(n);
  13. // the easiest way, count 5 and 10
  14. // const countOfZeros2 = n => Math.floor(n / 5);
  15.  
  16. // prevent incorrect e-typed numbers
  17. for (let i = 0; i < 21; i++) {
  18. const zeros = countOfZeros(i);
  19. console.log(`factorial(${i}) = ${fact(i)} // ${zeros} zero${zeros % 10 === 1 ? '' : 's'}`);
  20. }
Add Comment
Please, Sign In to add comment