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
flagwith the value0(if it ever becomes1, the number is not prime). - we enter the second for, which checks from 2 to
i(which at this point is3)- 2nd for:
j = 2- we check if the rest of the division between
iandjis0(3 % 2) 3 % 2is1, which is not0, so we will move on to the next iteration
- we check if the rest of the division between
- 2nd for:
j = 3, finishes because condition ofi < jis not respected (3 < 3false)
- 2nd for:
- now we check if
iis greater than1(3 > 1true) and ifflagis0(which it is, so we log3to the console) console.log(3)will output3
- we declare a
1st for:
i = 4- we declare a flag with the value
0(if it ever becomes1, the number is not prime). - we enter the second for, which checks from
2toi(which at this point is4)- 2nd for:
j = 2- we check if the rest of the division between
iandjis0(4 % 2) 4 % 2is0, soflagwill become1and we break out of the second for.
- we check if the rest of the division between
- now we check if
iis greater than1(4 > 1) and ifflagis0(which is not, so we do not show the number)
- 2nd for:
- we declare a flag with the value
1st for:
i = 5- we declare a
flagwith the value0(if it ever becomes1, the number is not prime). - we enter the second for, which checks from 2 to
i(which at this point is5)- 2nd for:
j = 2- we check if the rest of the division between
iandjis0(5 % 2) 5 % 2is1, which is not0, so we will move on to the next iteration
- we check if the rest of the division between
- 2nd for:
j = 3- we check if the rest of the division between
iandjis0(5 % 3) 5 % 3is2, which is not0, so we will move on to the next iteration
- we check if the rest of the division between
- 2nd for:
j = 4- we check if the rest of the division between
iandjis0(5 % 4) 5 % 4is1, which is not0, so we will move on to the next iteration
- we check if the rest of the division between
- 2nd for:
j = 5, finishes because condition ofi < jis not respected (5 < 5false).
- 2nd for:
- now we check if
iis greater than1(5 > 1true) and ifflagis0(which it is, so we log5to the console) console.log(5)will output5
- we declare a
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);
}
