Assignment 1

for Intro to Programming (with Mathematica)
due September 14 2004
Jim Mahoney

Here's my version of the solutions to the homework.  I'll try to post something like this each week.  Be aware that there's more than one "right" way to approach most of these problems.  Since I'll be discussing the answers in class, late work is worth reduced credit.

Problem 1

Question

Express the number of seconds in a year in hexadecimal form.[Hint:use Help to find functions which manipulate bases and note that hexadecimal form describes a number represented in base 16.]

Answer

I found the function I wanted by searching ?*Base*, as below.  I left the names of the units in to make it clear that they canceled out.

BaseForm[(60 sec/min) (60 min/hr) (24 hr/
              day) (365.25 day/yr), 16]

?*Base*

Problem 2

Question:

Display the first 20 powers of 2,i.e.{2,4,8,...}.

Answer:

(a)

There are several ways to do this.  Simplest is to use lists, using the fact that \(TraditionalForm\`2\^{1, 2, 3} = {2, 4, 8}\), and Range gives you a list of integers.

2^Range[20]

(b)

Another way is with a looping construction.  We haven't talked about loop constructions yet, but they're coming up soon - and I saw that several people had tried to use them for this problem.  This one generates a list by evaluating the (2^i)for i=1 to 20.

Table[(2^i), {i, 1, 20}]

(c)

And finally here's one that loops from i=1 to 20, printing out each (2^i).  
The downside of this kind of construction is that printing the result doesn't save it for another round of manipulation.

Do[Print[(2^i)], {i, 1, 20}]

Problem 3

Question:

Revelation 13.18:"Let him that hath understanding count the number of the beast, for it is the number of a man and his number is 666." Show that the sum of the squares of the first seven primes is the beast number.(Hint:see the Prime and Total functions.)

Answer:

(a)

One way is to use Range to create the integers {1,2,3,4,5,6,7}, then Prime on that to get a list of primes, then ^2 to square them, and finally Total to add 'em all up.  Boom.

Total[Prime[Range[7]]^2]

(b)

Another way is to use the Sum function, which is like an explicit loop.  Either Sum[Prime[i]^2,{i,1,7}] or using standard mathematical notation from the palette :

(∑_(i = 1)^7)(((Prime[i]))^2)

(c)

Finally, while this is a bit outside the scope of where we are, one person ran into the subtle point of the difference between Plus and Total : one takes a list, and one takes many arguments.  Note that Total only takes one argument - a list.

Plus[1, 2, 3, 4]

Total[ {1, 2, 3, 4} ]

Suppose you had constructed a list, and wanted to use Plus to add 'em up?  In other words, can you do something like what Total does if you don't have a function like that?  Well, the answer is yes - several ways, in fact - but you have to do more work.

One way is to have an explicit running total which is updated while you loop over the list.  This is what most programming languages would do.

grandTotal  =  
      0;                                                                                                 (* 
      running total *) 
theList  =  
      Prime[Range[7]]^2;                                                                  (* list of squares of primes *) 
Do[
        grandTotal  =  grandTotal + theList[[i]],  {i, 1, 
          7}];
grandTotal

A second way, which is probably more unfamiliar to most people, is to use Mathematica's "everything is a list" mentality, and explicitly turn {1,2,3,4}, which is really List[1,2,3,4], into Plus[1,2,3,4]

theList = Prime[
            Range[7]]^2                                    (* 
        list of squares of primes *)

FullForm[
      theList]                                                       (* 
      theList is really *)

\(\(\(theList[\([0]\)]\)\(\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \)\( (*\ 
      who' s\ 0' th\ part\ is\ the\ "Head"\ List\ *) \)\)\)

ReplacePart[theList, Plus, 
      0]

Problem 4

Question:

Create a notebook which presents a simple problem in your field of interest,or perhaps reproduces some recent lecture you heard,or some section of a textbook.Your notebook should have title,author information,sections,text presenting the problem and some background information,and references if appropriate.If you already possess enough Mathematica tools,include the solution also.

Answer:

This notebook does this, eh?  

Notice how I used Section, SubSection and SubSubSection cells to make the nesting work.
Also, I collapsed the Problem cells and did "Save as Special..."  HTML to make a series of webpages with links, which I then moved to the web server.

Problem 5

Question:

Explain in English what this Mathematica expression says.!(a&&b)==(!a||!b)/.{a->True,b->False

Answer:

The negation of the quantity A and B is the same as not A or not B when A is set to True and B is set to False.

All I was looking for here was that you understood what the logical notation and the /. stuff means.