素数,需要一点帮助
Prime number, need a little assistance
背景资料:
所以我想计算一个数内的所有素数,然后将它们相加。 sumPrimes(10)
应该 return 17
因为 2 + 3 + 5 + 7 = 17
。
挑战来了 --> https://www.freecodecamp.org/challenges/sum-all-primes
我添加了一些 console.log
只是为了展示发生了什么。
我的方法:
我这样做的方法是尝试在此处复制本教程,但在代码中:https://youtu.be/FBbHzy7v2Kg?t=67
我尝试用我的代码将数字分成一个数组,从 2 到你的数字是多少(1 不是素数)。 10
看起来像 [ 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
。然后我使用另一个 for
循环和 i
从前一个数组 arr
中挑选一个数字,将其添加到 prime
,然后从数组中删除该数字的每个倍数数组 (arr
) 使用单独的循环。当我回去获取另一个数字时,我们已经知道它是一个质数。
剩下的就看不懂了
示例:
该数组的第一个数字是 2。所以我将 2 添加到 prime
。
prime's current value: 2
然后过滤掉任意倍数。由于我在 j
循环中按数字本身进行计数,因此我已经知道任何数字都是倍数。它还从数组中删除我已经是质数的数及其所有倍数,因此它永远不会是未定义的。
arr's current value (hopefully): [ 3, 5, 7, 9]
等(继续 3、5,然后是 7)。
怎么了?
它并没有删除所有的倍数。我正在使用 splice
,但我认为它没有正确地删除该数字。帮助?
请不要只给我一些 ES6 答案然后走开。我想坚持我的(以及视频的)想法。我不在乎你的回答有多短,只要我的答案有问题。
repl --> https://repl.it/@John_Nicole/prime
function sumPrimes(num) {
var prime = 0;
var arr = [];
for (let i = 2; i <= num; i++) {
arr.push(i);
} // turns into array
console.log(arr)
for (let i = 0; i < arr.length; i++) {
var number = arr[i];
console.log("Prime: "+prime+"+"+number)
prime+=number
for (var j = number; j <= arr.length; j+=number) {
arr.splice(j)
} // j
} // i
return "Final result: "+prime;
}
sumPrimes(10);
提前致谢:D
正如大家已经指出的那样,您对 splice()
的期望似乎不正确。在此处查看 MDN 文档:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
>>> [1,2,3].splice() # Input
>>> [] # Output
有两个问题:
- 该函数未正确删除值(来自
arr
)
- 过滤器的循环逻辑没有考虑删除值时数组长度的变化
这是使用 splice
删除值的方法:
这会创建一个数组副本,从您要删除的索引之后的索引开始
arrayTail = arr.slice(j + 1);
这会提取数组中的所有元素,从开始到要提取的元素之前的索引
arrayHead = arr.splice(0,j);
最后将两部分合并在一起得到过滤后的数组
arr = arrayHead.concat(arrayTail);
或者,您也可以使用 Array.filter
函数:
arr = arr.filter(function(value){
// Filter all values that are divisible by the prime
return value % number != 0;
});
对于第二部分,请考虑:
arr = [1,2,3,4,5,6,7,8,9];
// You filter for multiples of 2
// And that's okay because all the values are indices offset by 2
arr = [1,2,3,5,9];
// You filter for multiples of 3
arr[2] == 3
arr[4] == 9
// But the multiples of 3 are no longer offset by 3
因此,与其对索引精打细算,不如像往常一样简单地遍历数组的其余部分,并检查当前值是否是素数的倍数。
if (arr[j] % number == 0) {
... your splice code
}
现在,这可能是不请自来的,听起来您只是在学习编程。我强烈建议养成使用完整变量名的习惯,以便于阅读。并且还包括大量注释以突出您试图通过该编码块完成的逻辑。
希望你觉得这是一个很好的例子:
function sumPrimes(num) {
var currentPrime = 0;
var arr = [];
var sumOfPrimes = 0;
// Create an array of potential primes
for (let i = 2; i <= num; i++) {
arr.push(i);
}
console.log("Prime array", arr);
// For every value in the array, remove any future multiples of it
for (let i = 0; i < arr.length; i++) {
var currentPrime = arr[i];
console.log("Prime: " + currentPrime);
sumOfPrimes += currentPrime;
console.log("Sum of Primes: " + sumOfPrimes);
// Remove multiples of prime
// Start at the next value in the array
// And loop till the end
for (let j = i + 1; j < arr.length; j++) {
// If the current value is a multiple of
// the prime, then remove it
if (arr[j] % currentPrime == 0) {
arrayTail = arr.slice(j + 1);
arr = arr.splice(0,j).concat(arrayTail);
j -= 1;
// Since you removed an element, you need to
// move the index back so it doesn't skip
// any checks
}
}
console.log("Filtered array for ", currentPrime, arr);
}
return "Final result: " + sumOfPrimes;
}
背景资料:
所以我想计算一个数内的所有素数,然后将它们相加。 sumPrimes(10)
应该 return 17
因为 2 + 3 + 5 + 7 = 17
。
挑战来了 --> https://www.freecodecamp.org/challenges/sum-all-primes
我添加了一些 console.log
只是为了展示发生了什么。
我的方法:
我这样做的方法是尝试在此处复制本教程,但在代码中:https://youtu.be/FBbHzy7v2Kg?t=67
我尝试用我的代码将数字分成一个数组,从 2 到你的数字是多少(1 不是素数)。 10
看起来像 [ 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
。然后我使用另一个 for
循环和 i
从前一个数组 arr
中挑选一个数字,将其添加到 prime
,然后从数组中删除该数字的每个倍数数组 (arr
) 使用单独的循环。当我回去获取另一个数字时,我们已经知道它是一个质数。
剩下的就看不懂了
示例:
该数组的第一个数字是 2。所以我将 2 添加到 prime
。
prime's current value:
2
然后过滤掉任意倍数。由于我在 j
循环中按数字本身进行计数,因此我已经知道任何数字都是倍数。它还从数组中删除我已经是质数的数及其所有倍数,因此它永远不会是未定义的。
arr's current value (hopefully):
[ 3, 5, 7, 9]
等(继续 3、5,然后是 7)。
怎么了?
它并没有删除所有的倍数。我正在使用 splice
,但我认为它没有正确地删除该数字。帮助?
请不要只给我一些 ES6 答案然后走开。我想坚持我的(以及视频的)想法。我不在乎你的回答有多短,只要我的答案有问题。
repl --> https://repl.it/@John_Nicole/prime
function sumPrimes(num) {
var prime = 0;
var arr = [];
for (let i = 2; i <= num; i++) {
arr.push(i);
} // turns into array
console.log(arr)
for (let i = 0; i < arr.length; i++) {
var number = arr[i];
console.log("Prime: "+prime+"+"+number)
prime+=number
for (var j = number; j <= arr.length; j+=number) {
arr.splice(j)
} // j
} // i
return "Final result: "+prime;
}
sumPrimes(10);
提前致谢:D
正如大家已经指出的那样,您对 splice()
的期望似乎不正确。在此处查看 MDN 文档:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
>>> [1,2,3].splice() # Input
>>> [] # Output
有两个问题:
- 该函数未正确删除值(来自
arr
) - 过滤器的循环逻辑没有考虑删除值时数组长度的变化
这是使用 splice
删除值的方法:
这会创建一个数组副本,从您要删除的索引之后的索引开始
arrayTail = arr.slice(j + 1);
这会提取数组中的所有元素,从开始到要提取的元素之前的索引
arrayHead = arr.splice(0,j);
最后将两部分合并在一起得到过滤后的数组
arr = arrayHead.concat(arrayTail);
或者,您也可以使用 Array.filter
函数:
arr = arr.filter(function(value){
// Filter all values that are divisible by the prime
return value % number != 0;
});
对于第二部分,请考虑:
arr = [1,2,3,4,5,6,7,8,9];
// You filter for multiples of 2
// And that's okay because all the values are indices offset by 2
arr = [1,2,3,5,9];
// You filter for multiples of 3
arr[2] == 3
arr[4] == 9
// But the multiples of 3 are no longer offset by 3
因此,与其对索引精打细算,不如像往常一样简单地遍历数组的其余部分,并检查当前值是否是素数的倍数。
if (arr[j] % number == 0) {
... your splice code
}
现在,这可能是不请自来的,听起来您只是在学习编程。我强烈建议养成使用完整变量名的习惯,以便于阅读。并且还包括大量注释以突出您试图通过该编码块完成的逻辑。
希望你觉得这是一个很好的例子:
function sumPrimes(num) {
var currentPrime = 0;
var arr = [];
var sumOfPrimes = 0;
// Create an array of potential primes
for (let i = 2; i <= num; i++) {
arr.push(i);
}
console.log("Prime array", arr);
// For every value in the array, remove any future multiples of it
for (let i = 0; i < arr.length; i++) {
var currentPrime = arr[i];
console.log("Prime: " + currentPrime);
sumOfPrimes += currentPrime;
console.log("Sum of Primes: " + sumOfPrimes);
// Remove multiples of prime
// Start at the next value in the array
// And loop till the end
for (let j = i + 1; j < arr.length; j++) {
// If the current value is a multiple of
// the prime, then remove it
if (arr[j] % currentPrime == 0) {
arrayTail = arr.slice(j + 1);
arr = arr.splice(0,j).concat(arrayTail);
j -= 1;
// Since you removed an element, you need to
// move the index back so it doesn't skip
// any checks
}
}
console.log("Filtered array for ", currentPrime, arr);
}
return "Final result: " + sumOfPrimes;
}