Project Euler Problem 1 in R & Python

Project Euler 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.

Solution: This is very easy to solve in primary mathematics.
All multiples of 3 below 1000: $3, 6, 9, \cdots, 999$;
All multiples of 5 below 1000: $5, 10, 15, \cdots, 995$;
All multiples of $[3, 5]=15$ below 1000: $15, 30, \cdots, 990$.
The sum of all the multiples of 3 or 5 below 1000 is:
\[(3+6+\cdots+999)+(5+10+\cdots+995)-(15+30+\cdots+990)\\
=3\times(1+2+\cdots+333)+5\times(1+2+\cdots+199)-15\times(1+2+\cdots+66)\\
=166833+99500-33165\\
=233168\]
R Code

Python Code

没有评论:

发表评论