嵌套循环的JavaScript-想逐步了解计算

发布于 2025-02-10 10:44:12 字数 569 浏览 1 评论 0原文

我是初学者。 谁能让我在以下代码中逐步知道“嵌套为循环”的逐步(每个步骤 /条件),以查找两个数字之间的质量数。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝天 2025-02-17 10:44:12

如果您是初学者,则可以理解算法的一个好方法是以具体的示例进行示例,并在纸上手动进行遵循代码会发生什么。我们可以说3和5是:

- &gt;低:3,高:5

我们将输入第一个,它将用i使用3

  • 1st初始化:i = 3

    • 我们用值0声明flag(如果它变成1,则该数字不是Prime)。
    • 我们输入第二个,该检查从2到i(此时为3
      • 第二个:J = 2
        • 我们检查iJ是 0 (3%2)之间的其余部分。
        • 3%21,不是0,因此我们将继续进行下一个迭代
      • 第二个为:j = 3,因为i&lt的条件; J不尊重(3&lt; 3 false
    • 现在我们检查i是否大于13&gt; 1 true)和if <代码> flag 是0(是,所以我们将3记录到控制台)
    • console.log(3)将输出3
  • 1st for:i = 4

    • 我们声明带有值的标志0(如果它变为1,该号码不是Prime)。
    • 我们输入第二个,从2检查到i(此时为4
      • 第二个:J = 2
        • 我们检查ij04%2)之间的其余部分。
        • 4%20,因此flag将成为1,我们脱离了第二个。
      • 现在,我们检查i是否大于14&gt; 1)和flag0(不是,所以我们不显示该号码)
  • 1st for:i = 5

    • 我们用值0声明flag(如果它变成1,则该数字不是Prime)。
    • 我们输入第二个,该检查从2到i(此时为5
      • 第二个:J = 2
        • 我们检查iJ是 0 (5%2)之间的其余部分。
        • 5%21,不是0,因此我们将继续进行下一个迭代
      • 第二个:j = 3
        • 我们检查iJ05%3)之间的其余部分。
        • 5%32,不是0,因此我们将继续进行下一个迭代
      • 第二个:J = 4
        • 我们检查iJ05%4)之间的其余部分。
        • 5%41,不是0,因此我们将继续进行下一个迭代
      • 第二个为:j = 5,因为i&lt的条件; J不尊重(5&lt; 5 false)。
    • 现在我们检查i是否大于15&gt; 1 true)和if <代码> flag 是0(是,所以我们将5记录到控制台)
    • console.log(5)将输出5

程序的最终结果将是记录35作为素数。

希望这有所帮助。

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 with 3

  • 1st for: i = 3

    • we declare a flag with the value 0 (if it ever becomes 1, the number is not prime).
    • we enter the second for, which checks from 2 to i (which at this point is 3)
      • 2nd for: j = 2
        • we check if the rest of the division between i and j is 0 (3 % 2)
        • 3 % 2 is 1, which is not 0, so we will move on to the next iteration
      • 2nd for: j = 3, finishes because condition of i < j is not respected (3 < 3 false)
    • now we check if i is greater than 1 ( 3 > 1 true ) and if flag is 0 (which it is, so we log 3 to the console)
    • console.log(3) will output 3
  • 1st for: i = 4

    • we declare a flag with the value 0 (if it ever becomes 1, the number is not prime).
    • we enter the second for, which checks from 2 to i (which at this point is 4)
      • 2nd for: j = 2
        • we check if the rest of the division between i and j is 0 (4 % 2)
        • 4 % 2 is 0, so flag will become 1 and we break out of the second for.
      • now we check if i is greater than 1 ( 4 > 1 ) and if flag is 0 (which is not, so we do not show the number)
  • 1st for: i = 5

    • we declare a flag with the value 0 (if it ever becomes 1, the number is not prime).
    • we enter the second for, which checks from 2 to i (which at this point is 5)
      • 2nd for: j = 2
        • we check if the rest of the division between i and j is 0 (5 % 2)
        • 5 % 2 is 1, which is not 0, so we will move on to the next iteration
      • 2nd for: j = 3
        • we check if the rest of the division between i and j is 0 (5 % 3)
        • 5 % 3 is 2, which is not 0, so we will move on to the next iteration
      • 2nd for: j = 4
        • we check if the rest of the division between i and j is 0 (5 % 4)
        • 5 % 4 is 1, which is not 0, so we will move on to the next iteration
      • 2nd for: j = 5, finishes because condition of i < j is not respected (5 < 5 false).
    • now we check if i is greater than 1 ( 5 > 1 true ) and if flag is 0 (which it is, so we log 5 to the console)
    • console.log(5) will output 5

