# How can I extract when a select condition is met in a do loop the value of the iterator?

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}}

-
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
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. – Sigis K Nov 29 '13 at 21:37

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.

-
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. – Sigis K Nov 29 '13 at 20:05

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:

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}}}

-
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) – Sigis K Nov 29 '13 at 20:20