Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

prime numbers javascript

function isPrime(num) {
    for (let i = 2; i * i <= num; i++)
        if (num % i === 0)
          return false; 
    return num > 1;
}
Comment

prime number js

function findPrime(to) {
	let factor;
 	for (let n = 0; n < to; n++) {
    	factor = 0
    	for (let p = 0; p < to; p++)
        	if (n % p == 0) factor++;
      	if (factor == 2) console.log(n);
  	}
}

console.log(30) //2 3 5 7 11 13 17 19 23 29
Comment

JavaScript Prime Number

// To understand this let's understand that Prime number has only two factors 
// 1) first factor = number it self
// 2) second factor = 1
// for instance 3 has only factors 3*1 = 3 and 1;
// I have taken two for loop to get two different numbers once we get two factors then it will console i
function getPrime(until) {
  let factor;
  for (let i = 0; i < n; i++) {
    factor = 0;
    for (let j = 1; j <= n; j++) {
      if (i % j == 0) {
        factor++;
      }
    }
    if (factor == 2) console.log(i);
  }
}
getPrime(30);
Comment

prime number in javascript

// program to check if a number is prime or not

// take input from the user
const number = parseInt(prompt("Enter a positive number: "));
let isPrime = true;

// check if number is equal to 1
if (number === 1) {
    console.log("1 is neither prime nor composite number.");
}

// check if number is greater than 1
else if (number > 1) {

    // looping through 2 to number-1
    for (let i = 2; i < number; i++) {
        if (number % i == 0) {
            isPrime = false;
            break;
        }
    }

    if (isPrime) {
        console.log(`${number} is a prime number`);
    } else {
        console.log(`${number} is a not prime number`);
    }
}

// check if number is less than 1
else {
    console.log("The number is not a prime number.");
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript sort object 
Javascript :: jquery fadeout to fadein 
Javascript :: apply css to iframe content javascript 
Javascript :: even or odd in javascript 
Javascript :: how to enable javascript 
Javascript :: base64 nodejs image 
Javascript :: sequelize migration set unique constraint 
Javascript :: how to create a filelist object in javascript 
Javascript :: ejs include with variable 
Javascript :: js reverse int in descending order 
Javascript :: print whole array javascript 
Javascript :: angular go to external url with blank target 
Javascript :: count vowels in a string javascript 
Javascript :: set datatable with jquery success return value 
Javascript :: momentjs display timezone 
Javascript :: “https://packagist.org/packages.json” file could not be downloaded: failed to open stream: Operation timed out 
Javascript :: es6 features javascript 
Javascript :: js array to string 
Javascript :: get zipcode from google places autocomplete 
Javascript :: ubuntu internet speed booster 
Javascript :: how to add base url as src in react native 
Javascript :: regex quantifiers 
Javascript :: how to write a program that shows a random number between 1 and 100 in your browser 
Javascript :: ho to loop trough an array of objects 
Javascript :: loop through array react native 
Javascript :: inverse of stringify js 
Javascript :: react native password strength meter 
Javascript :: sequelize update sql 
Javascript :: exec reges 
Javascript :: arry to object using reduce 
ADD CONTENT
Topic
Content
Source link
Name
6+2 =