The final result of the program will be logging 3 and 5 to the console as prime numbers.

Hope this helped.

热风软妹 2025-02-17 10:44:12

好的,首先,我们正在使用的素数的定义:prime数n是一个数字,因此数字2 to n -1是不是n的因素。这意味着,当我们将n除以2n -1之间的任何数字时,其余的不是0。

因此,首先,我们创建一个从number1number2的循环。我们将检查此范围内的每个数字是否是一个素数:

for (let i = number1; i <= number2; i++) {

现在,我们实际上必须检查数字i是一个典型,使用我们的检查方法,以查看来自>的数字是否。 2 to i -1不是i的因素。我们可以为此使用另一个循环,并带有一个从2i -1的变量J

for (let i = number1; i <= number2; i++) {
  let flag = 0;
  for (let j = 2; j < i; j++) {

在我们的J < /code>循环,我们检查ji的一个因素。请记住,如果数字是素数,则没有j将是i的因素,因此i的其余部分除以j <j < /code>不会是0。如果i%j0,那么我们知道i不是Prime,因此我们退出了对于循环和更改flag1,因此我们知道不输出它:

for (let i = number1; i <= number2; i++) {
  let flag = 0;
  for (let j = 2; j < i; j++) {
    if (i % j == 0) {
      flag = 1;
      break;
    }
  }

如果未完成break break ,则表示没有<< 代码> j 是i的因素,我们知道i是PRIME,这意味着flag仍然是0。这意味着如果flag0,我们可以打印出i,因为我们知道这是PRIME:

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迭代到下一个数字,并且循环继续进行,直到循环和代码完成为止。

请让我知道您是否需要进一步的帮助或澄清! :)

Okay first the definition of a prime number we are using: A prime number n is a number such that the numbers 2 to n - 1 are not factors of n. This means that when we divide n by any number between 2 and n - 1, the remainder will NOT be 0.

So first, we create a for loop from number1 to number2. We will check if each number in this range is a prime:

for (let i = number1; i <= number2; i++) {

Now, we actually have to check if the number, i, is a prime, using our method of checking to see if numbers from 2 to i - 1 are NOT factors of i. We can use another for loop for this, with a variable j that goes from 2 to i - 1:

for (let i = number1; i <= number2; i++) {
  let flag = 0;
  for (let j = 2; j < i; j++) {

Inside our j loop, we check if j is a factor of i. Remember, if the number is prime, no j will be a factor of i, so the remainder of i divided by j will not be 0. If i % j is 0, then we know i is not prime, so we exit out of the for loop and change flag to 1, so we know not to output it:

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 the for loop completed without break, which means that no j was a factor of i, we know that i is prime, which means flag is still 0. This means if flag is 0, we can print out i, since we know it is prime:

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);
}

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! :)

不再让梦枯萎 2025-02-17 10:44:12

我只是为了娱乐而改进了代码。至于您的问题的答案,它很容易从数字1到编号2-因为这些试图将其除以所有数字低于其的数字。如果找不到(不是1),那是一个素数。

const number1 = parseInt(prompt("Enter the lower number"), 10);
const number2 = parseInt(prompt("Enter the higher number"), 10);

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 <= Math.sqrt(i); j++) {
    if (i % j == 0) {
      flag = 1;
      break;
    }
  }
  if (i > 1 && flag == 0) console.log(i);
}

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.

const number1 = parseInt(prompt("Enter the lower number"), 10);
const number2 = parseInt(prompt("Enter the higher number"), 10);

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 <= Math.sqrt(i); j++) {
    if (i % j == 0) {
      flag = 1;
      break;
    }
  }
  if (i > 1 && flag == 0) console.log(i);
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文