I’m fairly new to programming with Mathematica and I’m trying to use Do loops and iteration as little as possible. One typical problem I run into is this:
The following code quickly finds all prime twins ( the first member of them) that are smaller than 100
I would like to know if it is possible to use a similar simple line of code to find the first hundred prime twins without using a do or for loop and iteration, as I would normally do with a procedural language. I studied the manuals and several books, but I haven’t found a way to do this.
Thanks for the reply, Ilian. Hadn’t thought of that.
Still, the code still needs some “advance” knowledge, because you have to tell it to generatie 1,000 primes first. But that assumes 1,000 consecutive primes will contain 100 prime twins and that may not be known in advance. In addition, it requires extra computation time. Do you think it would be possible to have a solution without the Range[1000], or is that impossible with a functional language?
Right, to figure out the correct range to use we would need a lower bound on the twin prime counting function, which isn’t known or else we could have proved a famous conjecture
I understand that this is a very targeted discussion to avoid usage of Range. But still just for the sake of general information I will notice the following. There is a curious coincidence - we had a very popular post about computing twin primes - maybe you can find something useful there: ** Ulam spiral to appear on Sept. 2013 cover of Math Horizons **. You could also use a function that calls Wolfram|Alpha to get those numbers in increments of 20:
This one is still a bit over my head, but well worth studying. It does achieve my goal of avoiding a for or do loop and it also doesn’t use the range command.
First it figures out how many primes there are up to a 100 (PrimePi[100]) then iterates just over the primes and checks if nth prime + 2 is also a prime. Only if so the results are sown and reaped afterwards.
for m=10000 it takes 0.27 seconds on my computer, the solution by Ilian Gachevski takes 3.26 on my computer (so my code is about 12X faster).
How many are you looking for? I’m pretty sure you can’t go much faster in Mathematica in just a few lines. Parallelisation could speed things up quite a bit more…
O! In addition you could look at functions like NextPrime and check if if it two more than the previous one. I believe, though, that the function is not very fast. But you could try it for your self
There is an error in your submission, it should be PrimeQ [ n ] , and for large number of primes, replaceall will give an error becuase MaxIterations is by default 2^16. So I changed the code of the 3 solutions now to measure the time needed to generate the primes and to measure computational duration for generating 100000 primes:
So although the code with replaceall is very neat, it is slow compared to the others. The main problem with Joe’s solution is that primes get more and more separated for large numbers, so the stepping with 2 or 4 makes it slow.
There is something funky about “publishing” code on this forum. I cut and pasted working code, but when published the was gone. I edited my post and it appears when editing, but not when published!? I finally got it to “take” by replacing with @n.
I guess I need to learn how to use the tags to get a code box.