# Euler #5

Published on 2011-07-21I found the fifth problem to be a lot quicker to solve than the previous four, either because it was significantly easier or because I'm getting more comfortable in C#. I'll flatter myself and assume the latter. The problem states:

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?

For some reason, I at first thought the best way would be to assemble a list of all numbers meeting this condition below 20!, the only number immediately recognizable as evenly divisible by all integers from one to twenty, and then have my comp tell me the smallest member of the list. I wrote a simple program to iterate over all multiples of twenty between 20 and 20!, checking each time if it was evenly divisible by all integers 11-19 (being divisible by 20 was a condition for being iterated over, and the range of 11-20 contains multiples of all numbers 1-10), and adding matches to a list. After having run my program and having received no answer after 45 seconds of processing, I reread the prompt and realized I didn't need to calculate **all** the values meeting this condition up to an arbitrary ceiling, but merely the smallest one. I rewrote my loop to start from 20 and break at the first match, and my computer calculated it in the blink of an eye. My code is below.

using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Problem5 { class Program { static void Main(string[] args) { long factorial20 = 1; for (int i = 1; i <= 20; i++) { factorial20 *= i; } for (long i = 20; i < factorial20; i += 20) { if ((i % 19 == 0) && (i % 18 == 0) && (i % 17 == 0) && (i % 16 == 0) && (i % 15 == 0) && (i % 14 == 0) && (i % 13 == 0) && (i % 12 == 0) && (i % 11 == 0)) { Console.WriteLine("The smallest number evenly divisible by all integers 1-20 is {0}",i); break; } } } } }