不能完全得到欧拉问题#2的问题

我试图通过一些项目欧拉问题来学习C ++的基础知识。 我已经做到了#2。

Fibonacci序列中的每个新项都是通过添加前两个项来生成的。 从1和2开始,前10项将是:

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

求出序列中所有不超过四百万的偶数项的和。

我的逻辑:

0, 1, 1, 2, 3, 5 xyz xyz xyz xyz 

以上是循环这个:

 x + y = z x = y y = z 

我的代码:

 #include <iostream.h> using namespace std; int main() { int x = 0; int y = 1; int z; int sum; for(y = 1; y <= 4000000; y++) { z = x + y; x = y; y = z; if(y % 2 == 0) { sum += y; } } cout << sum; cin.get(); } 

那输出4613788正确的答案是4613732

我不知道什么是错的。 = /。

您将y用作循环变量和序列中的第二项。

你的意思是:

 int x = 0; int y = 1; int z; int sum = 0; do { z = x + y; x = y; y = z; if (y % 2 == 0) sum += y; } while (y <= 4000000); 

注意你也应该初始化sum

为了提高速度,请注意顺序是偶数 – 奇数(重复),偶数 – 奇数。

你不需要测试每个数字,以确定它是偶数还是奇数。 只需添加每三个数字。

你没有初始化为零。

for循环代码块应该是这样的

 while(y <= 4000000) { z = x + y; x = y; y = z; if(y % 2 == 0) { sum += y; } } 

基本上,你不应该增加y。

这是我们如何在最少的循环中做到的。 如果我们用前两个数字来写斐波那契数列,那就是:

(a + b),(a + 2b), (2a+3b) ,(3a + 5b),(5a + 8b), (8a+13b) ,(13a + 21b),(21a + 34b ), (34a+55b) ….

在上面的系列中, a是1, b是2,突出显示的数字是偶数。 在斐波那契数列中,每第三个数字是偶数,序列是EVEN-ODD-ODD-EVEN-。 所以如果我们写这个系列的偶数就是:

b, (2a+3b), (8a+13b), (34a+55b), (144a+233b)...

如果我们在这个序列中观察模式,则coefficient_of_next_a4*(coefficient_of_current_a)+(coefficient_of_previous_a) 。 并且coefficient_of_next_b(coefficient_of_current_a)+(coefficient_of_current_b)+(coefficient_of_next_a)

Python代码:

 # Start sum with first event number sum = 2 # Values of first two Fibonacci numbers a = 1 b = 2 # Previous coefficient of a prev_coef_a = 0 # Current coefficient of a and b coef_a = 2 coef_b = 3 while ((coef_a*a)+(coef_b*b)) <= 4000000: print(((coef_a*a)+(coef_b*b))) sum += ((coef_a*a)+(coef_b*b)) # Coefficient of a for next number next_coef_a = (coef_a*4)+prev_coef_a prev_coef_a = coef_a # Coefficient of b for next number coef_b = coef_a+coef_b+next_coef_a coef_a = next_coef_a print('Sum: {}'.format(sum)) 

输出是:

 8 34 144 610 2584 10946 46368 196418 832040 3524578 Sum: 4613732 

这里有一种解决O(log(N))问题的方法 – 时间与O(N)的实现(O(log(N)))来自需要使用pow()函数的时间。

首先,您需要能够计算O(log(N))时间内的第N个Fibonacci数:

 double Fib(double N) { double Fn = (pow(PHI, N) - pow(PSI, N)) / sqrt(5); return floor(Fn); } 

其中PHI = 1.6180339 …和PSI = -0.61803398 …(查看wiki了解更多信息)

其次,你将需要计算最接近你的目标限制的指数(在问题2的情况下,这将是4,000,000):

 double nFib(double F) { double nFib = log((double)F * sqrt(5) + 0.5) / log(PHI); return floor(nFib); } 

第三,您将使用百安居身份#25(更多信息在这里 )来计算偶数斐波纳契数的总和:

 double bqIdentity25(double N) { return (Fib(3*floor(N) + 2) - 1) / 2; } 

