📜  素数之和javascript代码示例

📅  最后修改于: 2022-03-11 15:02:40.643000             🧑  作者: Mango

代码示例1
function sumPrimes(num) {
  //create an array with values from 1 to 10
  var val =Array.from({length: num}, (_, i) => i + 1)
  
  val = val.filter((number)=> {
    for(var i = 2; i < number; i++){
      if(number % i === 0){
        
        return false
      }
    }
    return number > 1    // [2, 3, 5, 7]
  }).reduce(function(a, b){
    return a + b   //sum = 17
  })
return val
  }
  

console.log(sumPrimes(10));