计算机编写程序的问题~~~~~~~~~~~~~~~

来源:百度知道 编辑:UC知道 时间:2024/05/10 05:04:20
<script language=vbs>
n=inputbox("n=")
b=true
for i=2 to n-1
if n mod i=0 then b=false
next
if b=true then
document.write("是素数!")
else
focument.write("不是素数!")
end if
</script>

这是一个计算机程序问题,输入一个数从而判断是否为素数!由于楼主是刚刚接触语言,所以里边写的......

你这样是得不到正确的结果的!

第一,你的想法不错!但是如果你小心一点可能会得到!focument.write("不是素数!") 这一句可能是你的笔误!但下次要小心点!

第二:其实,没有必要让计算机计算过长的时间!我是说没有必要计算到n-1整除了!只算到n的开方取整就可以了!int(sprt(n)),这样计算机会有很大的浪费时间!

第三,你的程序中少了一个东西!那就是如果N mod i等于0时就已经不是素数了,没有必要再循环下去!

第四,你的程序不能计算2是否为素数!既然是这样不如把2单独来写!而后没有必要再对2的倍数进行n mod i进行测试了!循环的步长就可以是2,这样速度快多了!

<script language=vbs>
n=inputbox("n=")
b=true
if n>2 then
if N mod 2 <> 0 then
for i=3 to int(sprt(n))+1 skip 2
if n mod i=0 then
b=false
break
end if
next
end if
end if
if b=true then
document.write("是素数!")
else
document.write("不是素数!")
end if
</script>

你的问题是什么啊!?