Finding Fibonacci sequence in C#. [Project Euler Exercise] -


I have a problem with this problem in Project Oller.

Here's the question:

Each new word is generated in the Fibonacci sequence by adding the last two words. Starting with 1 and 2, the first 10 words will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... In addition to the sum of all the important positions in the sequence which is 4 lakhs Not much.

My code is still: Edited with the new code that still does not work.

  static zero main (string [] args) {int a = 1; Int b = 2; Int container = 0; Int sum = 0; While (B & L; 4000000) {if (a% 2 == 0) {container + = A; } Yoga = A + B; A = B; B = Yoga; } Container + = B; Console.WriteLine (Container.ToString ()); Console.ReadLine (); }  

One of the fun features in C # is the "yield" keyword, which is Very useful for this type of thing:

  IEnumerable & lt; Int & gt; Fibonacci () {int n1 = 0; Int n2 = 1; Yield returns 1; Whereas (true) {int n = n1 + n2; N1 = N2; N2 = N; Yield yields n; }} Longer result = 0; In Focanasi () i (i> i & lt; 4000000). Where (i = & gt; i% 2 == 0)) {result + = i; } Console.light line (result);  

The implementation of "traditional" recursive Fibonacci is problematic here because it eliminates all the work done in the way of the last requested word. You will need to call this type of function in a loop, which is a duplicate of too many work, or you can start with that implementation and add logic to the recurring function to make it desired I like the sum of the sum of the final Fibonacci word as a calculation, because it is still a general purpose Fibonacci sequence in the core, rather than rewriting it to you Had rana.

Another approach is to use events (delegates) in the traditional implementation to call a different method as each term, but as still Iterator method, I have a practice for better reader As the representative will leave the option.


Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -