Let's follow your algorithm for the range 1 to 10 (for simplicity).
Numbers in this range: 1,2,3,4,5,6,7,8,9,10.
Prime numbers in this range: 2,3,5,7.
Multiplication of these numbers: 2*3*5*7 = 210. Which is different from the correct answer 2520, so your algorithm is wrong.
Sometimes it's better to write a simple, stupid algorithm and let a computer to do the heavy work instead of doing all the work in your brain, which might be complicated and error prone.
You misunderstood. The explanation I gave is one of many ways someone can DISCOVER that what you need to do is to find the LCM of the numbers.
You do agree that the problem is implicitly asking you to find the LCM of 1,2,3,...,20?
Once you reach that point of understanding then you can do at least two things:
You can write a program that finds you the LCM. As all the coded solutions do.
You can use prime factorization to quickly compute the answer. As I did and it boils down to finding the highest powers of the primes in the given range which would be 2*2*2, 3*3, 5 and 7.
And I don't disagree with you. Start with brute force and improve. You'd then have something to test your optimizations, clever code etc against.
Let's follow your algorithm for the range 1 to 10 (for simplicity).
Numbers in this range: 1,2,3,4,5,6,7,8,9,10.
Prime numbers in this range: 2,3,5,7.
Multiplication of these numbers: 2*3*5*7 = 210. Which is different from the correct answer 2520, so your algorithm is wrong.
Sometimes it's better to write a simple, stupid algorithm and let a computer to do the heavy work instead of doing all the work in your brain, which might be complicated and error prone.
Huh? That's not the algorithm.
You misunderstood. The explanation I gave is one of many ways someone can DISCOVER that what you need to do is to find the LCM of the numbers.
You do agree that the problem is implicitly asking you to find the LCM of 1,2,3,...,20?
Once you reach that point of understanding then you can do at least two things:
You can write a program that finds you the LCM. As all the coded solutions do.
You can use prime factorization to quickly compute the answer. As I did and it boils down to finding the highest powers of the primes in the given range which would be 2*2*2, 3*3, 5 and 7.
And I don't disagree with you. Start with brute force and improve. You'd then have something to test your optimizations, clever code etc against.
Not exactly. You need to multiply by the highest powers of the primes in the range.
Yes.
And as you mentioned there are at least two ways to solve the problem. Both are valid.
We're on the same page 😉