Euleryx Problem 5 - Smallest Multiple
My workflow and answer:
Answer: 232792560
Last Weeks Favourite Solution:
There are already some very clever solutions to last weeks challenges, they have highlighted how sometimes making an educated assumption can drastically reduce to workload. This week, our favourite solution award goes to @Yoshiro_Fujimori. By flipping the problem on its head, and creating a descending list of palindromic numbers as the starting point, @Yoshiro_Fujimori's solution was incredibly efficient and even found the answer to the problem with 4 digit numbers. Find this solution on page 1 of last week post, or click here.
Definitions:
Mathematical Theory:
This problem is effectively asking us to find the Lowest Common Multiple (LCM) of all the numbers 1-20. With this in mind, we can use a method often referred to as the LCM by prime factorisation. So, what does this mean?
Let's begin with the prime factorisation part. As we have seen before (in Problem 3), the Fundamental Theorem of Arithmetic states that all numbers can be factorised into prime numbers. We also saw how to do this using a factor tree.
From this point forward, we will use the numbers 30 and 28 as examples and for those who have forgotten, here are some factor trees and the prime factors for these numbers.
Prime factors 30 = 2 x 3 x 5
Prime factors 28 = 2 x 2 x 7
Now that we have the prime factors, we can use them to find the LCM. The rule here is to take the highest power, of each prime and multiply them all together. So, in this example:
The unique prime numbers here are 2, 3, 5 and 7. Let's find the highest power for them all:
Or if you want to do this pictographically, use a Venn diagram:
By taking this list of numbers (either from the Venn diagram or the list of highest powers) we come up with the following expression:
2 x 2 x 3 x 5 x 7 = 420
Therefore, the LCM of 30 and 28 is 420.
Applying this method to the problem at hand, if we find all the prime factors of the numbers from 1 – 20, then create a list of each one's highest power, we should arrive at a solution.
Method:
As hinted at above, this method leverages the iterative macro created for Problem 3. For this problem, I will just refer to it as the List of Prime Factors macro, but if you want a more detailed explanation, please look back at Problem 3.
Summary:
Although the question didn’t explicitly state this, the trick we used was to recognise that it was asking for the Lowest Common Multiple. Armed with this idea, we were once again able to utilise the Fundamental Theorem of Arithmetic to find to LCM via prime factorisation.
Want to find out more, follow this link to our introduction post - Euleryx: Let The Games Begin.
Solved!
I knew with this one that I needed to find the prime factors for each number. Then I needed to find the maximum needed value of each prime factor (e.g. 20 = 2 * 2 * 5, so I need one 5 and two 2s --> repeat for every number) to get the LCM.
I used my prime factor macro from #3 to prime factor (that's a verb) every number. I was very happy when I was able to reuse the macro already. Once I had that, I found the maximum number of 2's and 3's, etc that I needed, and multipled those together.
It is similar with you guys approach, use the product of prime number under 20, then multiply, 2, 2,2 (for 16), 3 (for 9), that will be our largest number.
My solution.
Do i consider cheat? 🤣
I built one LCM macro when Advent of code, it got 1 year keep asking about LCM.
explanation
No Macro solution with the Generate Row + Multi Row Formula!
Done
My solution!
A simple brute force approach. I use the Message tool to stop the workflow earlier.
@Carolyn , it may be the first, but like you suggested before, I don't think it will be the last time your prime number macro will come in useful 😄
@AkimasaKajitani I have never even seen the message tool used like this before, it's genius!