阶乘的最低有效非零数字
Least significant non-zero digit of a factorial
我正在尝试计算阶乘中的最低有效非零数字。
我有以下片段:
$(document).ready(function() {
$('#submit').click(function() {
var n = $('#number').val();
get_result(n);
});
});
function get_result(n) {
var factorial = 1;
var factorial2 = 1;
for (i = 1; i <= n; i++) {
factorial = factorial * i;
}
var count_5 = 0;
for (j = 1; j <= n; j++) {
if (j % 5 != 0) {
factorial2 = factorial2 * (j % 10);
factorial2 = factorial2 % 10;
} else if (j % 5 == 0) {
count_5 = 1;
}
}
if (count_5 == 1) {
factorial2 = factorial2 * 5;
}
console.log(factorial2);
factorial2 = factorial2.toString();
var digit = 0;
for (i = 0; i < factorial2.length; i++) {
if (factorial2[i] != '0') {
digit = factorial2[i];
}
}
$('#display').text("Factorial of " + n + " is " + factorial);
$('#display2').text("Least significant digit of Factorial of " + n + " is " + digit);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="display">
</div>
<div id="display2">
</div>
<input type="text" value="" id="number">
<input type="submit" id="submit">
作为上述代码的一部分,为了计算最低有效非零数字,我首先忽略所有 5 的倍数,其次,在阶乘计算的每一步,我从10 为了在computation.Eventually的每一步只保留非零数字,我将factorial2
的最终值乘以5然后将其转换为字符串并找到非零的最后一次出现- 字符串中的数字为零。
以上代码似乎适用于 n=1,2.......,8 的值。但是在 n=9 时,代码 returns 最低有效非零数字为 3 而它应该返回 8.
For example : Factorial(9) = 362880 , hence least significant non-zero digit = 8.
可能是什么错误,我应该如何纠正它?还有另一种性能更好的方法来计算这个结果吗?
Note : I've included the code to calculate the factorial just for verification purposes, my ultimate aim is to just calculate the least significant non-zero digit and not the factorial for a worst possible case when n is a billion(when actually computing and reading the factorial is not feasible or advisable).
问题是 5 不会简单地消失。它们与 2 结合生成 0。因此,在 5 的倍数(如 15 或 35)或具有很多 2 的幂(如 24)的数字之后,你会遇到问题。最好的办法可能是记录 2 的个数,并在每个 5 的倍数时减少它(2 总是多于 5)。 (此外,一旦您费尽心思找到了不带 0 的数字,就无需将其转换为字符串。)
$(document).ready(function() {
$('#submit').click(function() {
var n = $('#number').val();
get_result(n);
});
});
function get_result(n) {
var factorial = 1;
var factorial2 = 1;
for ( var i = 1; i <= n; i++ ) {
factorial = factorial * i;
}
var extra2s = 0;
for ( var j = 1; j <= n; j++ ) {
var jcopy = j;
while( jcopy%10 == 0 ) {
jcopy /= 10;
}
while( jcopy%2==0 ) {
extra2s++;
jcopy /= 2;
}
while( jcopy%5==0 ) {
extra2s--;
jcopy /= 5;
}
jcopy %= 10;
factorial2 = (factorial2 * jcopy)%10;
}
for ( var k = 0 ; k < extra2s ; k++ ) {
factorial2 = (factorial2 * 2)%10;
}
var digit = factorial2;
$('#display').text("Factorial of " + n + " is " + factorial);
$('#display2').text("Least significant digit of Factorial of " + n + " is " + digit);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="display">
</div>
<div id="display2">
</div>
<input type="text" value="" id="number">
<input type="submit" id="submit">
我正在尝试计算阶乘中的最低有效非零数字。
我有以下片段:
$(document).ready(function() {
$('#submit').click(function() {
var n = $('#number').val();
get_result(n);
});
});
function get_result(n) {
var factorial = 1;
var factorial2 = 1;
for (i = 1; i <= n; i++) {
factorial = factorial * i;
}
var count_5 = 0;
for (j = 1; j <= n; j++) {
if (j % 5 != 0) {
factorial2 = factorial2 * (j % 10);
factorial2 = factorial2 % 10;
} else if (j % 5 == 0) {
count_5 = 1;
}
}
if (count_5 == 1) {
factorial2 = factorial2 * 5;
}
console.log(factorial2);
factorial2 = factorial2.toString();
var digit = 0;
for (i = 0; i < factorial2.length; i++) {
if (factorial2[i] != '0') {
digit = factorial2[i];
}
}
$('#display').text("Factorial of " + n + " is " + factorial);
$('#display2').text("Least significant digit of Factorial of " + n + " is " + digit);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="display">
</div>
<div id="display2">
</div>
<input type="text" value="" id="number">
<input type="submit" id="submit">
作为上述代码的一部分,为了计算最低有效非零数字,我首先忽略所有 5 的倍数,其次,在阶乘计算的每一步,我从10 为了在computation.Eventually的每一步只保留非零数字,我将factorial2
的最终值乘以5然后将其转换为字符串并找到非零的最后一次出现- 字符串中的数字为零。
以上代码似乎适用于 n=1,2.......,8 的值。但是在 n=9 时,代码 returns 最低有效非零数字为 3 而它应该返回 8.
For example : Factorial(9) = 362880 , hence least significant non-zero digit = 8.
可能是什么错误,我应该如何纠正它?还有另一种性能更好的方法来计算这个结果吗?
Note : I've included the code to calculate the factorial just for verification purposes, my ultimate aim is to just calculate the least significant non-zero digit and not the factorial for a worst possible case when n is a billion(when actually computing and reading the factorial is not feasible or advisable).
问题是 5 不会简单地消失。它们与 2 结合生成 0。因此,在 5 的倍数(如 15 或 35)或具有很多 2 的幂(如 24)的数字之后,你会遇到问题。最好的办法可能是记录 2 的个数,并在每个 5 的倍数时减少它(2 总是多于 5)。 (此外,一旦您费尽心思找到了不带 0 的数字,就无需将其转换为字符串。)
$(document).ready(function() {
$('#submit').click(function() {
var n = $('#number').val();
get_result(n);
});
});
function get_result(n) {
var factorial = 1;
var factorial2 = 1;
for ( var i = 1; i <= n; i++ ) {
factorial = factorial * i;
}
var extra2s = 0;
for ( var j = 1; j <= n; j++ ) {
var jcopy = j;
while( jcopy%10 == 0 ) {
jcopy /= 10;
}
while( jcopy%2==0 ) {
extra2s++;
jcopy /= 2;
}
while( jcopy%5==0 ) {
extra2s--;
jcopy /= 5;
}
jcopy %= 10;
factorial2 = (factorial2 * jcopy)%10;
}
for ( var k = 0 ; k < extra2s ; k++ ) {
factorial2 = (factorial2 * 2)%10;
}
var digit = factorial2;
$('#display').text("Factorial of " + n + " is " + factorial);
$('#display2').text("Least significant digit of Factorial of " + n + " is " + digit);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="display">
</div>
<div id="display2">
</div>
<input type="text" value="" id="number">
<input type="submit" id="submit">