Learning D With Project Euler

D is a systems programming language. Its focus is on combining the power and high performance of C and C++ with the programmer productivity of modern languages like Ruby and Python. Special attention is given to the needs of quality assurance, documentation, management, portability and reliability.[1]

Project Euler is a website dedicated to a series of math problems intended to be solved with computer programs. Each problem has been designed according to a "one-minute rule", which means that an efficient implementation will allow a solution to be obtained on a modestly powered computer in less than one minute.[2]

In this article, I pick some problems and show you how to solve them with D.

I choose only easiest ones because published solution could spoil the problem.

The purpose is to demonstrate the feature and power of D, so the codes below may not be the best way to solve those problems.

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.

It's very easy, and the answer is:

This equation can be solved with a pencil and paper. Eh... wait a minute. We are talking about the programming language here, aren't we?
Maybe it's better to start with a common method that everybody uses at his first attempt:

It's not as fast as the second version (problem1_b.d), but it's faster than the corresponding program written in other language like python or haskell.
If you have no idea what these functions mean, here is a brief explanation:

!( ... )

Template argument are enclosed with !(...) instead of <...>. Foo!(int) in D is equal to Foo<int> in C++.

reduce!("a + b")(0, range);

sum the elements in the range and return a number.

filter!("a % 3 == 0 || a % 5 == 0")(range);

find all elements in the range that meet the requirements and return a new range.

iota(begin, end, step);

construct a range that goes through the numbers begin, begin + step, begin + 2 * step, ..., up to and excluding end.

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.

This one-liner functional solution is split into several lines so I can write some comments in it:

//problem2_a.dimportstd.stdio;importstd.algorithm;importstd.range;voidmain(){writeln(reduce!("a + b")(// the sum of allfilter!("(a&1) == 0")(// the even-valued termsuntil!("a >= 4000000")(// which do not exceed four millionrecurrence!("a[n-1] + a[n-2]")(1,2)// a Fibonacci sequence starting with 1 and 2))));}

It's a bad practice to calculate the fibonacci sequence with straight recursion[3], and it's even worse when recursion is also used to sum the sequence.

Problem 22 is chosen because it's the first problem that requires file and string operations:

Using names.txt, a 46K text file containing over five-thousand first names,
begin by sorting it into alphabetical order. Then working out the alphabetical
value for each name, multiply this value by its alphabetical position in the
list to obtain a name score.
For example, when the list is sorted into alphabetical order, COLIN, which is
worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list.
So, COLIN would obtain a score of 938 × 53 = 49714.
What is the total of all the name scores in the file?