我很难理解这个伪代码并将其转换成跟踪 table
I am having difficulty understanding this pseudocode and converting it into a trace table
我刚刚开始编程,遇到了一个让我困惑的问题。
我需要用它来填充一个痕迹table但是我真的不明白这个伪代码是什么意思
read in value of n
p ← true
for i is 2 to (n – 1)
if( n MOD i = 0)
p ← false
end if
end for loop
output p
这里是问题图片的link
http://s15.postimg.org/517iinbmz/Psuedocode.png
这是使用 "simple" 循环而不是 "for" 的伪代码的简单版本。
read the value of n
set p to true
set i to 2
loop until i is equal to n-1
if the module of n/i is equals to 0 //Basically checking if n is divisible by i
set p to false
endif
increment i by 1 //Basically i = i + 1
endloop
output p
有了这个,你应该能够弄清楚代码在做什么:) 不是为了告诉你答案,因为你应该自己思考并找到答案。
编辑:
为了清楚起见,您可以将 for 视为 "while loop" 的快捷方式。
基本上,您只需将迭代变量 (i) 设置为初始值,启动循环,检查是否满足停止条件,并在每次迭代后将迭代变量 (i) 递增 1(或递增 2,3,4.. .,9999999999:P).
因此,在示例中,for = "set i initial value to 2 then do the loop while i is different than n-1 and also increment the value of i by 1 every time you iterate"
我刚刚开始编程,遇到了一个让我困惑的问题。 我需要用它来填充一个痕迹table但是我真的不明白这个伪代码是什么意思
read in value of n
p ← true
for i is 2 to (n – 1)
if( n MOD i = 0)
p ← false
end if
end for loop
output p
这里是问题图片的link http://s15.postimg.org/517iinbmz/Psuedocode.png
这是使用 "simple" 循环而不是 "for" 的伪代码的简单版本。
read the value of n
set p to true
set i to 2
loop until i is equal to n-1
if the module of n/i is equals to 0 //Basically checking if n is divisible by i
set p to false
endif
increment i by 1 //Basically i = i + 1
endloop
output p
有了这个,你应该能够弄清楚代码在做什么:) 不是为了告诉你答案,因为你应该自己思考并找到答案。
编辑: 为了清楚起见,您可以将 for 视为 "while loop" 的快捷方式。 基本上,您只需将迭代变量 (i) 设置为初始值,启动循环,检查是否满足停止条件,并在每次迭代后将迭代变量 (i) 递增 1(或递增 2,3,4.. .,9999999999:P).
因此,在示例中,for = "set i initial value to 2 then do the loop while i is different than n-1 and also increment the value of i by 1 every time you iterate"