简体   繁体   English

条件2的循环不符合

[英]Loop for, condition 2, doesn't comply

I'm a student with limited JavaScript knowledge (first year) and need help with Project Euler #2. 我是一名JavaScript知识有限的学生(第一年),需要有关Euler项目#2的帮助。

 var arr = [1, 2]; var total = 2; var x = 0; var y = 1; for (var i; total < 4000000; x++, y++) { i = arr[x] + arr[y]; arr.push(i); if (i % 2 == 0) { total += i } } console.log(total); 

My loop is supposed to stop when the total is less than 4,000,000 but for some reason, the total is 4,613,732. 当总数小于4,000,000时,我的循环应该停止,但是由于某种原因,总数为4,613,732。

Your code is 'not' doing anything unusual, the for is running until total is more than 4,000,000. 您的代码“没有”做任何不寻常的事情,for一直在运行,直到总数超过4,000,000。

For example if i were to add 20 each time my for loops to my total and i check every time if my total is more than 50, it will run until i get 60. not less than 60 and it will never be 50 because im adding 20 each for. 例如,如果我每次将for循环添加到总数中,每次添加20,并且每次检查总数是否大于50,它将一直运行到我得到60。不小于60,并且永远不会为50,因为im添加每个20个。

since the loop is adding 20 for each loop, in your case is the same, just add an if inside your loop, checking if the total is bigger than what you actually want, if it is, break it and do not add the value that will make it go over your desired value. 由于循环为每个循环加20,在您的情况下是相同的,只需在循环内添加一个if,检查总数是否大于实际所需的大小(如果是,则将其破坏)并且不添加该值将使其超过您的期望值。

So: 所以:

My loop is supposed to stop after total is equal to 4,000,000 but for some reason, the total is 4,613,732. 我的循环应该在总数等于4,000,000之后停止,但是由于某种原因,总数为4,613,732。

what you are adding maybe just is never going to make total exactly 4,000,000 and if it is then just change your condition to total <= 4000000; 您所添加的内容可能永远不会使总数精确到4,000,000,如果是,则将您的条件更改为total <= 4000000; instead of total < 4000000; 而不是total < 4000000;

Here is the problem statement from Project Euler #2 : 这是来自Euler项目2的问题陈述:

Each new term in the Fibonacci sequence is generated by adding the previous two terms. 斐波那契数列中的每个新项都是通过将前两个项相加而生成的。 By starting with 1 and 2, the first 10 terms will be: 从1和2开始,前10个术语将是:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... 1,2,3,5,8,13,21,34,55,89,...

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms. 通过考虑斐波那契数列中值不超过四百万的项,找到偶值项的总和。

You have misinterpreted "four million" to be the upper bound on the sum , whereas it is actually the upper bound on the individual numbers. 您误认为“四百万”是总和的上限,而实际上是各个数字的上限。

Therefore the loop condition should be i <= 4000000 ; 因此,循环条件应为i <= 4000000 ; but that's not all - you need to rearrange the conditions because i is set inside the loop, so the condition will not catch the last number added. 但这还不是全部-您需要重新排列条件,因为i循环设置的,因此条件不会捕获最后添加的数字。

for (var i;; x++, y++) {
  i = arr[x] + arr[y];
  if (i > 4000000) break; // move condition to here
  arr.push(i);
  if (i % 2 == 0) {
    total += i
  }
}

I find it easier to think my way through recursive functions when they're expressed using functional style 使用函数样式表示递归函数时,我发现更容易思考自己的方式

 const projectEuler2 = (limit = 0, sum = 0, a = 0, b = 1) => a > limit ? sum : projectEuler2 ( limit , a & 1 ? sum : sum + a , b , a + b ) console.log (projectEuler2 (4e6)) // 4613732 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM