Lädt...


🔧 Add the sum of prime numbers for a given integer using JavaScript


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Write a function that takes a positive integer as a parameter and displays the sum of all prime numbers less or equal to it.

Solution

// Define a function named addPrimeSum that takes a single parameter 'number'
function addPrimeSum(number) {
  // Initialize a variable 'result' to store the sum of prime numbers, starting from 0
  let result = 0;

  // Define an inner function named isPrime that takes a single parameter 'num'
  function isPrime(num) {
    // If 'num' is less than 2, it is not prime, so return nothing (undefined)
    if (num < 2) return;

    // Loop from 2 to half of 'num' to check for factors
    for (let i = 2; i <= num / 2; i++) {
      // If 'num' is divisible by 'i', it's not prime, so return nothing (undefined)
      if (num % i === 0) return;
    }
    // If no factors are found, return 'num' indicating it is a prime number
    return num;
  }

  // Loop while 'number' is greater than 1
  while (number > 1) {
    // Check if 'number' is prime using the isPrime function
    if (isPrime(number)) {
      // If it is prime, add it to 'result'
      result += number;
    }
    // Decrement 'number' by 1 to check the next lower number
    number--;
  }

  // Return the total sum of all prime numbers found
  return result;
}

console.log(addPrimeSum(5));
console.log(addPrimeSum(21));
console.log(addPrimeSum(100));
console.log(addPrimeSum(239));
console.log(addPrimeSum(956));

Solution

> 10
> 77
> 1060
> 5589
> 70241
...

🔧 Add the sum of prime numbers for a given integer using JavaScript


📈 69.92 Punkte
🔧 Programmierung

🔧 Count numbers in range whose sum of digits is divisible by XOR sum of digits


📈 41.93 Punkte
🔧 Programmierung

🔧 JS Sum of an Array – How to Add the Numbers in a JavaScript Array


📈 38.59 Punkte
🔧 Programmierung

🔧 Tìm Hiểu Về RAG: Công Nghệ Đột Phá Đang "Làm Mưa Làm Gió" Trong Thế Giới Chatbot


📈 34.69 Punkte
🔧 Programmierung

🔧 Có thể bạn chưa biết (Phần 1)


📈 34.69 Punkte
🔧 Programmierung

🔧 Daily JavaScript Challenge #JS-39: Sum of Two Largest Numbers in an Array


📈 31.89 Punkte
🔧 Programmierung

🔧 Min sum of set of positive integers so that sum of no two elements is K


📈 31.45 Punkte
🔧 Programmierung

🔧 Today's Trending Projects: Sum of Prime Numbers Under 100 and More 🚀


📈 31.05 Punkte
🔧 Programmierung

🔧 Print all unique Pairs with given Sum


📈 29.48 Punkte
🔧 Programmierung

🔧 Print all unique Pairs with given Sum


📈 29.48 Punkte
🔧 Programmierung

🔧 TwoSum Example in C#: Print and Count Pairs with Given Sum


📈 29.48 Punkte
🔧 Programmierung

🔧 Count number of integers in given range with adjacent digits different and sum of digits equal to M


📈 29.48 Punkte
🔧 Programmierung

🔧 Find the Maximum sum of the Array by performing the given operations


📈 29.48 Punkte
🔧 Programmierung

🔧 Minimum operations to make sum at least M from given two Arrays


📈 29.48 Punkte
🔧 Programmierung

🔧 Find if path from top-left to bottom-right with sum X exists in given Matrix


📈 29.48 Punkte
🔧 Programmierung

🔧 Minimum sum of a set of nodes of Tree following given conditions


📈 29.48 Punkte
🔧 Programmierung

🔧 Find the maximum sum on the basis of the given Binary String


📈 29.48 Punkte
🔧 Programmierung

🔧 Maximize Array sum by adding multiple of another Array element in given ranges


📈 29.48 Punkte
🔧 Programmierung

🔧 Create Y[] by given Array X[] following given condition


📈 27.5 Punkte
🔧 Programmierung

🔧 Check if Array elements in given range form Permutation by performing given updates


📈 27.5 Punkte
🔧 Programmierung

🔧 Generate an Array from given Array according to given conditions


📈 27.5 Punkte
🔧 Programmierung

🔧 Number of ways to form a given String from the given set of Strings


📈 27.5 Punkte
🔧 Programmierung

🔧 How to add two numbers in JavaScript without using the "+" operator?


📈 26.93 Punkte
🔧 Programmierung

🔧 633. Sum of Square Numbers


📈 26.21 Punkte
🔧 Programmierung

🔧 simple ways to sum an array of numbers in golang


📈 26.21 Punkte
🔧 Programmierung

🔧 Sum Root to Leaf Numbers | LeetCode | Java


📈 26.21 Punkte
🔧 Programmierung

🔧 129. Sum Root to Leaf Numbers


📈 26.21 Punkte
🔧 Programmierung

🔧 Looking for an app where I can take a screenshot of a few different numbers and it'll sum them up!


📈 26.21 Punkte
🔧 Programmierung

🔧 Unique sum shuffle for Continuous numbers


📈 26.21 Punkte
🔧 Programmierung

🔧 Maximum Subsequence sum with difference among consecutive numbers less than K


📈 26.21 Punkte
🔧 Programmierung

🔧 Count numbers in range whose sum of digits is divisible by XOR of digits


📈 26.21 Punkte
🔧 Programmierung

🐧 While Loop Sum of Numbers in C++


📈 26.21 Punkte
🐧 Linux Tipps

🔧 Count of integer whose factorial of digit sum contains every digit of original number


📈 24.39 Punkte
🔧 Programmierung

matomo