Solutions to ProjectEuler.net
ProjectEuler.net is an excellent site that hosts a number of math problems designed to be solved by a computer -- aka let's program! Here is a list of solutions I've compiled. For some solutions, I discuss my approach. I may also offer suggestions for a clever approach to the problem. You're welcome to attempt the problem yourself or verify your answer by browsing the page.
Problem 1
Multiples of 3 and 5
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6, and 9. The sum of these multiples is 23.
Find the sum of all multiples of 3 and 5 below 1000.
Solution
233,168
Explanation
The easiest way to solve this problem is by Brute Force . This can be achieved by running a loop beginning at 1 and incrementing by 1 until 1000. In each iteration we check if the current number is divisible by 3 or 5. If it is, we add it to our sum otherwise continue to increment. Once the loop terminates we have our sum. We can generalize this idea simply checking for divisions by other factors and looping until we've reached out desired upper limit. The advantage to the Brute Force method is that it is very easy to translate into code.
A second clever appraoch is to employ the Gauss Sum Formula.The formula allows us to sum a series of numbers with the pattern 1 + 2 + 3 + ... + n, where n is the upper limit. The Gauss Formula is as follows,