This site will not help you cook pots of snakes potions. It is my diary as I make my way through the challenges at https://projecteuler.net/. I will attempt to use mathematics, as well as the programming languages
elixir to solve them. There will be spoilers, so if you want to solve the problems yourself, stop right here!
Solutions to the problems are not meant to be optimal or elegant, but rather a tool to continue learning
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 the multiples of 3 or 5 below 1000.
The sum up to of numbers which are multiples of 3 can be expressed as . Equally for multiples of 5 up to the sum will be: .
Adding both together we get:
Or, more precisely the sum of multiples of 3 should go up to
floor(1000/3)=333, since will be the last multiple before . This however, will double-count some of the multiples. For example are both in and . We need to subtract the sum of the numbers which contain multiples of both which we can call . This sum can be expressed as:
where stands for the floor function. The sum of the first N numbers is equal to , so our final answer will be:
or in a more functional way: