Continuing the wonderful community solutions to Project Euler.
This is Problem 5, finding the smallest multiple.
2520 is the smallest number tha...
For further actions, you may consider blocking this person and/or reporting abuse
Here's mine via paper and pencil. The primes less than 20 are 2, 3, 5, 7, 11, 13, 17 and 19. The LCM of 1,2,3,...,20 is 16*9*5*7*11*13*17*19.
That's the only proper way to do it. A software developer's job is to solve problems, ideally as efficiently as is possible and/or as is practical. That may or may not include writing code.
I really like "make it work, then make it fast" approach.
First implement a naive solution that works, then make it fast if necessary.
Starting out with the attitude of best solution possible might be very time consuming.
I'm not really talking about premature optimization here (on that, I completely agree). It's of course always a balance, because you don't want to get caught in analysis paralysis. But in general I'd say it's still better to 'think before you act'. Otherwise you might be devoting a lot of time and energy to creating and then maintaining a complex solution while a simple one would have sufficed.
+1 for 'think before you act'.
I wrote my initial comment because of the phrase 'That's the only proper way to do it.' which I disagree with.
Let's return to the initial problem from the post.
There are no other requirements about code complexity, performance or anything else. Dwayne's solution is clever, but it requires a domain knowledge to understand what's going on. Moreover, it's less flexible for the case of different inputs.
A naive bruteforce solution is definitely slower, but the code intention is clear from the code itself and doesn't require some implicit wisdom.
So, I think both solutions are good and solve the problem. A concrete project's requirements is the most important part when choosing one solution over another.
Did you notice that all the posted solutions required the same domain knowledge?
If all the numbers from 1 to 20 divide the number then the number must be a multiple of all the numbers from 1 to 20.
Since we want the smallest such number it follows that we want the least common multiple of the numbers from 1 to 20.
Everyone has to make a similar deduction in order to even begin writing their solution.
Rather than writing a program to solve it I simply went ahead and did the LCM calculation by hand. The only extra domain knowledge required is knowing how to calculate the LCM of a set of numbers using prime factorization.
As much as I'd like to take credit for a clever solution, I must say that the prime factorization method is the standard way one learns to find LCM. (LCM using prime factorization)
The way I went about my explanation must have made the solution seem clever but my intention there was to describe a thinking pattern one might go through if they don't know much about LCM and how you'd reason your way from an initial answer (1*2*...*20) to the smallest possible answer.
Also, everyone else used the fact that LCM(a,b)*GCD(a,b) = a*b so that LCM(a,b)=(a*b)/GCD(a,b). Now that's not obvious, unless you look it up or you know why it's true which is based on prime factorization (which in itself is a nice proof, though that's beside the point).
Here's mine!
The most significant part here is that you can compute the least common multiple by computing the greatest common denominator and using it to divide the product of the two numbers.
I was about to propose a similar solution (in JavaScript) but yours is sufficient 👍
(Yes, the language itself isn't important for me. Just the mathematical challenge.)
Please do propose your version in javascript. Certainly it will not be excessive
Can you please explain your code
Bruteforce node solution 🤣
Please how do i put my code in this snippet like you did when i want to comment with my own answer
Wrap your content with three backtics to render it as a code snippet.
Thannk you so much Eugene. This gave me tough time
Here's mine
C
And Go
Rust Solution Playground
I like solving it via paper and pencil, but it is always a pleasure to do some code :)
Java code:
Here is my solution. it's not the cleanest but is quite fast and it seems to work. I used nodeJS
here is the output
Perl 6 has an lcm operator (no modules/libraries necessary):
I guess you won't find a solution shorter than this one. However I doubt that this makes Perl 6 attractive. The more features the language has, the more you have to learn. If you master it, it sure is fun to write code like this or the next example:
It's a solution for Euler#1 and it's another example that the language has a ton of features that enable you to write super short code. Unfortunately it also gets harder to read, some might even call it gibberish.
But you can also write beautiful code in Perl 6. Here are solutions for Project Euler Problems, super-short ones as well as beautiful ones. 😄
My solution in Python:
smallest number that can be divided by each of the numbers from 1 to 10 without any remainder
for smallest_num in range(1, 1000000):
check_list = []
for x in range(1, 11):
if smallest_num % x == 0:
check_list.append(x)
check_list.sort()
if check_list == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:
print("Smallest Number is : ", smallest_num)
exit()
Clojure