function isPrime(num) {
for ( var i = 2; i < num; i++ ) {
if ( num % i === 0 ) {
return false;
}
}
return true;
}
function display(n) {
var arr = [2];
for ( var i = 3; i < n; i+=2 ) {
if ( isPrime(i) ) {
arr.push(i);
}
}
console.log(arr); // use arr result on your own
}
display(100);
//Added Functionality
function prime(n,flag) {
( typeof flag === "undefined" || flag === false ) ? flag = false : flag = true;
function isPrime(num) {
if ( num === 0 || num === 1 ) {
return false;
}
for ( var i = 2; i < num; i++ ) {
if ( num % i === 0 ) {
return false;
}
}
return true;
}
if ( flag ) {
var arr = [2];
for ( var i = 3; i <= n; i+=2 ) {
if ( isPrime(i) ) {
arr.push(i);
}
}
return arr;
} else {
return isPrime(n);
}
}
//Prime Number from 1 to 100
const arr = [] //Creating an Empty List for keeping all prime numbers
for(var p=2;p<=100;p++){ // 1 is not a prime number it is excluded and loop is started with 2 to 100.
for(var i=2;i<p;i++ ){
//2<2 ----->(Which is false program will return to last if)
if(p%i == 0){ //|
break //|
} //|
} //|
if(p == i){ //Here and adds that number <-------------------------=
arr.push(p)
}
}
console.log(arr)
// Result :: [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97]