Euleryx Problem 7 - 10 001st Prime
Last Weeks Favourite Solution:
Last week @gawa spotted that if we simply rearrange the equation, we can cancel out some like terms, avoiding the need to do any form of subtraction. We thought this was a great approach, and therefore, it gets our favourite solution award! Find it on page one of last weeks post, or click here.
Definitions:
My workflow and macro:
Workflow:
Macro:
Answer: 104743
Mathematical Theory:
As we know, a prime number has only two factors. That means, if we take all numbers less than our prime value (and greater than 1), none of them will have a multiple of our prime value. To help clarify what I mean, let's take the number 5 as an example.
The list of numbers less than 5 (and greater than one) would be: 2, 3 and 4.
The first few multiples of these numbers would be:
2: 2, 4, 6, 8, 10, 12
3: 3, 6, 9, 12, 15, 18
4: 4, 8, 12
As you can see, there is no 5. Excluding 1 and 5, all other numbers are greater than 5, therefore can't be factors of 5. This means 5 must be prime.
Now, how do we find the next prime number? Well, let's find the first few multiples of 5
5: 5, 10, 15
Looking at the list of multiples, 6 has already been included, so we know that not prime. We can still find its first few multiples, though.
6: 6, 12, 18
The number 7 has not appeared in any of the lists of multiples; therefore, it must be prime.
Those with keen eyes may have spotted some redundancy too, all the multiples of 4 and 6 are also multiples of other (prime) numbers. In fact, this is true of any non-prime because a factor will have all the same multiples, and more multiples, than the original number. Thanks to the Fundamental Theorem of Arithmetic (as explained in Problem 3), we know that any non-prime, can be expressed as a product of its prime factors (therefore has factors which are prime).
If we were to generalise what we are doing above, we are simply listing multiples, identifying numbers which haven’t appeared. Now let's flip the script and instead of building up a bank, let's reduce an existing list. I.e let's find all the prime numbers from 2-10. We’ll, start with a list of numbers 2, 3, 4, 5, 6, 7, 8, 9, 10.
This idea can be extrapolated out over a much larger range, and that is the basic idea I used to tackle this particular Problem.
Method:
In order to use the method above, we do need to choose the starting list size. Admittedly, this is a potential downside to this method; however, I believe the quick run time counteracts this. As we are looking for the 10,001st prime, I decided a starting list of 200,000 should be plenty big enough.
Summary:
By taking a different approach to previous weeks and actually, starting with a full dataset and then reducing its size down each iteration, we were able to find the 10,001st prime number.
Want to find out more, follow this link to our introduction post - Euleryx: Let The Games Begin.
The problem of finding prime numbers will often appear in Project Euler, so I share a macro that speeds up the process.
My solution!
My 1st workflow took over 2 minutes, so I remake my workflow and now it takes 0.8 seconds.
The Generate Row tool does not output a result when the condition is met. For the trial division, I wanted to know whether I had divided it down to SQRT(N), so I needed to devise a way to find out whether I had divided it down to SQRT(N) or whether it was evenly divisible.
I made a judgment first within the LoopExpression, and if the condition was met, I output a value as a flag(-1) to prevent the loop from ending, and then exit the loop in the next loop.
My solution.
Team brute force
I generated a list of 2 and then all odd numbers (e.g. 2 - 3 - 5 - 7 - 9, etc). Then I used my Prime Macro to determine which was prime and found the 10,001th value.
I'm looking forward to see the w*tchcraft [which is apparently a banned word... who knew] that other people used to do this more efficiently :)
There really are so many ways to find prime numbers, loving the fact that everyone's solution is different so far.
@DaisukeTsuchiya , your absolutely right, prime numbers seem to be a common theme in Project Euler, thank you for sharing the macro.
Just thought I'd share one more method for solving this one:
Last week @gawa posted a custom formula function here, specifically designed for checking the primality of a number. I used this to solve this Euleryx challenge:
3) Order the results and Record ID them.
4) Filter to the 10,001st number
I was obviously expecting the function to work but I was still quite shocked by its speed. In just 0.5 seconds, the entire workflow had ran (there were 1 million records)!
Would highly recommend checking this custom formula out if you haven't already
It takes 17 seconds for me.
Thank you for mentioning my post @pilsworth-bulien-com !
I know that the custom formula technically violate the Base-A rules. However, problems themed around prime numbers frequently appear in Euleryx. Repeating a time-consuming process each time with Macro can detract from the essence of the problems. I would encourage all to utilize IsPrime function so that they can enjoy the challenges more!
@gawa, that's a very good point about the essence of the problem. I think we've all proven we can check the primality of a number using 100% Base-A, so I don't see any harm in using your function going forward, especially considering its impressive speed😄
String coding continued although the workflow isn't quite as efficient
Happy Solving, everyone!