Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

skipping some i, j , ... values in Table

20 views
Skip to first unread message

Andre Hautot

unread,
May 18, 2013, 2:38:24 AM5/18/13
to

Hi,
Suppose I need a list, Flatten[Table[something[i,j],{i,10},{j,10}]],
where some combinations of indices i and j are voluntarily missing under
a known condition (for example, stupid I know, when i+j is a prime).
How can I do that ?
Thanks in advance,
ahautot

Ray Koopman

unread,
May 19, 2013, 5:46:00 AM5/19/13
to
Flatten@Table[If[missing[i,j],{},something[i,j]],{i,10},{j,10}]

spamsl...@gmail.com

unread,
May 19, 2013, 5:46:44 AM5/19/13
to
Others will chime in with more elegant solutions, but I believe this does what you wanted:

f @@@ Select[
Flatten[Table[{i, j}, {i, 1, 10}, {j, 1, 10}],
1], ! PrimeQ@(Plus @@ #) &]

That gives a list of f[i,j] where i+j aren't prime.

spamsl...@gmail.com

unread,
May 19, 2013, 5:46:55 AM5/19/13
to
Here's another solution, slightly faster for large numbers:

Rest@Flatten@
Reap@Do[If[! PrimeQ@(i + j), Sow@f[i, j]], {i, 1, 10}, {j, 1, 10}]

On Saturday, May 18, 2013 1:38:24 AM UTC-5, Andre Hautot wrote:

Sseziwa Mukasa

unread,
May 19, 2013, 5:47:00 AM5/19/13
to

Insert {} when the condition is true eg,

Flatten[Table[If[PrimeQ[i+j],{},something[i,j]],{i,10},{j,10}]]

Bob Hanlon

unread,
May 19, 2013, 5:47:05 AM5/19/13
to

sol = something @@@
DeleteCases[
Flatten[
Table[{i, j}, {i, 10}, {j, 10}],
1],
_?(PrimeQ[Total[#]] &)];


or


sol == something @@@
Select[
Flatten[
Table[{i, j}, {i, 10}, {j, 10}],
1],
! PrimeQ[Total[#]] &]


True



Bob Hanlon

James Stein

unread,
May 19, 2013, 5:47:22 AM5/19/13
to

Before calling Flatten, call Select.
0 new messages