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:Alright so we're going to need to again keep a running total with the extra twist of only adding the even valued terms. The Fibonacci sequence is of course 1 + 1 = 2, 1+2 = 3, 2+ 3 = 5, 3 + 5 = 8 and so forth. So we can see that it is taking the preceding two numbers in the sequence and adding them together to get the next digit in the sequence. Then moves over one and adds the two digits to come up with the next digit in the sequence, so on and so forth. So we will need to replicate the Fibonacci sequence until it's value is greater than 4,000,000 doing a mod 2 each time to determine if the digit is even.
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.
I'm curious if there is a better way to determine if a number is even, but a quick mod 2 appears to run fast enough. Speaking of Project Euler in general states that each question should take less than a minute to return. So it occurs to me to see if I can get a system timestamp before the code and then a timestamp after the code is done running. Take that delta and return the elapsed time. In my search I discovered C# is awesome and has a built in function to do just that!
Stopwatch Function:
More info can be found on MSDN. First thing we need to do is include System.Diagnostics. This is the package that includes this nifty little function. Then to utilize it I set a variable to the function. I named the variable stopWatch according to their example:
Stopwatch stopWatch = new Stopwatch();The to start the stopwatch you simply call the variable and use it's built in start function (ie stopWatch.Start();). Watch your capitalization here. stopWatch is your variable whereas Stopwatch is the function that you set your variable to.
Onto the code for Problem 2.This time I used a While Loop to look at my sequence 2 variable to make sure it didn't exceed 4,000,000.
While Loops:
While (variable is some condition)So I will set my first sequence to 0 and my second sequence to 1. add them into a temporary variable, check if that temporary variable is an even number. If it is add it to my running total. Then set the 1st sequence to the 2nd sequence and the second sequence to the temporary variable. Repeat as long as we're under 4,000,000. I have my stopWatch set and to output the time lapsed in milliseconds (stopWatch.ElapsedMilliseconds).
{
do stuff
}
Solution Code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int seq1 = 0;
int seq2 = 1;
int tempseq = 0;
int total = 0;
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
while(seq2 < 4000000)
{
tempseq = seq1 + seq2;
if (tempseq % 2 == 0)
total = total + tempseq;
seq1 = seq2;
seq2 = tempseq;
}
stopWatch.Stop();
Console.WriteLine("The Answer is: ");
Console.WriteLine(total);
Console.WriteLine(stopWatch.ElapsedMilliseconds);
}
}
}
The Code comes back with the correct answer and the time lapsed in milliseconds is ZERO! Well I'd say that's under a minute so this problem is complete.Still working on making that code paste look pretty.
No comments:
Post a Comment