Java 中的 Euler 项目 2
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18927767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Project Euler 2 in Java
提问by Breon Thibodeaux
public class Euler2 {
public static void main(String[] args) {
int Num1 = 0;
int Num2 = 1;
int sum = 0;
do
{
sum = Num1 + Num2;
Num1 = Num2;
Num2 = sum;
if (Num2 % 2 == 0)
sum = sum + Num2;
}
while (Num2 < 4000000);
System.out.println(sum);
}
}
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.
通过考虑 Fibonacci 数列中值不超过 400 万的项,找出偶数项的总和。
I don't feel like I coded it wrong but the answer I'm getting is 5702887 and I know it should be 4613732.
我不觉得我编码错了,但我得到的答案是 5702887,我知道它应该是 4613732。
采纳答案by spydon
public class Euler {
public static void main(String[] args) {
int num1 = 0;
int num2 = 1;
int temp = 0;
int sum = 0;
do {
if (num2 % 2 == 0) {
sum = sum + num2;
}
temp = num1 + num2;
num1 = num2;
num2 = temp;
} while (num2 < 4000000);
System.out.println(sum);
}
}
You messed up the sum by assigning it twice on each iteration where num2 is even. In this solution we use a temporary variable to store the next fibonacci number.
您通过在 num2 为偶数的每次迭代中分配两次来弄乱总和。在这个解决方案中,我们使用一个临时变量来存储下一个斐波那契数。
Solution= 4613732
解决方案= 4613732
回答by user2573153
I'm not sure what the program is supposed to do but part of the problem may be that you are assigning num2 to the value of sum before your if statement, making the inside of that if statement equivalent to sum = sum + sum;
我不确定程序应该做什么,但部分问题可能是您在 if 语句之前将 num2 分配给 sum 的值,使 if 语句的内部等效于 sum = sum + sum;
On another note it is bad practice to capitalize the names of your local variables. Good luck!
另一方面,将局部变量的名称大写是不好的做法。祝你好运!
回答by musical_coder
Here are some hints (and not a complete answer since this appears to be homework):
这里有一些提示(不是完整的答案,因为这似乎是家庭作业):
- When doing Fibonacci, you need to provide the first two terms as 1 and 2, not 0 and 1 as you are now.
- The
if (Num2 % 2 == 0)
means that, as user2573153 pointed out, the value ofsum
is getting doubled everytimeNum2
is even. But this isn't how Fibonacci works. I can see that you're trying to continuously sum up the even terms by using thatif
statement. So how can you do that without messing upsum
? (Hint: store up the even numbers somewhere else).
- 做斐波那契时,您需要提供前两项为 1 和 2,而不是像现在这样的 0 和 1。
- 这
if (Num2 % 2 == 0)
意味着,正如 user2573153 指出的那样, 的值sum
每次Num2
都翻倍是偶数。但这不是斐波那契的工作方式。我可以看到您正在尝试使用该if
语句不断总结偶数项。那么你怎么能做到这一点而不搞砸sum
呢?(提示:将偶数存储在其他地方)。
回答by Doro
I just made it and I can only tell you that you need an if(... % 2 == 0){} and only sum up the even numbers.... I hope it helps you.
我刚刚做到了,我只能告诉你你需要一个 if(... % 2 == 0){} 并且只总结偶数......我希望它对你有帮助。
回答by Om Shankar
JavaScript:
JavaScript:
function printSumOfEvenFiboNumbersWithin (limit) {
var current = 2, prev = 1, next, sum = 0;
do {
next = current + prev;
prev = current;
current = next;
sum += prev % 2 == 0 ? prev : 0;
} while (prev < limit);
console.log(sum);
}
printSumOfEvenFiboNumbersWithin(4000000); // 4613732
printSumOfEvenFiboNumbersWithin(4000000); // 4613732
回答by ROMANIA_engineer
Another solution, using:
另一种解决方案,使用:
while
instead ofdo-while
bits
operation instead of%
only 2 variables to keep the effective values (no
aux
):public static void main(String[] args) { int sum = 0 ; int x1 = 1; int x2 = 2; while ( x1 < 4000000 ) { if ( (x1 & 1) == 0 ){ // x % 2 == 0 sum += x1; } x2=x1+x2; // x2 = sum x1=x2-x1; // x1 = the old value of x2 } System.out.println(sum); }
while
代替do-while
bits
操作而不是%
只有 2 个变量来保持有效值(没有
aux
):public static void main(String[] args) { int sum = 0 ; int x1 = 1; int x2 = 2; while ( x1 < 4000000 ) { if ( (x1 & 1) == 0 ){ // x % 2 == 0 sum += x1; } x2=x1+x2; // x2 = sum x1=x2-x1; // x1 = the old value of x2 } System.out.println(sum); }
回答by anji_rajesh
public void execute() {
int total = 1;
int toBeAdded = 1;
int limit = 4000000;
int totalSum = 0;
int temp = 0;
while (total <= limit) {
if (total % 2 == 0) {
totalSum = totalSum + total;
}
temp = toBeAdded;
toBeAdded = total;
total = toBeAdded + temp;
}
}
回答by JennyShalai
Swift 3:
斯威夫特 3:
func evenFibonacciNumbersSum() -> Int {
// init first two Fibonacci numbers
// init sum is 2 (counting only even numbers)
var result = 2
var firstFibonacci = 1
var secondFibonacci = 2
while true {
// new (next) number is a sum of two previous numbers
let nextFibonacci = firstFibonacci + secondFibonacci
// check if new Fib number is even
if nextFibonacci % 2 == 0 {
// if even - add to result
result += nextFibonacci
}
// if new Fib number hit 4M - no more calculations
if nextFibonacci > 4000000 {
return result
}
// to move on we need to reassign values
firstFibonacci = secondFibonacci
secondFibonacci = nextFibonacci
}
}
print(evenFibonacciNumbersSum()) // will print 4613732
回答by Theja Mitta
This should work:
这应该有效:
public static void main(String[] args)
{
int n1=0;
int n2=1;
int n3=0;
int count=10000;
int limit=4000000;
int sum=0;
for(int i=1;(i<=count && n3<=limit); i++)
{
n3=n1+n2;
if(n3%2==0){
sum = sum+n3;
System.out.println(sum);
}
n1=n2;
n2=n3;
}
}
回答by Dhaval Mistry
This might help you...
这可能会帮助你...
#include<stdio.h>
int main()
{
int i,a = 0,b = 1,temp = 0,sum = 0;
while(temp < 4000000)
{
temp = a + b;
printf("%d\n",temp);
a = b;
b = temp;
if(temp % 2 == 0)
{
sum = sum + temp;
}
}
printf("The sum is :- %d\n", sum);
return 0;
}