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]

666

(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 :

Underoverscript[∑, i = 1, arg3] (Prime[i])^2

666

(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]

10

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

10

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 ;               ... es of primes *)Do[grandTotal = grandTotal + theList[[i]], {i, 1, 7}] ; grandTotal

666

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 *)

{4, 9, 25, 49, 121, 169, 289}

FullForm[theList]             &nbs ... sp;            (* theList is really *)

List[4, 9, 25, 49, 121, 169, 289]

theList[[0]]              &nb ... p;       (* who ' s 0 ' th part is the "Head" List *)

List

ReplacePart[theList, Plus, 0]           &nbs ... sp;         (* Replace part 0 of theList with Plus *)

666


Created by Mathematica  (September 13, 2004)