Lists II

\(Needs["Graphics`Master`"]; 
Needs["Calculus`Master`"]; \)

Selecting from Lists

"Select" is a very powerful command, to slim down lists:

tab = Table[i, {i, 1, 10}]

Select[tab, PrimeQ]

Select[Table[i, {i, 1., 10}], PrimeQ]

Example: Mersenne Primes

A "Mersenne Prime" is a prime number of the form (2^n)-1. (n automatically has to be prime!) Let's find a few Mersenne Primes. We first define a Mersenne question and the select among the first 100 primes:

mersenneQ[n_] := PrimeQ[2^n - 1]tab = Table[Prime[i], {i, 1, 100}]

mersexp = Select[tab, mersenneQ]

mers = 2^mersexp - 1

Length[mers]

Manipulating Lists

tab = Table[Random[Real, {-10, 10}], {10}]

Sort[tab]

Reverse[tab]

tab = Table[i, {i, 1, 10}]

RotateLeft[tab]

RotateLeft[tab, 2]

RotateRight[tab]

Append[tab, 11]

Prepend[tab, 0]

Insert[tab, 4.5, 5]

ReplacePart[tab, -4.5, 5]

tab1 = Table[i, {i, 1, 6}]

tab2 = Table[i, {i, -1, -6, -1}]

{tab1, tab2}

Transpose[{tab1, tab2}]

Partition[tab1, 2]

Lists as Sets

Join[{1, 2, "Dog"}, {2, 5, π}]

Union[{1, 2, "Dog"}, {2, 5, π}]

Intersection[{1, 2, "Dog"}, {2, 5, π}]

Table[Random[Integer, {0, 5}], {10}]

Union[%]

Complement[{1, 2, 3, 4}, {1, 4, 7, 8}]

Etc

Helmut Knaust, email: helmut@math.utep.edu. Created 3-1-1999.