CS99
Summer 2002: HW 6 07.16
Slip
sliding snails, conjectures
Due Date: 07.19
1. Objectives
Completing all tasks in this assignment will help you:
First skim, then read the entire
assignment before starting.
2. Sisyphus,
the Snail
A snail is at the bottom of an H-foot well and wants to climb to the
top. The snail can climb U
feet during the day when it is rested.
But because the snail gets tired of climbing, each successive day, it
climbs (F/100)
x U less than it did the day before. F
is the fatigue factor; the distance
lost to fatigue is always F% of the first day's climbing distance.
Every night, the snail slides D feet, when it’s sleeping.
Depending on the values of the parameters, the snail will eventually either
leave the well or slide back to the bottom (which is to say, the snail's height
will either exceed the height of the
well or become negative). The snail
should also never climb a negative number. Write a program called snail.m to
find out which happens first and on what day.
Sample Output.
>>snail
Enter well height: 6
Enter distance snail can travel during day while rested: 3
Enter distance snail slides down at night: 1
Enter the fatigue factor: 10
Calculating ... the snail climbs out on day 3.
>>snail
Enter well height: 50
Enter distance snail can travel during day while rested: 6
Enter distance snail slides down at night: 4
Enter the fatigue factor: 1
Calculating … the snail is stuck in the well on day 68.
You will find the MATLAB command break (help break)
useful. To test your program, see if
for the input values (H, U, D, F) = { (10, 2, 1, 50), (50,
5, 3, 14), and (50, 6, 3, 1) } your
program finds that the snail { ( fails on day 4 ), ( fails on day 7
), and ( succeeds on day 20
) }.
3. Goldbach's
Conjecture*
Goldbach's
Conjecture: For any even number n ³ 4, there exists at least one pair of
prime numbers p1 and p2 such that n = p1
+ p2.
No one is sure whether this conjecture holds, but no one has ever found an
exception.
Your task is to write a program that reports all the pairs of prime numbers
satisfying the condition for each number in a sequence of even numbers supplied
by the user (if the user enters an odd number or a number less than 4, your
program should quit).
Note that we are interested in essentially
different pairs, so your program should not report (p1, p2)
and (p2,
p1) separately.
You will find the built-in MATLAB function primes (help primes) very useful
here. For an array p, the kth
element is p( k ).
If p = [2 3 5 7 11 13],
p( 1 ) = 2,
p( 6 ) = 13. Your program will contain a nested loop.
Save your program in a file called goldbach.m.
Sample Output.
>>goldbach
Please enter a sequence of even numbers, each greater or equal to 4.
> 4
4 = 2 + 2. (1 pair)
> 18
18 = 5 + 13 = 7 + 11. (2 pairs)
> 94
94 = 5 + 89 = 11 + 83 = 23 + 71 = 41 +
53 = 47 + 47. (5 pairs)
> 3
Bye!
See if you
can mimic my output exactly with fprintf.
4. Submitting
your work
Type your name, student ID, and the date at the top of each
file. Only snail.m is
required. Print and sign each
file. Hand the signed documents to the
instructor in lab on Friday, 19 July
2002 between the hours of 10AM to 12:30PM (you may also hand in your assignment
in lab on Thursday, 18 July 2002 if you finish early).
*Challenge Problem