# How to search for patterns to find the positions of prime numbers

I have a summation which yields a prime number at each location there is a 2, and I do not know how to search for the 2's.

N[Sum[1/(10^(n*4) - 1), {n, 1, 100}], 10^2*4]

0.0001000200020003000200040002000400030004000200060002000400040005000200060002000600040004000200080003000400040006000200080002000600040004000400090002000400040008000200080002000600060004000200100003000600040006000200080004000800040004000200120002000400060007000400080002000600040008000200120002000400060006000400080002001000050004000200120004000400040008000200120004000600040004000400120002000600060009000


In the above example, I want to find the positions of the form/pattern 0002 in the output. It should yield all the prime locations under 100. I have tried using Position[list,form] by putting the expression in for the list and putting the Out in for the list, without success. Do I have to make a list first out of the decimal output, and if so, how do I make a list with chunks of 4 digits, and then search it? Below is what I've tried.

Position[N[Sum[1/(10^(n*4) - 1), {n, 1, 10}], 10^2*4], 2]

{}

Position[%%, 0002]

{}

• The output is not list. it is an atomic number. so what do you mean by position? – Algohi Feb 8 '15 at 5:10

A slighlty different RealDigits approach:

N[Sum[1/(10^(n*4) - 1), {n, 100}], 10^2*4];

Partition[RealDigits[%][[1]], 4, 4, -1] ~Position~ {0 .., 2} // Flatten

{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,
43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}

• @Karsten Thanks for pointing out my mistake. By the way I already voted for your answer; I guess I should have read it more carefully. – Mr.Wizard Feb 8 '15 at 11:21
• This method is slower but easier on the monitor display, it's a tough choice of which one is better for dealing with large numbers. Thanx for your contribution. – user24719 Feb 15 '15 at 23:11
• I've been using this method exclusively now since trying to do larger numbers, and it's the best. thanks once again. I have a new formula that can give the primes in a specified range. At higher ranges, there are less primes. What happens when there are no results to display? Does the progran know how to terminate? Do you know a bit of code I could include for when there are no primes, that would return a default like zero ? – user24719 Feb 25 '15 at 3:51
• @user24719 If Position finds no matches it returns {}. You could therefore check to see if the result is (SameQ) {} replace it with whatever you wish. Is that what you mean? – Mr.Wizard Feb 25 '15 at 23:23
• I'm not sure what you mean with (SameQ). I would have to see it put into practice for me to understand it. The coding aspects of everything I'm trying to do is just so way over-my-head. I understand the Math but not the Mathmatica language. I haven't learned all the magical words to chant, And the documentation only gets me so far. That's why I greatly appreciate the help you've given me. You've helped me take a concept off the drawing board and actually give it some wheels, some realization. Thank you so much. – user24719 Mar 5 '15 at 1:06

One approach would be to first convert the output of

N[Sum[1/(10^(n*4) - 1), {n, 1, 100}], 10^2*4]


to a String dropping the leading 0.

str = StringDrop[ToString@%, 2]


Than one can use StringPosition

Last /@ StringPosition[str, "0002"]/4

{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79,
83, 89, 97}


An other approach would be to use the RealDigits of

N[Sum[1/(10^(n*4) - 1), {n, 1, 100}], 10^2*4]


In this case we have to add the first 000

{0, 0, 0}~Join~First@RealDigits[%]


Then this list can be partitioned into sublist of length 4 and converted to digits

FromDigits /@ %~Partition~4


Now the position of the 2s can be found with

Position[%, 2] // Flatten

{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79,
83, 89, 97}


Or

Flatten@Position[
Tr /@ (RotateLeft @@ RealDigits[%]) ~Partition~ 4,
2]

{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79,
83, 89, 97}

• Okay, so I put it all together into one input, like this – user24719 Feb 12 '15 at 5:09
• N[\!( *UnderoverscriptBox[([Sum]), (n = 1), (100)](1/((((10^((((n))*4)))) - 1)))), (10^2)*4] str = StringDrop[ToString@%, 2] Last /@ StringPosition[str, "0002"]/4 – user24719 Feb 12 '15 at 5:10
• and I still got three outputs on screen. The problem I have with this is that this toy problem is simple, and the real problems I want to solve are much bigger, so much bigger that one would never want the output displayed on screen. I would prefer to "export to file", so my follow up question is how to do this, export and import outputs? I've tried by myself, like this – user24719 Feb 12 '15 at 5:17
• In[78]:= N[\!( *UnderoverscriptBox[([Sum]), (n = 1), (100)](1/((((10^((((n))*4)))) - 1)))), (10^2)*4] >> "ddneg100" str = StringDrop[ToString@"ddneg100", 2] >> "ddneg100string" Last /@ StringPosition[str, "0002"]/4 Out[80]= {} – user24719 Feb 12 '15 at 5:18
• and I get nothing for in brackets, just empty brackets. Please help. – user24719 Feb 12 '15 at 5:19
a = N[Sum[1/(10^(n*4) - 1), {n, 1, 100}], 10^2*4];
Quotient[3 + SequencePosition[RealDigits[a][[1]], {0, 0, 0, 2}][[All, 2]], 4]


{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}

I just invented this last week.

Copy the following into Mathematica, select all, right-click on Convert To, choose StandardForm.

N[Sum[2/10^((n*(Floor[c/n] + 1) - c)*s), {n, 1, Floor[Sqrt[ c]]}] + Sum[2/(10^((n*(Floor[c/n] + 1) - c)*s)* (10^(ns) - 1)), {n, 1, Floor[Sqrt[ c]]}] + Sum[2/(10^((o^2 - o - c) s)*(10^(o*s) - 1)), {o, Floor[Sqrt[c]] + 1, max}], ((max + 1)^2 - c)*s]; AbsoluteTiming[Flatten[ Position[Partition[ RealDigits[%][[1]], s, s, -1], {(0) .., 2}]]] + c

-end

c=crossover s=spacing


The crossover is the number which you want to surpass. The spacing is difficult to determine, so I'll say that, in general, if you use a number which is equal to the number of digits of the max^2, you should be safe. For all primes up to 100^2 = 10000, that is five digits, so use a 5 for the spacing. Follow this rule and you should be safe.

For those who are wondering, the code is based on a bigger structure of numbers formed by the Sieve of Eratosthenes.

N[Sum[2/(10^(Mod[-(10 - n^2), n]*4)*
(10^(n*4) - 1)),
{n, 1, Sqrt[10000]}], (10000 - 10)*4]
str = StringDrop[ToString[%], 2]
(1/4)*Last /@ StringPosition[str,
"0002"] + 10


100 seems to be the practical limit when it comes to computer runtime. I asked my 32GB RAM computer to do 1000 and it just keeps running forever. It's still impressive that I can get all the primes under 100^2 in the blink of an eye, but not near as impressive as I need it to be to look for primes in the range that will win the EFF Award for largest provable prime. I'm going to try to work on a method that goes in the positive direction instead of the negative decimal direction.

• Please learn how to format your posts. You can format inline code and code blocks by selecting it and clicking the {} button above the edit window. The edit window help button ? is also useful for learning how to format your questions and answers. – Michael E2 Feb 15 '15 at 23:05