Friday, February 26, 2021

Project euler #1

 https://projecteuler.net/problem=1

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.

A:

sum of multiples of 3: 3, 6, ... 999 = 333*(1+333)*3/2=166833

sum of multiples of 5: 5, 10, ... 5*199 = 199*(1+199)*5/2=99500

sum of multiples of 15: 15, 30, ... 66*15 = 66*(66+1)*15/2=33165

Final sum: 166833+99500-33165=233168

No comments:

Post a Comment