Skip to content

Instantly share code, notes, and snippets.

@Gladozzz
Last active December 17, 2020 10:19
Show Gist options
  • Save Gladozzz/febef402bfa933c08f9a597d1df6d2ed to your computer and use it in GitHub Desktop.
Save Gladozzz/febef402bfa933c08f9a597d1df6d2ed to your computer and use it in GitHub Desktop.
Number of zeroes in end of factorial.
var getZeroesOfFactorial = function (n) {
var number_of_2 = 0;
var number_of_5 = 0;
var number_of_zeroes_of_factorial = 0;
for (var i = 2; i <= n; i++) {
var currentValue = i;
console.log(i);
while (currentValue >= 5) {
if (currentValue % 5 == 0) {
console.log(currentValue);
currentValue = currentValue / 5;
number_of_5++;
} else {
break;
}
}
currentValue = i;
while (currentValue >= 2) {
if (currentValue % 2 == 0) {
console.log(currentValue);
currentValue = currentValue / 2;
number_of_2++;
} else {
break;
}
}
}
if (number_of_5 <= number_of_2) {
number_of_zeroes_of_factorial = number_of_5;
} else {
number_of_zeroes_of_factorial = number_of_2;
}
console.log("number_of_2 = " + String(number_of_2));
console.log("number_of_5 = " + String(number_of_5));
return number_of_zeroes_of_factorial;
};
var userNumber = prompt("введите число");
var factorial_zeroes = getZeroesOfFactorial(userNumber);
console.log("zeroes = " + String(factorial_zeroes));
alert(factorial_zeroes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment