Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I use the following code to find out the number of consecutive prime numbers using a formula $n^2+n+i$ found out by Euler (starting from n=0):

Nbs = {};
Do[Nbs = Union[Nbs, 
Select[Range[5000], (PrimeQ[#^2 + # + i] == False &), 1]], {i, 1, 
5000}];
Nbs 

How can I also get in the output list the value of the Do iterator where $i$ is corresponding to each number of consecutive primes?

I would like to get something like that:

({1,2},...,{40,41}}
share|improve this question
1  
Look up Reap and Sow –  Mike Honeychurch Nov 27 '13 at 22:17
    
I don't think your question can be done ... without adding an If and Sow to your Select condition which is ugly beyond reason. Do you care about do this kind of filtering, or is your question specifically about using Select? –  Gabriel Nov 29 '13 at 2:19
1  
Also you might want to be careful about using capital letters for variables when asking questions ... agitates the style police ... –  Gabriel Nov 29 '13 at 2:23
    
@Gabriel A solution was found by Kevin without using Reap and Sow that I do not know how to use here.The Select has to be of the form [list,crit, 1] because I need to get only the first element of the list that satisfies the criteria. –  Sigismond Kmiecik Nov 29 '13 at 21:37
add comment

2 Answers 2

up vote 2 down vote accepted

I don't fully understand your question, but I believe you want either Nbs2 or Nbs3 (same data, sorted differently) in this code:

Nbs = {};
Do[AppendTo[Nbs, 
   Append[Select[Range[5000], (PrimeQ[#^2 + # + i] == False &), 1], 
    i]], {i, 1, 5000}];
Nbs2 = DeleteDuplicates[Nbs, (#1[[1]] == #2[[1]]) &];
Nbs3 = Sort[Nbs2];

Where Nbs2:

{{4, 1}, {1, 2}, {2, 3}, {10, 11}, {16, 17}, {40, 41}, {3, 65}, {6, 77},
 {12, 221}, {5, 347}, {7, 437}}

and Nbs3:

{{1, 2}, {2, 3}, {3, 65}, {4, 1}, {5, 347}, {6, 77}, {7, 437},
 {10, 11}, {12, 221}, {16, 17}, {40, 41}}

Nbs holds the whole data since I am not using Union there anymore so that I can keep the iterator as you needed.

share|improve this answer
    
Yes it is indeed a solution at the expense of keeping all data with your use of AppendTo. It avoids the pitfall I went into by using Union , thus eliminating all duplicates straight away. –  Sigismond Kmiecik Nov 29 '13 at 20:05
add comment

EDIT

My previous answer related to the title of the question and was directed to "reaping" or "catching" first cases of a condition being met from a loop.

The Euler formula, as I now understand, was a method for generating consecutive prime numbers : $n^2+n+k$, where $k$ is prime and $n$ ranges from 0 to $k-2$. With all due respect this does not appear consistent with the aim of the code. A relevant reference here.

The following relates to the use of Euler formula to generate consecutive primes:

f[u_?PrimeQ] := Module[{s = {}, j = 0},
  While[PrimeQ[j^2 + j + u] && j <= u - 2, AppendTo[s, j^2 + j + u]; 
   j++]; {u, Length@s, s}]
f[u_] := Sequence[];

Then:

Grid[Prepend[
  f /@ Range[100], {"k", "number of consecutive primes", "Primes"}], 
 Frame -> All]

yields:

enter image description here

Further, if the intention is just to explore the effect of any the original code, miscounts as if i is composite the corresponding value j will be 1 greater than the count of consecutive primes, eg i=77-> 79, 83, 89, 97, 107}, i.e. 5 primes not 6 as listed in the OP code (the value for n=6 being 119=7 x 17). My original answer listed all 5000 "answers" (in compact form) based on merely answering the title of the question.

If the purpose is to explore i composite or prime:

g[u_?PrimeQ] := Module[{set = {}, j = 0},
  While[PrimeQ[j^2 + j + u] && j <= u - 2, AppendTo[set, j^2 + j + u];
    j++];
  {u, Length@set, set}]
g[u_] := Module[{set = {}, j = 1},
  While[PrimeQ[j^2 + j + u] && j <= u - 2, AppendTo[set, j^2 + j + u];
    j++];
  {u, Length@set, set}]

I note that up to 100000, the case i=41 yields the longest number of consecutive primes:

prim = g /@ Range[100000];
mx = Max[prim[[All, 2]]];
Cases[prim, {_, mx, _}]

yields:

{41, 40, {41, 43, 47, 53, 61, 71, 83, 97, 113, 131, 151, 173, 197,
223, 251, 281, 313, 347, 383, 421, 461, 503, 547, 593, 641, 691,
743, 797, 853, 911, 971, 1033, 1097, 1163, 1231, 1301, 1373, 1447,
1523, 1601}}}

share|improve this answer
    
Your solution is OK for some value of i but not all of them (see solution given above). I think the problem lies in the fact that you do not catch the cases when n= 0 (it is implicit in the way the Select is coded in my question) –  Sigismond Kmiecik Nov 29 '13 at 20:20
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.