gpt4 book ai didi

javascript - 阶乘的最低有效非零数字

转载 作者:可可西里 更新时间:2023-11-01 13:50:36 25 4
gpt4 key购买 nike

我正在尝试计算阶乘中的最低有效非零数字。


我有以下片段:

$(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 中取 factorial2 的余数,所以至于在计算的每一步只保留非零数字。最终,我将 factorial2 的最终值乘以 5,然后将其转换为字符串并找到最后一次出现的非零数字字符串中的零位。

上面的代码似乎适用于 n=1,2.......,8 的值。但是在 n=9 时,代码返回最低有效非零数字 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">

关于javascript - 阶乘的最低有效非零数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35163930/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com