Home > Back-end >  JavaScript for nested loop - want to know step by step of calculation
JavaScript for nested loop - want to know step by step of calculation

Time:06-27

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

CodePudding user response:

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.

CodePudding user response:

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

CodePudding user response:

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

  • Related