Welcome back to Project Euleryx, this time: Largest Prime Number.
This week's post is going to be a little longer than previous posts, as we'd like to cover some of the mathematical theory behind our solution. That being said, just because there is some theory behind our approach, doesn’t mean it is the only approach that works, so please feel free to share your thoughts below.
Euleryx Problem 3 - Largest Prime Number
The challenge is to find the largest prime factor of the number 600851475143.
Here's my full workflow and answer:
Last Weeks Favourite Solution:
Another tough decision for our favourite solution so we have gone with a tie. Both @CoG and @gawa provided excellent solutions. Calculating the neccesary terms only and storing variables within a single column in a generate rows was genius. Checkout there solutions on page one of last weeks challenge here.
Definitions:
Mathematical Theory:
During this problem, we exploited what’s called the Fundamental Theorem of Arithmetic (FTA), which states:
“Every positive integer (whole number) can be written as a product of prime numbers, or the number itself is prime.”
e.g.
If we take the number 30, we can write this as 2 x 3 x 5
The Method to our Madness (Factor Trees):
We decided that to tackle this problem, we would list all the prime factors of the number, then simply take the largest. The tricky part here was creating that list of prime numbers.
Using the Fundamental Theorem of Arithmetic, we know that 600851475143, can be written as a product of its prime factors. But how do we find these prime factors? We decided to use what's called a factor tree.
A factor tree works as follows:
1) Identify any 2 factors of your number.
2) If the number is prime, circle it.
3) If the number is not prime, thanks to the FTA, we know that it too can be expressed as a product of its primes, so, repeat steps 1 and 2 for each uncircled number.
4) Stop when there are no more uncircled numbers at the bottom of the tree.
e.g. for the number 30, the tree would grow as follows:
First step:
Second step:
Therefore for we can see, 30 can be written as 5 x 2 x 3
In case you’re worried that you didn’t think of 5 and 6 to begin with, rest assured, it doesn’t matter which two numbers you choose, as long as they make up a factor pair. Here are some other examples of possible factor trees for the number 30:
All of which lead to the same answer. With this in mind, the only thing left was to create a workflow in Alteryx, that can create these factor trees.
Setting up the Workflow:
Whilst all this theory is great, we still need a way to get the computer to do the hard part. If it wasn’t apparent from the above, we will be using an iterative macro to solve this problem. So, the first step is to decide what information we need to be passed between each iteration.
For this question, we fed 3 columns into the macro, as shown in the image below. You will notice we have two starting numbers to help illustrate the workflows process, as we step through it:
The Iterative Macro:
Yes, it's time for the tough part, but fear not, let's break the steps down together. On each iteration, our goal is:
i.e. at the start of the first iteration:
vs the end of the first iteration (on the loop anchor). Notice [Number] * [List of Prime Factors] = [Starting Number]
This is achieved as follows:
Without this step, each iteration would be passing the same number through, so it's vital that we remember this part.
Final Steps:
Running this macro should give you the following output.
As you can see, all the prime factors are listed in the “List of Prime Factors” column, with a comma separating them. It's important to note that this list may include duplicates, for example, if you started with the number 4, the “List of all prime Factors” should be “2,2” as 2 x 2 = 4.
To find the largest prime factor for each starting value, simply parse out the “List of Prime Factors” columns, sort the results descending, and select the top record per “Starting Number”:
Now, all that's left to do is submit your answer to the Project Euler website!
Summary:
By using Alteryx to Iteratively create a factor tree, one branch at a time, we have been able to reduce any number down to its prime factors. Thanks to the Fundamental Theorem of Arithmetic, we know that the largest prime number from our list, is indeed the largest prime factor of 600851475143.
Want to find out more, follow this link to our introduction post - Euleryx: Let The Games Begin.
I used the macro from here to speed up the Prime Number finding process.
https://community.alteryx.com/t5/Weekly-Challenges/Challenge-187-Generate-Prime-Numbers/m-p/482140/h...
My solution.
This is an interesting challenge.
First, I determined the prime numbers up to 10,000. The number 10,000 was chosen arbitrarily. Finding prime numbers itself was easy because I had done it before in a Weekly Challenge.
After appending the obtained prime numbers to the original number, I used the Mod function to filter out prime factors. Then, I multiplied these to confirm that they matched the original number, ensuring the prime factorization was correct.
However, this approach is incomplete if the same prime number appears multiple times or if there are prime factors larger than the initially assumed maximum prime factor. Therefore, I also created a macro to perform complete prime factorization. This involved continuously dividing the original number by the prime factors until it reduced to 1.
For this problem, since we only need to find the largest prime factor, we can get the answer through a few rounds of Try & Error without using a macro.
Fun challenge!
After having a look through, I think my solution is most similar to @caltang's. Great minds think alike
Generate all numbers up to the sqrt of the number, then use the mod function to remove any (to get all factors).
I did a separate filter to remove all even factors first - figured this would speed things up as the number we're checking is odd.
Then effectively did that manual check again against all factors - to check if they're prime.
Shout out to @Carolyn for adapting a prime number checker macro - great way to make the process reusable.
@CoG's solution goes straight over my head... for now. Very impressed you can cram so much logic inside a single generate rows.
Thanks for the knowledge share, all!