ARE YOU A MATH FREAK!!!!!!!

Tuesday, December 1, 2009

Problem 1 : Sum of numbers

Q. Find the sum of all the multiples of 3 or 5 below 1000.

Sol:
This one must be the easiest problem on Euler.. It can be solved easily without programming.
For an arithmetic progression whose first term is 'a', last term is 'l' and no. of elements is n, the sum is given by
n/2 * (a+d).
So, since we are required to calculate the sum of numbers up to 1000 divisible by either 3 or 5, we just calculate the sum of numbers divisible by 3(S1) and add it to sum of numbers divisible by 5(S2).Now taking care that those divisible by both 3 and 5(i.e. those divisible by 15) have been summed up twice,we subtract the sum of numbers divisible by 15(S3) from it. Nice and easy!!!!

No. of numbers divisible by 3=999/3=333
S1= 333/2 * (3+999)=166833
No. of numbers divisible by 5=995/5=199
S2=199/2 * (5+995)=99500
NO. of numbers divisible by 15= 990/15=66
S3=66/2 * (15+990)=33165

Result= S1 + S2 -S3= 233168

Please post your comments...
Better algorithms are always welcome..

No comments:

Post a Comment