Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

prime numbers using for loop in Js

// range = 1 o 20 
for(let number=2 ; number<=20; number++){
    let isPrime = true;

for (let i=2; i<= number-1;i++){
    if(number%i===0){
        isPrime = false;
        break
    }
}
if (isPrime) console.log(number);
}
// result = 2,3,5,7,11,13,17,19
Comment

prime number in javascript using for loop

function sumPrimes(num) {
  // Check all numbers for primality
  let primes = [];
  for (let i = 2; i <= num; i++) {
    if (primes.every((prime) => i % prime !== 0))
      primes.push(i);
  }
  return primes.reduce((sum, prime) => sum + prime, 0);
}
Comment

prime number in javascript using for loop

function sumPrimes(num) {
  // Helper function to check primality
  function isPrime(num) {
    for (let i = 2; i <= Math.sqrt(num); i++) {
      if (num % i == 0)
        return false;
    }
    return true;
  }

  // Check all numbers for primality
  let sum = 0;
  for (let i = 2; i <= num; i++) {
    if (isPrime(i))
      sum += i;
  }
  return sum;
}
Comment

prime number in javascript using for loop

function sumPrimes(num) {
  // Prime number sieve
  let isPrime = Array(num + 1).fill(true);
  // 0 and 1 are not prime
  isPrime[0] = false;
  isPrime[1] = false;
  for (let i = 2; i <= Math.sqrt(num); i++) {
    if (isPrime[i]) {
      // i has not been marked false -- it is prime
      for (let j = i * i; j <= num; j += i)
        isPrime[j] = false;
    }
  }

  // Sum all values still marked prime
  return isPrime.reduce(
    (sum, prime, index) => prime ? sum + index : sum, 0
  );
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: check if a number is multiple of 3 javascript 
Javascript :: javascript call example 
Javascript :: javascript loop through array backwords 
Javascript :: how to multiply two array in javascript 
Javascript :: node js delete array element 
Javascript :: replace methord 
Javascript :: react word cload 
Javascript :: fetch devto api with api key 
Javascript :: Make a Responsive Portfolio Website: JavaScript and HTML 
Javascript :: number of filters 
Javascript :: configuring styled component to support ssr and hydration 
Javascript :: with jquery Make a style menu that displays paragraphs and hides them according to the style of the slides 
Javascript :: mongodb-nodejs-driver-deprecationwarning-collection-count-is-deprecated 
Javascript :: svelte function at interval 
Javascript :: how to access property from inside an array 
Javascript :: onclick switch javascript 
Javascript :: getderivedfromstate alternative 
Javascript :: json format in .net core 
Javascript :: javascript assigning index number to row in table 
Javascript :: Angularjs different dependency injection for factories inside controller 
Javascript :: js read html file 
Javascript :: Wait for AngularJS Service to finish running 
Javascript :: Pass JSON array to function in React as props and then count items in area 
Javascript :: Scaling elements proportionally using CSS and JQUERY 
Javascript :: restrict file input with react uploady 
Javascript :: Javascript if time is between 7pm and 7am do this? Javascript If Statement Time Action 
Javascript :: show hide div in javascript 
Javascript :: div auto extend win righting in 
Javascript :: 120. Triangle - JavaScript Solution With Explanation 
Javascript :: removevalidators angular 
ADD CONTENT
Topic
Content
Source link
Name
7+7 =