CPSC 215 — Assignment 2
Calculating with Lists

Winter 2002 — February (1^st), 2002 — C. Jacob

<your name here>
<your student ID here>

Instructions for this Assignment

Welcome to the second assignment for CPSC 215 — Introduction to Programming with Mathematica.

(1) In the following sections, enter your answers as Mathematica commands in the Input cells provided after each problem description or question. Whenever several lines labelled by SOLUTION 1, SOLUTION 2, etc. are given, provide different ways of solving the problem.

(2) After you have solved all the problems, go to section "How to Submit Your Notebook" and follow the instructions.

Note: This Assignment 2 is due February (14^th), 2002.

Generating Lists

Problem 1. Multiples

Construct a list of the positive multiples of 7 which do not exceed 100.

SOLUTION 1:

SOLUTION 2:

Problem 2. Prime Numbers

Construct a list of the first 10 prime numbers

SOLUTION 1:

SOLUTION 2:

Problem 3. Polynomials

Let p(x) = (x^2) - 8  x + 10. Compute the values of p(x) for x = 1, 2, 3, …, 10.

p[x_] = (x^2) - 8  x + 10;

SOLUTION 1:

SOLUTION 2:

Problem 4. Descending Integers

Construct a list of the integers 1 to 20 in descending order.

SOLUTION 1:

SOLUTION 2:

SOLUTION 3:

Calculating with Lists

Problem 5. Integer Digits

The Mathematica function IntegerDigits returns a list containing the digits of an integer.

SOLUTION

Problem 6. Fibonacci

The Mathematica function Fibonacci[n] returns the (n^th) Fibonacci number. Compute the sum of the digits of the (100^th) Fibonacci number.

SOLUTION:

Problem 7. Mississippi

The Mathematica function Characters["str"] gives a list of the individual characters in a string str.

SOLUTION:

SOLUTION:

Problem 8. Philosophers (1)

Ten philosophers are having dinner sitting around a table. As none of them likes to be left with the dirty dishes at the end of the meal, they decide to use the following algorithm to choose the one who has to take care of the dishes at the end of their gathering:

(1) They assign themselves consecutive numbers (1 to 10) in the order they sit around the table.

(2) Every other philosopher will leave the party — one after the other, until only one person is left, the "lonely dishwasher".

Start with the following list of philosophers:

philosophers = Range[10]

SOLUTION

Problem 9. Philosophers (2)

Soon as the philosophers realized that their algorithm is not very fair, they decided to try the following new algorithm to choose the "lonely dishwasher".

(1) They generate ten cards, each of which has a different number between 1 and 10.

(2) After they have shuffled the cards and put them in the middle of the table, one of the philosophers draws a card. The philosopher with the number on the card drawn has to leave the party.

Start with the following list of philosophers:

philosophers = Range[10]

SOLUTION (step-by-step)

SOLUTION (one-step calculation)

\(\(philosophers = {"Kant", "Socrates", "Diogenes", "Plato"};\)\)

philosopherLottery[philosophers]

How to Submit Your Notebook

Step 1

Save this notebook under the filename Ass2-<your student id>.nb.

For example, if your student ID is 123456, you would save this notebook as Ass2-123456.nb

Step 2

Send your notebook by email to your Teaching Assistant (TA).

Note: This Assignment 2 is due February (14^th), 2002.