[Date Index]
[Thread Index]
[Author Index]
Re: AGAIN Nested while loop!!!
*To*: mathgroup at smc.vnet.net
*Subject*: [mg71647] Re: AGAIN Nested while loop!!!
*From*: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
*Date*: Sat, 25 Nov 2006 05:37:41 -0500 (EST)
*Organization*: The Open University, Milton Keynes, UK
*References*: <ek63rd$907$1@smc.vnet.net>
mumat wrote:
> I have a list of numbers
>
> lst = Table[Random[Integer, {i, 50}], {i, 6}]
>
> I want to write a program that if There are two numbers x, y in A where
>
>
> Mod[x^2+y^2, 5]==0 reuturn False and the pair {x,y}, otherwise True!
>
>
> For[i = 0, i < 6, For[j = i, j < 6,
> If[Mod[lst[[i]]^2 + lst[[j]]^2, 3] == 0,
> Return[False,{i,j}]];Break[], j++], i++]
>
> While loop and nested while loops accept only one counter "i".
>
> i=1;j=2;
>
> While[i<6 && While[j<6 &&
> Mod[lst[[i]]^2+lst[[j]]^2,7]=!=0,j++];i++];{i,j}
>
> {2,6}
The following should fit your needs:
In[1]:=
lst = Table[Random[Integer, {i, 50}], {i, 6}]
Out[1]=
{33, 13, 28, 17, 15, 24}
In[2]:=
pairs = DeleteCases[Flatten[Outer[List, lst, lst], 1], {x_, x_}]
Out[2]=
{{33, 13}, {33, 28}, {33, 17}, {33, 15}, {33, 24}, {13, 33}, {13, 28},
{13, 17}, {13, 15}, {13, 24}, {28, 33}, {28, 13}, {28, 17},
{28, 15}, {28, 24}, {17, 33}, {17, 13}, {17, 28}, {17, 15}, {17, 24},
{15, 33}, {15, 13}, {15, 28}, {15, 17}, {15, 24}, {24, 33},
{24, 13}, {24, 28}, {24, 17}, {24, 15}}
In[3]:=
(If[Mod[#1[[1]]^2 + #1[[2]]^2, 5] == 0, {False, #1}, True] & ) /@ pairs
Out[3]=
{True, True, True, True, {False, {33, 24}}, True, True, True, True,
{False, {13, 24}}, True, True, True, True, {False, {28, 24}}, True,
True, True, True, {False, {17, 24}}, True, True, True, True, True,
{False, {24, 33}}, {False, {24, 13}}, {False, {24, 28}},
{False, {24, 17}}, True}
or if you are interested in the first pair only
In[4]:=
pos = Position[pairs, {x_, y_} /; Mod[x^2 + y^2, 5] == 0, 1, 1]
Out[4]=
{{5}}
In[5]:=
If[Length[pos] > 0, {False, Extract[pairs, pos][[1]]}, True]
Out[5]=
{False, {33, 24}}
Regards,
Jean-Marc
Prev by Date:
**Bookmarks in Mathematica????**
Next by Date:
**Re: symbolic solution of ODE**
Previous by thread:
**Re: AGAIN Nested while loop!!!**
Next by thread:
**Re: AGAIN Nested while loop!!!**
| |