Tag Archives: Project Euler

Project Euler Exercise 4

Ok – so I decided to skip blogging about exercise 3 – you can find it on my github account though. I’m trying to spend about 20 minutes a day doing these code katas, but I only have time to blog about once a week, so I’ll typically stick to blogging about the most recent / interesting one.

So I’m going to focus on my second attempt at exercise 4. I’m happy to say that this was “pure TDD”, in that I wrote the test not having any idea how I was going to implement it! I focussed purely on the design and how I wanted it to look to consumers of these components. I decided I’d try a Fluent Interface style – and I’m pretty happy with the result! So the first test I wrote is as follows:

public void CanFindMaximumPalindromeUsingFluentSyntax()
	var result = Find.Maximum(number => number.IsPalindrome()).ThatIsProductOf.TwoNumbersEachLessThanOrEqualTo(99);
	Assert.That(result, Is.EqualTo(9009));

Continue reading

Project Euler Exercise 2

I need to start spending more time reading requirements!

Project Euler Exercise 2 is stated as follows:

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, …

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

My first 2 implementations of this completely missed the “even-valued” aspect of this task – I just added up everything in the sequence. Let’s move swiftly on from that! After having re-read the problem, I started again and wrote the following test:

public void CanGetTotalOfAllEvenNumbersInFibonacciSequenceUpToAndIncluding4Million()
	var fibonacci = new FibonacciSequence(4000001);
	var result = fibonacci.GetValues().Where(i => i % 2 == 0).Sum();
	Assert.That(result, Is.EqualTo(4613732));

Continue reading

Project Euler Exercise 1

Ok, so this is the first of what I hope to be a whole series of blogs about my attempts at each of the Project Euler exercises. I’m doing them all in C# – the main thing I’m trying to get from doing these exercises is more TDD practise.

Project Euler Exercise 1 is stated as:

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000.

Take 1 – Iterator / Visitor Pattern

I decided to use something approximating a Visitor / Iterator pattern – this was a pattern I saw on JP Boodhoo’s Nothin’ But .NET course recently.

Continue reading