Euler – 2
Euler serisinin ikinci yazısında, Project Euler’in 2. sorusunu çözeceğiz;
Orijinal soru; 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.
Türkçesi; Fibonacci serisinde her rakam, kendisinden önce gelen iki rakamın toplamıdır. 1 ve 2 ile başlayan serinin ilk 10 rakamı:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …
Bu serinin 4.000.000 (Dört Milyon) ‘dan küçük tüm çift rakamlarının toplamını bulunuz.
Önce siz çözmeyi deneyin, çözemezseniz ;
using System;
using System.Linq;
using System.Collections.Generic;
class Program
{
static IEnumerable< long > FibonacciSeri()
{
long a = 1;
long b = 1;
while (true)
{
a = a + b;
b = a - b;
if (b < 4000000)
yield return b;
else
break;
}
}
static void Main(string[] args)
{
long Toplam = FibonacciSeri().Where(Rakam => Rakam % 2 == 0).Sum();
Console.WriteLine("Toplam : {0}", Toplam);
Console.ReadLine();
}
}
İlgili diğer başlıklar:
Merhabalar,
Kodunuz gerçekten güzel, ben de kendimce biraz düzenledim. Başlangıçta iki tane 1 vermiyor yukarıdaki haliyle…
static IEnumerable FibonacciSeri(long maxCount) { List<int> fib = new List<int>() { 1, 1 }; while (true) { if (fib[0] < maxCount) { fib.Add(fib[1] + fib[0]); yield return fib[0]; fib.RemoveAt(0); } else { break; } } }yukarıdaki kodda static IEnumerable FibonacciSeri(long maxCount) satırında IEnumerable’dan sonra içerisinde int yazmalı