0

我有以下代码,我试图将所有素数相加到 10。

我不是试图为这个问题找到最有效的代码或正确的代码,但我很难理解 for 循环的作用。我特指什么时候i = 7

由于我有 2 个嵌套的 for 循环,由于某种原因,i = 7外循环循环不止一次,我的意思是内循环似乎达到了它的终止条件,j < k但它似乎坚持继续循环。 j = 2k = 2

这是代码:

var array = [2];
var total = 0; 
function isPrime(i, j) {
    if ( i%array[j] === 0 ) {
        console.log("Not P Check: i was " + i + ", j was " + j + " and k was " + k);
        console.log(i + " is not a prime");
        k = j;
    }
    else if ((j + 1) === array.length) {
        console.log(i + " is a prime");
        total += i;
        console.log("total so far is " + total);
        array.push(i);
        console.log(array);
        k = j;
        console.log("is P Check: i was " + i + ", j was " + j + " and k was " + k);
    }
    else {
        j++;
        console.log("Check " + (j + 1) + ": i is " + i + ", j is " + j + " and k is " + k);
        isPrime(i,j);
    }
}

for(var i = 3; i <=10; i++) {
   var k = array.length;
   for(var j = 0; j < k; j++) {
       console.log("Check 1: i is " + i + ", j is " + j + " and k is " + k);
       isPrime(i, j);
   }
}

console.log(total);
console.log(array);
4

2 回答 2

0

我为您创建了一个小提琴http://jsfiddle.net/yLby9/并编辑了几行。基本上,我将 isPrime 更改为返回一个布尔值,并告诉该循环在找到素数后中断。在您的版本中,当它达到 7 时,7 被推入数组(array.length 增加)。现在,当我们已经发现 7 是一个素数时,我们想跳出我们的内部循环并继续使用不同的 i。

var array = [2];
var total = 0; 
function isPrime(i, j) {
    if ( i%array[j] === 0 ) {
        console.log("Not P Check: i was " + i + ", j was " + j + " and k was " + k);
        console.log(i + " is not a prime");
        k = j;
        return false;
    }
    else if ((j + 1) === array.length) {
        console.log(i + " is a prime");
        total += i;
        console.log("total so far is " + total);
        array.push(i);
        console.log(array);
        k = j;
        return true;
        console.log("is P Check: i was " + i + ", j was " + j + " and k was " + k);
    }
    else {
        j++;
        console.log("Check " + (j + 1) + ": i is " + i + ", j is " + j + " and k is " + k);
        return isPrime(i,j);
    }
}

for(var i = 3; i <=10; i++) {
   var k = array.length;
   for(var j = 0; j < k; j++) {
       console.log("Check 1: i is " + i + ", j is " + j + " and k is " + k);
       if (isPrime(i, j)) break;
   }
}

console.log(total);
console.log(array);
于 2013-02-15T09:07:23.250 回答
0

答案是您的解决方案中不需要内部循环。您的递归函数已经在完成内部循环的工作。内部循环中发生的情况是,一些数字都重复了不必要的次数。

Dygestor 的解决方案是一种方法。另一种方法更简单

for(var i = 3; i <= 10; i++) {
  isPrime(i, 0);
}

您的方法的日志:

Check 1: i is 3, j is 0 and k is 1
3 is a prime
total so far is 3
[2, 3]
is P Check: i was 3, j was 0 and k was 0
Check 1: i is 4, j is 0 and k is 2
Not P Check: i was 4, j was 0 and k was 2
4 is not a prime
Check 1: i is 5, j is 0 and k is 2
Check 2: i is 5, j is 1 and k is 2
5 is a prime
total so far is 8
[2, 3, 5]
is P Check: i was 5, j was 1 and k was 1
Check 1: i is 6, j is 0 and k is 3
Not P Check: i was 6, j was 0 and k was 3
6 is not a prime
Check 1: i is 7, j is 0 and k is 3
Check 2: i is 7, j is 1 and k is 3
Check 3: i is 7, j is 2 and k is 3
7 is a prime
total so far is 15
[2, 3, 5, 7]
is P Check: i was 7, j was 2 and k was 2
Check 1: i is 7, j is 1 and k is 2
Check 3: i is 7, j is 2 and k is 2
Check 4: i is 7, j is 3 and k is 2
Not P Check: i was 7, j was 3 and k was 2
7 is not a prime
Check 1: i is 7, j is 2 and k is 3
Check 4: i is 7, j is 3 and k is 3
Not P Check: i was 7, j was 3 and k was 3
7 is not a prime
Check 1: i is 8, j is 0 and k is 4
Not P Check: i was 8, j was 0 and k was 4
8 is not a prime
Check 1: i is 9, j is 0 and k is 4
Check 2: i is 9, j is 1 and k is 4
Not P Check: i was 9, j was 1 and k was 4
9 is not a prime
Check 1: i is 10, j is 0 and k is 4
Not P Check: i was 10, j was 0 and k was 4
10 is not a prime
15
[2, 3, 5, 7]

删除内部循环的日志(请记住,这也缺少console.log内部循环):

3 is a prime
total so far is 3
[2, 3]
is P Check: i was 3, j was 0 and k was 0
Not P Check: i was 4, j was 0 and k was 0
4 is not a prime
Check 2: i is 5, j is 1 and k is 0
5 is a prime
total so far is 8
[2, 3, 5]
is P Check: i was 5, j was 1 and k was 1
Not P Check: i was 6, j was 0 and k was 1
6 is not a prime
Check 2: i is 7, j is 1 and k is 0
Check 3: i is 7, j is 2 and k is 0
7 is a prime
total so far is 15
[2, 3, 5, 7]
is P Check: i was 7, j was 2 and k was 2
Not P Check: i was 8, j was 0 and k was 2
8 is not a prime
Check 2: i is 9, j is 1 and k is 0
Not P Check: i was 9, j was 1 and k was 0
9 is not a prime
Not P Check: i was 10, j was 0 and k was 1
10 is not a prime
15
[2, 3, 5, 7] 

在新解决方案中也不需要该k变量。j将递增直到它到达素数数组的末尾或直到目标数可整除,以较早者为准。

如果要使用内部循环解决方案,则需要删除递归函数并执行以下操作:

var primes = [2];
var sum = 0;

// Start looping
for(var i = 3; i <= 10; ++i) {
  var prime = true; // Prime until proven innocent
  for(var j = 0; j < primes.length; ++j) { // Length stays same until later
    if(i % arr[j] === 0) { // The meat of your isPrime function: divisible?
      prime = false;
      break; // Stop the loop early: number is not prime!
    }
  }
  if(prime) { // We have a prime!
    primes.push(i); // Add it to our list of primes
    sum += i; // Add the prime to the sum
  }
}

// Log the result
console.log("The sum of primes up to 10 (inclusive)", sum);
console.log("These primes were", primes);
于 2013-02-15T09:23:17.453 回答