private static FibonacciNode NewNodeFromPreviousNode(FibonacciNode node, int n) { return(new FibonacciNode { SecondLastNumber = node.LastNumber, LastNumber = node.LastNumber + node.SecondLastNumber, SumOfEvensSoFar = node.SumOfEvensSoFar + (node.LastNumber % 2 == 0? node.LastNumber:0) }); }
private static void Exercise2() { var node = new FibonacciNode { LastNumber = 1, SecondLastNumber = 1, SumOfEvensSoFar = 0 }; while (node.LastNumber <= 4000000) { node = NewNodeFromPreviousNode(node, 0); } Console.WriteLine(node.SumOfEvensSoFar); //var lastNode = Enumerable // .Range(0, 20) // .Aggregate(seed, NewNodeFromPreviousNode); //Console.WriteLine(lastNode.SumOfEvensSoFar); return; var sum = GetFibonacciNumbers(4000000).Where(n => n % 2 == 0).Sum(); Console.WriteLine(sum); //var ints = new List<int>(); //ints.Add(1); //ints.Add(1); //while (ints.Last() < 100) //{ // AddNextFibonacciNumber(ints); // Console.WriteLine(ints.Last()); //} //var lastNumber = 1; //var secondLastNumber = 1; //while (lastNumber <= 100) //{ // var nextNumber = lastNumber + secondLastNumber; // secondLastNumber = lastNumber; // lastNumber = nextNumber; // Console.WriteLine(lastNumber); //} }