"Method for finding efficient algorithms? What can you recommend to get better at finding efficient solutions to math problems? The first, and only solution that I could think of was the brute force way: target = 999 sum = 0 for i = 1 to target do if (i mod 3 = 0) or (i mod 5 = 0) then sum := sum + i output sum This does give me the correct result, but it becomes exponentially slower the bigger the target is. So then I saw this solution: target=999 Function SumDivisibleBy(n) p = target div n What I do ask tho is, are there methods and or steps, you can apply to solve math problems to find the best (or at least a good) solution? If yes, can you guys recommend any books/videos/lectures that teach these methods? And what do you do yourself when attempting to find such solutions?"

princetonaqo3

princetonaqo3

Answered question

2022-10-14

Method for finding efficient algorithms?
TL;DR
What can you recommend to get better at finding efficient solutions to math problems?
Background
The first challenge on Project Euler says:
Find the sum of all the multiples of 3 or 5 below 1000.
The first, and only solution that I could think of was the brute force way:
target = 999
sum = 0
for i = 1 to target do
if (i mod 3 = 0) or (i mod 5 = 0) then sum := sum + i
output sum
This does give me the correct result, but it becomes exponentially slower the bigger the target is. So then I saw this solution:
target=999
Function SumDivisibleBy(n)
p = target div n
return n * (p * (p + 1)) div 2
EndFunction
Output SumDivisibleBy(3) + SumDivisibleBy(5) - SumDivisibleBy(15)
I don't have trouble understanding how this math works, and upon seeing it I feel as though I could have realised that myself. The problem is just that I never do. I always end up with some exponential, brute force like solution.
Obviously there is a huge difference between understanding a presented solution, and actually realising that solution yourself. And I'm not asking how to be Euler himself.
What I do ask tho is, are there methods and or steps, you can apply to solve math problems to find the best (or at least a good) solution?
If yes, can you guys recommend any books/videos/lectures that teach these methods? And what do you do yourself when attempting to find such solutions?

Answer & Explanation

Jaylin Wheeler

Jaylin Wheeler

Beginner2022-10-15Added 11 answers

There is no general method to find efficient algorithms, as there is no method to solve math problems in general. Besides practice and culture.
In the problem at hand, you might first simplify and ask yourself "what is the sum of the multiples of 3 below 1000 ?". Obviously, these are 3 , 6 , 9 , 999 i.e. 3 1 , 3 2 , 3 3 , 3 333 and the sum is thrice the sum of integers from 1 to 1000/3, for which a formula is known (triangular numbers). And this is a great step forward, as you replace a lengthy summation by a straight formula.
Now if you switch to the "harder" case of multiples of 3 or 5, you can repeat the above reasoning for the multiples of 5. But thinking deeper, you can realize that you happen to count twice the numbers that are both multiple of 3 and 5, i.e the multiples of 15. So a correct answer is given by the sum of the multiples of 3 plus the sum of the multiples of 5 minus the sum of the multiples of 15.

Do you have a similar question?

Recalculate according to your conditions!

New Questions in High school statistics

Ask your question.
Get an expert answer.

Let our experts help you. Answer in as fast as 15 minutes.

Didn't find what you were looking for?