发现在C#Fibonacci序列中。 [项目欧拉运动] [英] Finding Fibonacci sequence in C#. [Project Euler Exercise]

查看:115
本文介绍了发现在C#Fibonacci序列中。 [项目欧拉运动]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些麻烦,在项目欧拉这个问题。



这里的问题问什么:



在Fibonacci序列中的每个新项是通过将前两方面产生的。通过用1和2开始,前10项将是:
1,2,3,5,8,13,21,34,55,89,...
。查找所有的总和不超过400万序列中的连值方面



我到目前为止的代码:新的代码仍然没有按EDITED科技工作。

 静态无效的主要(字串[] args)
{
INT一个= 1;
INT B = 2;
INT集装箱= 0;
INT总和= 0;

,而(B< 4000000)
{
如果(%2 == 0)
{
集装箱+ =一个;
}

和= A + B;
A = B;
B = SUM;
}

+集装箱= B;

Console.WriteLine(Container.ToString());
到Console.ReadLine();
}


解决方案

一个有趣的功能,在C#是收益的关键字,这是这种东西非常有用的:

 的IEnumerable< INT>斐波那契()
{
INT N1 = 0;
INT N2 = 1;

收益率的回报1;
,而(真)
{
INT N = N1 + N2;
N1 = N2;
N2 = N;
收益率的回报N;
}
}

长效= 0;

的foreach(INT I中斐波()TakeWhile(ⅰ=方式> I&下; 4000000)。凡(ⅰ= I标记%2 == 0))
{
结果+ =我;
}
Console.WriteLine(结果);



传统的递归斐波那契的实现是这里的问题,因为它扔掉沿途的所有工作到最后申请术语。你将不得不在一个循环中一遍又一遍地叫这样的功能,这将重复工作的很多,或者你可以与执行开始,并添加参数递归函数建立所需的总和的结果作为最终的斐波纳契术语被计算。我喜欢这个好得多,因为它仍然在芯通用斐波纳契序列,而不是一个你不得不重新写入或专门



另一种方法是使用在传统的执行事件(委托)来调用一个单独的方法,因为每个学期完成,但我还是喜欢iterator方法更好,我会离开委托选项作为练习读者。


I'm having some trouble with this problem in Project Euler.

Here's what the question asks:

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, ... Find the sum of all the even-valued terms in the sequence which do not exceed four million.

My code so far: EDITED WITH NEW CODE THAT STILL DOESN'T WORK.

static void Main(string[] args)
{
    int a = 1;
    int b = 2;
    int Container = 0;
    int Sum = 0;

    while (b < 4000000)
    {
        if (a % 2 == 0)
        {
            Container += a;
        }

        Sum = a + b;
        a = b;
        b = Sum;
    }

    Container += b;

    Console.WriteLine(Container.ToString());
    Console.ReadLine();
}

解决方案

One of the fun feature in C# is the "yield" keyword, which is very useful for this kind of thing:

IEnumerable<int> Fibonacci()
{
   int n1 = 0;
   int n2 = 1;

   yield return 1;
   while (true)
   {
      int n = n1 + n2;
      n1 = n2;
      n2 = n;
      yield return n;
   }
}

long result=0;

foreach (int i in Fibonacci().TakeWhile(i => i<4000000).Where(i => i % 2 == 0))
{
    result+=i;
}
Console.WriteLine(result);

The "traditional" recursive Fibonacci implementation is problematic here because it throws away all the work done along the way to the last requested term. You would have to call such a function over and over in a loop, which would duplicate a lot of work, or you could start with that implementation and add an argument to the recursive function to build up the desired sum result as the final fibonacci term is calculated. I like this much better, because it's still a general purpose fibonacci sequence at the core, rather than one you had to re-write or specialize.

Another approach is to use events (delegates) in a traditional implementation to call a separate method as each term is completed, but as I still like the iterator method better I'll leave the delegate option as an exercise for the reader.

这篇关于发现在C#Fibonacci序列中。 [项目欧拉运动]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