图形计算器逻辑测试错误
我尝试制作以下程序,一开始效果很好。
Label 1
Input "n=", Q
If Q≤0
...Then
Goto 1
Else
Label 2
((Q-2int(Q/2))(3Q+1)+(1-Q+2int(Q/2))(Q/2))->Q
Display Q
If Q≠1
Then
Goto 2
Else
Goto 1
这样做的目的是输入一个起始数字,如果它是偶数,则执行Q/2
。如果是奇数,则执行 3Q+1
并将结果存储回 Q
。这就是 Collatz 猜想,它指出,如果进行此迭代,无论您从什么数字开始,它总是会降到 1。有些起始数字需要更多步骤才能达到,而有些则需要更少步骤。
问题:
有时程序经过大量迭代后,会显示 ERROR,当我按下 debug 时,它会将光标移到 if Q≠1
中的 Q
上代码>.
你知道为什么这种情况不断发生吗?谢谢。
I tried making the following program and it worked fine at first.
Label 1
Input "n=", Q
If Q≤0
...Then
Goto 1
Else
Label 2
((Q-2int(Q/2))(3Q+1)+(1-Q+2int(Q/2))(Q/2))->Q
Display Q
If Q≠1
Then
Goto 2
Else
Goto 1
What this is supposed to do is I put in a starting number, and if its an even number, it does Q/2
. if its odd, it does 3Q+1
and stores the result back into Q
. This is the Collatz Conjecture, which states that if this iteration is followed through, no matter what number you start out with, it will always come down to 1. some starting numbers take more steps to reach while some take fewer steps.
The problem:
Sometimes after the program goes through a large number of iterations, it shows up ERROR, and when I press debug, it takes my cursor over the Q
in if Q≠1
.
Do you know why this keeps happening? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
大多数人在做这个谜题时必须克服的障碍是由巨大数字引起的溢出——我认为它发生在 23 左右的数字上,IIRC。情况可能是这样,但我不明白为什么你的光标会移到 Q 上。
The hurdle that most people have to overcome when doing this puzzle is the overflow caused by huge numbers -- I think that it happens at a number somewhere around 23, IIRC. That might be the case, but I can't see why it would take your cursor over the Q.