This algorithm will calculate the lowest number that is evenly divisible by all of the numbers from 1-20. This is one of the Project Euler problems (ProjectEuler.net), which i have recently taken to playing with. I really enjoy thinking in a completely different way to my day job, and this is a great place to do that. Check it out below.

The code uses a do-while loop to increase the number to be tested from 1 to as high as is needed to allow the number to be divisable by 20 and all numbers below.

The for loop iterates through each number up to 20, and the if statement checks to see if the remainder is 0 or not. if it is not, then the number being tested cannot be right, so the for loop breaks and the do-while loop iterates again, having incremented the counter.

Update:

After a bit of thinking followed by some optimisation and refactoring, the code below is a considerably faster way to compute the result.

The method above takes about 10seconds to complete, depending on your system; the new method, below, takes about 200ms on the systems I have tested it on.

The counter int q jumps up 20 with each iteration as we know that the result has to be divisible by 20. The for loop starts counting backwards, counting from 20-1, but stops at 10, this is because if a number is divisible by the numbers 11-20, it is divisible by the numbers 1-10, so there is no need to test them. Also, counting backwards means that the higher numbers are reached first, and these are the ones that are harder to divide evenly by.

I'll keep looking for further optimisations, I'm sure there are some somewhere...