最后,计算总和:

 double limit = 4000000; double nearestIndex = nFib(limit); double sum = bqIdentity25(nearestIndex / 3); 

我们只需要每个第三个元素来计算偶数斐波那契数的和。

希望这可以帮助! 将

  //fibonacci sequence #include <iostream> #include <vector> using namespace std; int main() { vector<unsigned long int> list; list.clear(); list.push_back(1); list.push_back(2); cout<<"1\n2\n"; unsigned long int last; unsigned long int prev; do{ last=list.at(list.size()-1); prev=list.at(list.size()-2); list.push_back(last+prev); cout << list.at(list.size()-1)<<'\n'; }while(last<4000000); unsigned long int sum=0; for(int a=0;a<list.size();a++) { if(list.at(a)%2==0) { sum+=list.at(a); } } cout <<'\n'<<sum<<'\n'; return 0; } 
 perl -e '$a,$b=(0,1);while($a<4*10**6){$a%2==0and$sum+=$a;$a+=$b;$c=$a;$a=$b;$b=$c;}print"$sum\n"' 

我最近开始研究Perl的神秘艺术…(爱它!)

但我会解释它…我们需要三个变量,我们将移动我们需要的2个值,以便找到序列中的下一个步骤(将分配给第三个变量,如$c=$a;$a=$b;$b=$c; )。 因为我们知道fibo以它们开始( $a,$b=(0,1)$a,$b=(0,1)所以$a$b被预先声明。 从那里我们得到一个while循环滚动,只要我们在我们的boologic中使用的变量小于4mil( while($a<4*10**6) )。 每一次迭代我们都检查偶数( $a%2==0 )和模数,加上它们等于我们的$ sum变量( $sum+=$a )。 在洗牌之后(如前所述),只是“打印和完成”。

我知道你想用C / C ++来完成这个工作(perl是用C编写的),但是我只是在解决Perl中的euler问题,并认为这可能会提供深入的见解。

如果它根本没有帮助(除了不是正确的语言),请告诉我如何改进我的答案,以便将来能够提供更好的答案。 最重要的是,祝你有美好的一天!

高尔夫的人?

它显示每个斐波那契序列号并选择偶数,最后给出偶数的和。

  #include <stdio.h> #include <math.h> #include <time.h> //project euler //problem# 2 int main() { long int a = 0; long int b = 1; long int sum = 0; long int f = 0; long int t = 1; long int d = 1; while (f <= 4000000){ f = a + b; printf(" %2d. number is %7d",d,f); d++; a = b; b = f; if (f % 2 == 0){ sum += f; printf("\t\t%2d. target is %7d",t,f); t++; } printf("\n\n"); printf("--------------------------------------------------------------------------------"); } printf("\n\n\t\t\t\t\tSum of targets is %d\n\n\n", sum); printf("--------------------------------------------------------------------------------\n"); printf("Press any key to continue...\n"); getchar(); } 

试图给这个问题增加一些帮助。下面的程序显示了所有甚至斐波那契数列对于用户输入的给定长度的系列。

  #include<iostream.h> #include<conio.h> class fibonacci { int input; public: void series(); }; void fibonacci::series() { cout<<"enter the value"; cin>>input; int initial=0; int initial1=1; for(int loop=0;loop<input;loop++) { int initial2; initial2=initial1+initial; if(initial2%2==0) {cout<<initial2<<"\t";} initial=initial1; initial1=initial2; } } void main() { fibonacci a; a.series(); getch(); } 

这是如何在Swift中完成的:

 /** Calculate the next even fibonacci number within a limit. Methodology: 1) Fibonacci numbers are either odd (o) or even (e) as follows: o, e, o, o, e, o, o, e, o, o, e, ... because of the arithmetic rule: Odd + Odd = Even Even + Even = Even Odd + Even = Odd 2) By do two rounds of fibonacci, we can get from one "e" to the next "e". We don't need to bother checking its even. 3) To avoid re-computing past results, we ask for the past running total to be supplied, and the past pair of fibonacci numbers before doing our two rounds of fibonacci 4) We assume the passed in pair of fibonacci numbers don't exceed are supplied limit, and on the next even fibonacci we can just test for exceeding the limit there only. 5) Fibonacci numbers grow very fast (nearly doubling each time). Since the next even is found after two iterations, it means we have exponential growth for the next fibonacci number. For limit L, we'll find the sum after O(log(L)) time. @param runningTotal Total of even fibonacci numbers seen so far @param upperLimit Limit number not to exceed the next even fibonacci @param n0 First of an adjacent pair of fibonacci numbers with n0 < upperLimit @param n1 Next fibonacci number after n1 with n1 < upperLimit @returns (updatedTotal,n3,n4) where updatedTotal is the supplied runningTotal plus the next even fibonacci number not exceeding the supplied upperLimit, n3 and n4 are the next pair of fibonacci numbers to be supplied for the next call to this method */ func sumNextEvenFibonacci(runningTotal:Int, upperLimit:Int, n0:Int, n1:Int) -> (Int, Int, Int) { let n2 = n0 + n1 let n3 = n2 + n1 let n4 = n3 + n2 if (n4 < upperLimit) { return (runningTotal + n4, n3, n4) } else { return (runningTotal, n3, n4) } } func eulerProblem_02() { println("Problem 2\n\nEach 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:\n 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... \n\nBy considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.\n") var n0 = 1, n1 = 2, n2 = 0, runningTotal = 2 do { (runningTotal, n0, n1) = sumNextEvenFibonacci(runningTotal, 4_000_000, n0, n1) } while (n1 < 4_000_000) println("The answer is \(runningTotal).\n") } eulerProblem_02() 

程序输出:

 Problem 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, 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. The answer is 4613732. 

一个使用Kotlin的解决方案,我使用这个问题来练习我的数学,并为我学习这种新的语言:

 import java.math.BigInteger /** * * https://projecteuler.net/problem=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, 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. * */ class Problem2 { var maxValue: Int = 4000000 // Looking for this fibonacci value var fibonacci = 32 var fibonacciValues = hashMapOf<Int, BigInteger>(0 to BigInteger.ONE, 1 to BigInteger.ONE); fun solution(): BigInteger { var solution: BigInteger = BigInteger.ZERO calculateFibonacci(fibonacci) fibonacciValues.filter { it.value < BigInteger.valueOf(maxValue.toLong()) && it.value.mod(BigInteger.ONE.add(BigInteger.ONE)).equals(BigInteger.ZERO) }.forEach { //println("Key: ${it.key} and ${it.value} and mv $maxValue") solution = solution.add(it.value) } return solution } private fun calculateFibonacci(n: Int): BigInteger? { if ( fibonacciValues.contains(n)) { return fibonacciValues.get(n) } else { val f = calculateFibonacci(n - 2)!!.add(calculateFibonacci(n - 1)) fibonacciValues.put(n, f) return f } } } 

这有点冗长,因为我添加了可测试性,如果你想看单元测试,这里是:

https://github.com/moxi/project-euler-solutions/blob/master/code/src/test/kotlin/org/rcgonzalezf/onetoten/Problem2Test.kt

每个第三个数字是偶数,所以偶数的和是(n个fib数的和)/ 2。

但是,一些纤维。 数字=(n + 2)的术语 – 第二项(1)。

你可以从benet的公式中获得第(n + 2)个任期

在JavaScript中,你可以这样解决:

 function add(a, b) { // body... return a + b; } function fabonacci(limit) { var i = 2; //parseInt(limit); var fabonacci = [0, 1]; var evenFab = []; var valToPush = 0; var result = []; while (valToPush < 4000000) { valToPush = fabonacci[fabonacci.length - 2] + fabonacci[fabonacci.length - 1] i++; if ((valToPush % 2) == 0) { evenFab.push(valToPush); } if (valToPush > 4000000 || i > limit) { break; } fabonacci.push(valToPush); } result['sequence'] = fabonacci; result['sumOfEven'] = evenFab; return result; } var result = fabonacci(10); console.log("Fabonacci sequence:" + result['sequence']); console.log("Sum of Even Number:" + (result['sumOfEven']).reduce(add, 0));`