斐波那契数列 (JS) - 偶数之和
Fibonacci Sequence (JS) - Sum of Even Numbers
我开始了欧拉计划。我在问题 2 上想出了这个代码来计算高达 400 万的偶数斐波那契数的总和。代码似乎做了很多我想做的事。当代码为 运行 时,我确实看到列出了正确的总和。我真正感到困惑的唯一部分是结果中显示的最后一个数字。这是它显示的内容:
JS 代码:
var previous = 0;
var current = 1;
var sum = 0;
var next;
for(i = 1; i < 100; i++){
next = current + previous;
previous = current;
current = next;
if(current % 2 === 0 && current < 4000000) {
sum += current;
console.log(sum);
}
}
结果:
2
10
44
188
798
3382
14328
60696
257114
1089154
4613732 (this is the number i was trying to get)
=> 354224848179262000000 (confused as to why this number shows up and what it represents)
让我分解一下:
为什么会出现这个?
在控制台上,您将看到执行任何表达式的结果。如果您执行一段代码,您将看到您在代码块中执行的最后一个表达式。与直觉相反,在这种情况下它是 current = next
的结果,因为 if 语句在您上次通过 for 循环时不是 运行。
为什么 next 等于 354224848179262000000?
第 100 个斐波那契数是 354224848179261915075。JavaScript 然而,当您的数字超过某个点并开始假设您的数字的所有下部都为零时,它会失去精度。有关移动详细信息,请参阅此问题:Why does JavaScript think 354224848179262000000 and 354224848179261915075 are equal?.
你可以用这段代码来解决你的问题,有了这个算法不需要经过100次迭代就可以得到你的答案(是你的修改,区别在于for循环,当你使用迭代的当前变量):
function sumFibs(num) {
let previous = 0;
let current = 1;
let sum = 0;
let next;
for(current; current <= num;){
next = current + previous;
previous = current;
if(current % 2 === 0) {
sum += current;
}
current = next;
}
return sum;
}
sumFibs(4000000); // return 4613732
这可能是 JavaScript
中的解决方案之一
let a = 1;
let b = 2;
let upperBound = 4000000;
let c = a + b;
let sum = 2;
while(true) {
c = a + b;
if(c >= upperBound) {
break;
}
if(c % 2 == 0){
sum += c;
console.log(c);
}
a = b;
b = c;
}
console.log(`The Fibonacci for even numbers below ${upperBound} is:`, sum);
我开始了欧拉计划。我在问题 2 上想出了这个代码来计算高达 400 万的偶数斐波那契数的总和。代码似乎做了很多我想做的事。当代码为 运行 时,我确实看到列出了正确的总和。我真正感到困惑的唯一部分是结果中显示的最后一个数字。这是它显示的内容:
JS 代码:
var previous = 0;
var current = 1;
var sum = 0;
var next;
for(i = 1; i < 100; i++){
next = current + previous;
previous = current;
current = next;
if(current % 2 === 0 && current < 4000000) {
sum += current;
console.log(sum);
}
}
结果:
2
10
44
188
798
3382
14328
60696
257114
1089154
4613732 (this is the number i was trying to get)
=> 354224848179262000000 (confused as to why this number shows up and what it represents)
让我分解一下:
为什么会出现这个?
在控制台上,您将看到执行任何表达式的结果。如果您执行一段代码,您将看到您在代码块中执行的最后一个表达式。与直觉相反,在这种情况下它是 current = next
的结果,因为 if 语句在您上次通过 for 循环时不是 运行。
为什么 next 等于 354224848179262000000?
第 100 个斐波那契数是 354224848179261915075。JavaScript 然而,当您的数字超过某个点并开始假设您的数字的所有下部都为零时,它会失去精度。有关移动详细信息,请参阅此问题:Why does JavaScript think 354224848179262000000 and 354224848179261915075 are equal?.
你可以用这段代码来解决你的问题,有了这个算法不需要经过100次迭代就可以得到你的答案(是你的修改,区别在于for循环,当你使用迭代的当前变量):
function sumFibs(num) {
let previous = 0;
let current = 1;
let sum = 0;
let next;
for(current; current <= num;){
next = current + previous;
previous = current;
if(current % 2 === 0) {
sum += current;
}
current = next;
}
return sum;
}
sumFibs(4000000); // return 4613732
这可能是 JavaScript
中的解决方案之一let a = 1;
let b = 2;
let upperBound = 4000000;
let c = a + b;
let sum = 2;
while(true) {
c = a + b;
if(c >= upperBound) {
break;
}
if(c % 2 == 0){
sum += c;
console.log(c);
}
a = b;
b = c;
}
console.log(`The Fibonacci for even numbers below ${upperBound} is:`, sum);