嵌套循环的JavaScript-想逐步了解计算
我是初学者。 谁能让我在以下代码中逐步知道“嵌套为循环”的逐步(每个步骤 /条件),以查找两个数字之间的质量数。
const number1 = parseInt(prompt("Enter the lower number"));
const number2 = parseInt(prompt("Enter the higher number"));
console.log(`The prime numbers between ${number1} and ${number2} are: `);
for (let i = number1; i <= number2; i++) {
let flag = 0;
for (let j = 2; j < i; j++) {
if (i % j == 0) {
flag = 1;
break;
}
}
if (i > 1 && flag == 0) console.log(i);
}
I'm a beginner.
Can anyone let me know the step by step (each step / condition) of a "nested for loop" in the below code written for finding Prime Numbers between two numbers.
const number1 = parseInt(prompt("Enter the lower number"));
const number2 = parseInt(prompt("Enter the higher number"));
console.log(`The prime numbers between ${number1} and ${number2} are: `);
for (let i = number1; i <= number2; i++) {
let flag = 0;
for (let j = 2; j < i; j++) {
if (i % j == 0) {
flag = 1;
break;
}
}
if (i > 1 && flag == 0) console.log(i);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您是初学者,则可以理解算法的一个好方法是以具体的示例进行示例,并在纸上手动进行遵循代码会发生什么。我们可以说3和5是:
- &gt;低:3,高:5
我们将输入第一个,它将用
i
使用3
1st初始化:
i = 3
0
声明flag
(如果它变成1
,则该数字不是Prime)。i
(此时为3
)J = 2
i
和J
是 0 (3%2
)之间的其余部分。3%2
是1
,不是0
,因此我们将继续进行下一个迭代j = 3
,因为i&lt的条件; J
不尊重(3&lt; 3
false
)i
是否大于1
(3&gt; 1
true
)和if <代码> flag 是0
(是,所以我们将3
记录到控制台)console.log(3)
将输出3
1st for:
i = 4
0
(如果它变为1
,该号码不是Prime)。2
检查到i
(此时为4
)J = 2
i
和j
是0
(4%2
)之间的其余部分。4%2
是0
,因此flag
将成为1
,我们脱离了第二个。i
是否大于1
(4&gt; 1
)和flag
是0
(不是,所以我们不显示该号码)1st for:
i = 5
0
声明flag
(如果它变成1
,则该数字不是Prime)。i
(此时为5
)J = 2
i
和J
是 0 (5%2
)之间的其余部分。5%2
是1
,不是0
,因此我们将继续进行下一个迭代j = 3
i
和J
是0
(5%3
)之间的其余部分。5%3
是2
,不是0
,因此我们将继续进行下一个迭代J = 4
i
和J
是0
(5%4
)之间的其余部分。5%4
是1
,不是0
,因此我们将继续进行下一个迭代j = 5
,因为i&lt的条件; J
不尊重(5&lt; 5
false
)。i
是否大于1
(5&gt; 1
true
)和if <代码> flag 是0
(是,所以我们将5
记录到控制台)console.log(5)
将输出5
程序的最终结果将是记录
3
和5
作为素数。希望这有所帮助。
A good way to understand an algorithm if you're a beginner is to take a concrete example, and do manually on a paper what happens by following the code. We can say 3 and 5 are the numbers:
-> low: 3, high: 5
We will enter the first for, which will initialize
i
with3
1st for:
i = 3
flag
with the value0
(if it ever becomes1
, the number is not prime).i
(which at this point is3
)j = 2
i
andj
is0
(3 % 2
)3 % 2
is1
, which is not0
, so we will move on to the next iterationj = 3
, finishes because condition ofi < j
is not respected (3 < 3
false
)i
is greater than1
(3 > 1
true
) and ifflag
is0
(which it is, so we log3
to the console)console.log(3)
will output3
1st for:
i = 4
0
(if it ever becomes1
, the number is not prime).2
toi
(which at this point is4
)j = 2
i
andj
is0
(4 % 2
)4 % 2
is0
, soflag
will become1
and we break out of the second for.i
is greater than1
(4 > 1
) and ifflag
is0
(which is not, so we do not show the number)1st for:
i = 5
flag
with the value0
(if it ever becomes1
, the number is not prime).i
(which at this point is5
)j = 2
i
andj
is0
(5 % 2
)5 % 2
is1
, which is not0
, so we will move on to the next iterationj = 3
i
andj
is0
(5 % 3
)5 % 3
is2
, which is not0
, so we will move on to the next iterationj = 4
i
andj
is0
(5 % 4
)5 % 4
is1
, which is not0
, so we will move on to the next iterationj = 5
, finishes because condition ofi < j
is not respected (5 < 5
false
).i
is greater than1
(5 > 1
true
) and ifflag
is0
(which it is, so we log5
to the console)console.log(5)
will output5
The final result of the program will be logging
3
and5
to the console as prime numbers.Hope this helped.
好的,首先,我们正在使用的素数的定义:prime数
n
是一个数字,因此数字2
ton -1
是不是n
的因素。这意味着,当我们将n
除以2
和n -1
之间的任何数字时,其余的不是0。因此,首先,我们创建一个从
number1
到number2
的循环。我们将检查此范围内的每个数字是否是一个素数:现在,我们实际上必须检查数字
i
是一个典型,使用我们的检查方法,以查看来自>的数字是否。 2
toi -1
不是i
的因素。我们可以为此使用另一个循环,并带有一个从2
到i -1
的变量J
:在我们的
J < /code>循环,我们检查
j
是i
的一个因素。请记住,如果数字是素数,则没有j
将是i
的因素,因此i
的其余部分除以j <
j < /code>不会是0。如果
i%j
是0
,那么我们知道i
不是Prime,因此我们退出了对于循环和更改flag
为1
,因此我们知道不输出它:如果未完成
break break
,则表示没有<< 代码> j 是i
的因素,我们知道i
是PRIME,这意味着flag
仍然是0
。这意味着如果flag
是0
,我们可以打印出i
,因为我们知道这是PRIME:然后,
i
迭代到下一个数字,并且循环继续进行,直到循环和代码完成为止。请让我知道您是否需要进一步的帮助或澄清! :)
Okay first the definition of a prime number we are using: A prime number
n
is a number such that the numbers2
ton - 1
are not factors ofn
. This means that when we dividen
by any number between2
andn - 1
, the remainder will NOT be 0.So first, we create a for loop from
number1
tonumber2
. We will check if each number in this range is a prime:Now, we actually have to check if the number,
i
, is a prime, using our method of checking to see if numbers from2
toi - 1
are NOT factors ofi
. We can use another for loop for this, with a variablej
that goes from2
toi - 1
:Inside our
j
loop, we check ifj
is a factor ofi
. Remember, if the number is prime, noj
will be a factor ofi
, so the remainder ofi
divided byj
will not be 0. Ifi % j
is0
, then we knowi
is not prime, so we exit out of the for loop and changeflag
to1
, so we know not to output it:If the for loop completed without
break
, which means that noj
was a factor ofi
, we know thati
is prime, which meansflag
is still0
. This means ifflag
is0
, we can print outi
, since we know it is prime:Then,
i
iterates to the next number and the cycle continues until the loop and code is finished.Please let me know if you need any further help or clarification! :)
我只是为了娱乐而改进了代码。至于您的问题的答案,它很容易从数字1到编号2-因为这些试图将其除以所有数字低于其的数字。如果找不到(不是1),那是一个素数。
I Improved the code just for fun. As for the answer to your question, it's easily running from number1 to number2 - foreach of those tries to divide it by all numbers lower than it. if none found (and it's not 1) then it is a prime number.