Problem 25. Prime.js ( how to find Prime) Here we are doing without mark variable
/* Question
How to find Prime (steps)
eg:- 7 is divisible by 1,7
which means it is not divisible 2,3,4,5,6
Algorithm:-
1. Read the number
2. Run a loop from 2 to number -1 (eg 7-1 =6)
3. Check if the number is divisible by any number in the above range
4. If yes , Not Prime else Prime
*/
// Without using mark lets see what is the problem
let num = 7;
let i = 2;
while (i < num) {
if (num % i == 0) {
console.log('Not Prime');
}
else {
console.log('Prime');
}
i++;
}
Terminal:
Nobins-MacBook-Pro:STOC_MODULE 11SEPT 2022 nobinpunyo$ node prime.js
Prime
Prime
Prime
Prime
Prime
Nobins-MacBook-Pro:STOC_MODULE 11SEPT 2022 nobinpunyo$
** we get Prime or not prime for each iteration but we need only one time so lets use mark
variable in the next problem
Comments
Post a Comment