Functions and the Map command

Comprehension Questions

Math 250 - Mathematical Computing
Christopher Hanusa

5-1. Before evaluating the following lines of code, anticipate what will be the result will be.  
     After evaluating the code, understand why the output is what it is and compare it with your expectations.

Sin@Pi

Map[Sin,{0,Pi/2,Pi,3Pi/2}]

Map[Sin, Pi]

Range@Prime@10

Map[Prime,{1,2,4,8}]

Map[f,Map[g,Range[5]]]

f/@g/@h/@Range[5]

5-2. Explain the difference in the outputs between Framed @ {x,y,z} and Framed /@ {x,y,z}.

5-3. Use the functions Prime, Map, and Range commands to generate a list of the 100th, 200th, ... up to 2000th prime numbers.

5-4. If there is some coordinate data ((x,y)-pairs) in the lists data1, data2, data3, and data4, how can you make a list of scatterplots of these datasets in one line of code?

5-5. What do the following functions do?

f1[x_]:=Length[Range[Prime[x]]]

f2[x_,y_]:={x,y}

5-6. Define a function that takes as input a number n and outputs the (2^n)-th prime number

5-7. Define a function that takes as input three variables and outputs the sum of the first and third number and divides by the second.

5-8. Define a function that takes as input a list and outputs the average value of the entries.  Use the function Total.

5-9. What is wrong with the following code?  
      It is supposed to take in a number, divide by two, then give the floor of that number.
      For example, the output for an input of 19 should be Floor[19/2]=Floor[9.5]=9.

fountainOfYouth[x]:=Floor[x/2]
fountainOfYouth(19)

Challenge Questions

5-X1. Below is the Mathematica input and output for someone hoping to make a list of square numbers and then append the next square number to the end of that list. How should the code be modified to do these two desired operations?

squares=Table[i^2,{5}];
Append[6^2,%%];
squares

5-X2. Use various commands that you have learned so far to create the list

{1,1,1,2,4,8,3,9,27,4,16,64,5,25,125,6,36,216,7,49,343,8,64,512,9,81,729,10,100,1000}

5-X3. Use the Reverse function as necessary to change triangle from Example 5.2.1 into

{{10,9,8,7,6,5,4,3,2,1},
{9,8,7,6,5,4,3,2,1},
{8,7,6,5,4,3,2,1},
{7,6,5,4,3,2,1},
{6,5,4,3,2,1},
{5,4,3,2,1},
{4,3,2,1},
{3,2,1},
{2,1},
{1}}

5-X4. Use a Table command to create a list of 10 to 100 points that lie in the range [0,100]x[0,100].  Then use the Map command to apply the function Disk to them.  Lastly, wrap this whole list in a Graphics command, and you will see your points displayed.

5-X5. Create a function that takes as input a list of numbers, interprets the list as a sequence of two-dimensional points (coordinate pairs), and outputs a scatterplot of these points.  You may find the function Partition helpful.

5-X6. Create a function that takes as input a number n and gives as output an image with of n random points in the range [0,100]x[0,100].  (Similar to 5-X3 above.)