



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




CSCI 5582 Problem Set 1 Due: Sept. 22, 1997
Problem 1. (15 points) Euclid's algorithm for finding the greatest common divisor of two positive integers m and n (assume m >= n) can be expressed as follows:
If m is divisible by n with no remainder, then the greatest common divisor is n.
Otherwise, find the remainder r that is left over when m is divided by n. Now find the greatest common divisor of n and r.
Write a recursive Scheme procedure, euclid-gcd, that takes two positive integers as arguments (assume that the first is greater than or equal to the second), and returns the greatest common divisor of the two integers. You will find the Scheme primitive remainder helpful: this takes two integer arguments and returns the remainder when the first is divided by the second.
Here are some examples of how the working procedure should behave:
(euclid-gcd 20 4) 4
(euclid-gcd 20 3) 1
(euclid-gcd 35 14) 7
Problem 2. (20 points) Consider the following peg-jumping game. There are 7 holes placed in a row. The starting position of the game has three black pegs in the three leftmost holes and three white pegs in the three rightmost holes (the center hole is empty). We could represent the starting position, then, as follows:
B B B _ W W W
A legal move is as follows:
a. A peg can move into an empty space on either side of it. b. A peg can jump over a peg of an opposite color into the empty space.
Thus, from the starting position, there are two possible following positions:
B B _ B W W W and B B B W _ W W
From the first of these possibilities, the following moves are possible:
B _ B B W W W B B B _ W W W B B W B _ W W
Your job is to write a Scheme procedure named find-peg-moves. This procedure should take as its argument a position of the game (expressed as a list of 7 symbols: three B's, three W's, and a dash) and return a list of legal positions of the game following one move. Here are a couple of examples:
Problem 5. (15 points) Write a procedure named make-n-element-tester. This procedure takes two arguments: a predicate pred and a number n. It returns a procedure which, when called on a list, will return #t if and only if the list contains at least n elements for which the predicate is true. Here are some examples of our procedure at work:
Do just o n e of problem 6a or 6b.
Problem 6a. (15 points)
Modify the branch-making procedure shown in class so that it takes four arguments: a stem-length stem, a level-count level, a branch-angle theta, and a shrinking-ratio ratio. When we call our procedure with a level argument of 0, we get a straight line of length stem. Otherwise, we should see a straight line of length stem with two smaller trees (each smaller by a factor of ratio), branching off to the left and right from the vertical stem with angle theta. Here are some examples:
These three trees are produced by the following three calls to our new branch procedure.
For instance, the middle tree is a six-level tree in which each sub-tree has a stem of length two-thirds the size of the previous-level stem, and in which the two subtrees branch off from the stem at an angle of 60 degrees.
Problem 6b. (15 points) The following sequence of numbers is due to a puzzle by John Conway (the inventor of the "Game of Life"):
1 1 1 2 1 1 2 1 1 1 1 1 2 2 1 3 1 2 2 1 1 ...