1.求1!+2!+3!+...+10!的和弊予?
這是數(shù)學(xué)中的階乘和計(jì)算翼岁!
請(qǐng)輸入階乘和的個(gè)數(shù)<input type="text" id="box1" />
<button id="btn">計(jì)算</button>
<input type="text" id="box2" />
<script type="text/javascript">
var box1 = document.getElementById("box1");
var box2 = document.getElementById("box2");
document.getElementById("btn").onclick = function() {
var a = parseInt(box1.value);
var sum = 0 ,n = 1;
for(var i=1; i <= a; i++) {
n = i * n;//第a個(gè)階乘的表達(dá)式
sum = sum + n;//階乘和
box2.value = sum;
}
}
</script>
2.對(duì)一個(gè)整數(shù)分解質(zhì)因數(shù)悦析。例如: 90 = 2 * 3 * 3 * 5寿桨??
<script type="text/javascript">
var num = parseInt(prompt("請(qǐng)輸入一個(gè)數(shù)"));
document.write(num + "=");
for(var i = 2; i <= num; i++) {
if(num % i === 0) {
if(i === num) {
document.write(i);//去掉最后一個(gè)數(shù)后面的“*”
} else {
document.write(i + "*");
num /= i;
i--;//計(jì)算出num中所有的最小i值
}
}
}
</script>