# Category: <span>Algorithms</span>

## Finding amicable numbers with Linq

As a (somewhat lapsed but still interested) mathematician, I like to play around with interesting numbers. Well, I guess we all have our little foibles eh?

I decided to have a go at generating pairs of amicable numbers using Linq. This post explains what they are, and how I went about it.

## My buses don’t come in threes!

A few weeks ago, I blogged about some code I was writing to simulate buses arriving at bus stops and picking up passengers.

I mentioned at the end that the code shown there had three major shortcomings. Having nothing better to do with an hour this afternoon (meaning: not being bothered to do what I was supposed to be doing and preferring to have a play instead), I decided to address those shortcomings.

This blog post explains the changes, and how they led to a very pretty, but probably wrong simulation!

## Why do buses come in threes?

The answer to the age-old question of why do buses come in threes is that they don’t. Well, not often.

I decided to write a simple simulation to see how buses might behave, and whether or not they really do arrive in threes.

## Finding the two closest factors of an integer

I was browsing the latest C# questions on StackOverflow, and came across one that caught my eye. Sadly, it was quickly closed by people who, in my opinion, didn’t read it properly. The question was…

I am trying to write a program to find the factor of a number with minimum difference. For Ex: for 20 it should be 4×5 or vice versa. How can this be achieved for a really BIG number. lets say 989287498274928743928174192847219347123984723498. Please guide

Ignoring the usual SO rules about showing some effort or research, the question is not simply “How do I find the factors of n?”, which is what all the linked answers were about. This question was how to find the two closest factors. Sure you can do that by finding them all, then sorting them by their difference, but for a big number, that’s hugely inefficient. There are ways of solving this specific question much more efficiently, which is what I decided to do.

## Using Linq to debunk the Feng Shui about the number of days in December

Debunking the Internet myth that this year December (or whatever month is chosen) will have 5 Saturdays, 5 Sundays and 5 Mondays, and how this only happens once every 823 years.

It’s junk, and I proved it using Linq. Afterwards, I realised that I could prove it without Linq, had I only thought about the question first. Still, it was an interesting exercise, both in Linq and in thinking.

## Mazes For Programmers part 11, Ruby vs C# speed tests

This entry is part 11 of 11 in the series Mazes For Programmers

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…