我试图计算第一个,甚至四百万个斐波那契数的总和.但是,过了一段时间,即使我使用标识符long,只有在值变大时才打印出Y值.
这些是起始值:
long amount = 4000000;
long x = 1;
long y = 2;
long sum = 2;
Run Code Online (Sandbox Code Playgroud)
这是一个for循环,总结并在程序运行时打印出数字.
for (int i = 0; i < amount - 1; i++) {
if (x > y) {
y = x + y;
if (y % 2 == 0) {
sum += y;
}
System.out.println("X: " + x);
} else {
x = x + y;
if (x % 2 == 0) {
sum += x;
}
System.out.println("Y: " + y);
} …Run Code Online (Sandbox Code Playgroud)