Inspired by a long-defunct billboard advert, I coded up a Linq expression to find the first 10-digit prime found in consecutive digits of e. This turned out to be disappointingly easy

# Tag: <span>algorithms</span>

Ever wondered what the prime factors of your telephone number are? No, nor had I until I read Humble Pi. A throwaway comment there led me to contemplate how to do this in a single Linq statement.

Why? Why not?

Following on from my experiments in timing the algorithms in part 9 of this series, I was curious as to why my code for Wilson’s algorithm took so much longer than the code for the Aldous-Broder algorithm. Jamis Buck (the author of the book this is all based on) was foolish enough to leave his email address on his blog, inviting people like me to ask him questions! I asked if he had seen anything like this difference. Turns out he hadn’t actually tried timing them, so couldn’t comment. This got me curious, so I pulled up his code, and started searching around to find out how to time code in Ruby. This turned out to be fairly simple (as it should), giving me the following… This gave me 10 numbers, which enabled me to calculate an average, as well as check on the variation (standard deviation for the statistical…

One of the idea he had for modifying the Aldous-Broder algorithm was to force it to pick an unvisited neighbour, if one exists. I think the idea was to cut down on the number of very short dead ends that the basic algorithm produces. If you look at the following sample, you can see that there are a lot of places in the maze where there are many short walls, making it pretty obvious which way to go… The plain algorithm picks a random cell from the current cell’s neighbours… It was pretty easy to change this so that it would always pick an unvisited cell if there were any… With a little thought, I could probably change the Where clause to include both cases, but it didn’t seem worth it at the time. Anyway, this simple change produced a marked difference in the quality of the mazes. Apart from…

Having coded up two more complex algorithms, I analysed them to see how they compared, and then combined the two in an attempt (failed) to produce a hybrid algorithm that was faster than either of its parents.

This was my first real challenge. I converted his code for the Binary Tree and Sidewinder two algorithms, and Aldous-Broder turned out to be pretty straightforward to implement, so my code looked a lot like his. However, Wilson’s algorithm was a different matter altogether. I had been reading the book at night, and didn’t have it by my side when I tackled this algorithm. I had read his description of the algorithm a few times, and it seemed easy enough. Ha, famous last words. My first attempt was pretty rubbish. The algorithm didn’t terminate, and what it produced along the way was not very impressive. Unsure what had gone wrong, I re-read his description (whilst avoiding the code) and realised that I had mis-read it, and had got two small, but significant points wrong. I could go into a lengthy discourse about how important it is to understand your requirements,…

The main part of Dijkstra’s algorithm is implemented in the Distances class. For the most part, my code didn’t look that much different from his. The main change was that I was able to use Linq to remove an if when traversing the frontier… I broke lines 6-8 into three to make it easier to read in this blog, but in the code, it’s just one line. Linq enabled me to avoid having to check if the next cell had already been linked. Adding the numbers to the text output was very simple, as I already added the facility to display cell contents (see part 2 of this series). The challenge was putting these numbers on the UI. This turned out to be much easier than I expected, and even worked first time, which surprised me! I added a method to the window’s code behind, passing in the distances collection…

Part 4 in my ongoing series of how I implemented the code from Mazes For Programmers by Jamis Buck in C#.

In this post, I explain how I implemented the first two maze-generation algorithms in C#, and show how parts of the code were improved by using Linq.

Leave a Comment