甚至1000岁以下的斐波那契数的总和

Sum of even Fibonacci numbers under 1000

本文关键字:1000岁 甚至      更新时间:2023-10-16
#include <iostream>
#include <math.h>
#include <iomanip>
int main(int argc, const char * argv[])
{
    register double j = 1, i = 0, sum=0, sum2 = 0;
    std::cout.setf(std::ios::fixed);
    for (register double c=1; sum=i+j,i=j,j=sum,c<1000; ++c)
        floor(fmod(sum,2))==0?sum2+=sum:0;
    std::cout << std::setprecision(0) << sum2;
    return 0;
}

我在终端上获得了极大的价值。我不知道怎么了。请帮助。

您正在计算到第1000个斐波那契号的总和不超过1000。删除 c并测试您所产生的斐波那契号是否小于1000,而不是测试是否测试是否是否测试C小于1000。

main()
{
  int n, c, first = 0, second = 1, next;
  cout << "Enter the number of terms of Fibonacci series you want" << endl;
  cin >> n;
  cout << "First " << n << " terms of Fibonacci series are :- " << endl;
  for ( c = 0 ; c < n ; c++ )
  {
     if ( c <= 1 )
     next = c;
     else
     {
      next = first + second;
      first = second;
      second = next;
     }
     cout << next << endl;
 }
return 0;
}

我想这是家庭作业吗?小心,这是初学者的高级代码。

#include <iostream>
class FibonacciGenerator {
public:
  FibonacciGenerator() : f0(0), f1(1) {}
  int operator()() {
    int f2 = f0 + f1;
    f0 = f1;
    f1 = f2;
    return f0;
  }
private:
  int f0;
  int f1;
};
int main(){
  FibonacciGenerator fibgen;
  int sum = 0;
  for( int f = fibgen(); f < 1000; f = fibgen() ){
    if( f % 2 == 0 ) sum += f;
  }  
  std::cout << sum << std::endl;
}

我是C 的初学者,我已经完成了Euler项目的第一个问题,我使用了一种非常简单的方法来解决它,这是我的代码

#include <iostream>
using namespace std;
int main() {
//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.
//since it had already said the first two number has to be 1 and 2, initialize variable n1, n2 and also n3 to store the sum of n1, n2
//and also a variable to store the even valued terms
int n1 = 1;
int n2 = 2;
int n3 = 3;
int nsum = 0;
//now start a for loop
for (int count = 0; count < n3; count++) {
    //since n2 will be always bigger than count, it will be a unstoppable loop
    //so we will add a break when n2 is bigger than 4 million
    //now start hte Fibonacci sequence
    n3 = n1 + n2;
    //change the value of old n1 to old n2
    n1 = n2;
    //also do this to n2
    n2 = n3;
    //now check the value of n3 to see if it's even,if it is, add it to nsum
    if (n3 % 2 == 0) {
        nsum += n3;
    }
    //add the break when n3 is over 4 million
    if (n3 >= 4000000) {
        break;
    }
}
//now display the value
cout << nsum << endl;
//add the pause
system("PAUSE");
return 0;

}

/**
 * Write a method that returns the sum of all even Fibonacci numbers.
 * Consider all Fibonacci numbers that are less than or equal to n.
 * Each new element in the Fibonacci sequence is generated by adding the previous two elements.
 * 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
*/
import java.io.*;
public class EVENFAB
{
    public static int EvenFabbonicSum(int n)
    {
        int previousNum=1;
        int currentNum=2;
        int evenFabSum=0;
        do
        {
            if(currentNum%2==0)
            {
                evenFabSum+=currentNum;
            }
            int temp = currentNum+previousNum;
            previousNum = currentNum;
            currentNum = temp;
         
         }
         while (currentNum < n);
        return evenFabSum;
    }
    public static void main (String[] Args)
    {
        System.out.println("u000C");
        int a = 1000;
        System.out.println(EvenFabbonicSum(a));
    }
    
}