### Edit (18/01/2013)

I have edited this post on the above date. However, the only change that I have made is that I am now hosting the source code (found at the end of this post) on GitHub. This means, that if I need to make a change to the code, I do not have to edit this post again – as it will pull the latest version of it from the relevant page on GitHub whenever it is accessed by a reader.

### Firstly

This is a solution to problem 5 on Project Euler. I’ve not posted it up here to gloat, or to provide answers for cheats. I’ve posted it, so that you can see how I solve this kind of problem.

The original problem reads like this:

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

### The Algorithm

Firstly, there are two ways to solve this problem. One involves brute force, and one requires a little savvy.

Technically, there are a whole bunch of ways to solve this problem – dependant on your understanding of Mathematics. Hopefully, both of these methods will be simple enough to follow – assuming you can understand Prime Numbers.

The brute force check goes like this:

1. Start at n, check this against all numbers in m
2. If n divides evenly (i.e there is no remainder) by all the numbers in m, stop. This is the answer
3. If not, add 1 to n and start again.

“n” starts at 1 (this is the smallest positive number that will divide by all the numbers from 1 to 20)

“m” starts at 1 and goes only as far as 20 (this is the series of numbers that “n” has to divide by)

That’s it.

The smarter algorithm goes like this:

2. If n divides evenly (i.e there is no remainder) by all the numbers in m, stop. This is the answer
3. If not, add 2520 to n and start again.

This method assumes that you recognise a few things from the problem statement. Let’s take another look:

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

Since the numbers between 1 and 20 contain all the numbers between 1 and 10 (1, 2, 3, 4, 5, 6, 7, 8, 9 & 10), some of the work is done for us. We can assume that, since 2520 is evenly divisible by all numbers between 1 and 10, that it must be evenly divisible by all the numbers between 1 and 20.

This algorithm speeds up execution exponentially. Instead of going through every positive integer between 1 and he final answer, we can jump in steps of 2520.

### The Code

I’m not going to spend time explaining the code, since it contains enough information in the comments (within the code itself) and in the description of the algorithm above.

So, without further ado, here is the finished code:

And there you have it, another problem solved (and optimised) in minutes.

Until next time,

J

### Related Post

#### GaProgMan

Jamie is a .NET developer specialising in ASP.NET MVC websites and services, with a background in WinForms and Games Development. When not programming using .NET, he is either learning about .NET Core (and usually building something cross platform with it), speaking Japanese to anyone who'll listen, learning about languages, writing for this blog, or writing for a blog about Retro Gaming (which he runs with his brother)
{"ft_lang_label":"__label__en","ft_lang_prob":0.9466622,"math_prob":0.8895872,"size":2925,"snap":"2021-43-2021-49","text_gpt3_token_len":720,"char_repetition_ratio":0.13865115,"word_repetition_ratio":0.22557727,"special_character_ratio":0.2557265,"punctuation_ratio":0.1015873,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98503965,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-26T04:34:22Z\",\"WARC-Record-ID\":\"<urn:uuid:d5901fe9-1a78-4649-ab95-2a4998c67540>\",\"Content-Length\":\"57266\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:2c4cdaf0-15ea-4e24-999b-85dc8ebbfbe9>\",\"WARC-Concurrent-To\":\"<urn:uuid:783e06c1-f480-42e4-848d-e0e33f96b875>\",\"WARC-IP-Address\":\"172.67.213.171\",\"WARC-Target-URI\":\"https://blog.gaprogman.com/2011/07/pining-for-primes-or-how-i-solved-project-euler-problem-5/\",\"WARC-Payload-Digest\":\"sha1:OXZX4G5IKH4MWPXP3EXIRNOTCLMXADAN\",\"WARC-Block-Digest\":\"sha1:HFRLXBD2L4GC63ZDEAUPKFJM4LNQW36K\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323587799.46_warc_CC-MAIN-20211026042101-20211026072101-00107.warc.gz\"}"}