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

时间:2014-01-26 07:02:50

标签: c++ math fibonacci

#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;
}

我在终端上获得了巨大的价值。我不知道出了什么问题。请帮忙。

5 个答案:

答案 0 :(得分:0)

你计算的是第1000个斐波纳契数的总和,而不是1000个。移除c并测试你生产的斐波纳契数是否小于1000,而不是测试c是否小于1000。

答案 1 :(得分:0)

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;
}

答案 2 :(得分: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;
}

答案 3 :(得分:0)

我是C ++的初学者,我已经完成了Project Euler的前2个问题,我使用了一个非常简单的方法来解决它,这里是我的代码

#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;

}

答案 4 :(得分:-1)

/**
 * 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));
    }
    
}