CSci 1901: Lab 2, January 25

Lab 2: Writing Procedures

Step 1 (Drill)

Write a procedure that takes in 4 arguments and returns the smallest argument. You may NOT use the builtin min procedure, though you may feel free to use it in all future labs.

STk> (min-of-4 1 3 5 6)
STk> (min-of-4 9 0 21 -2)
STk> (min-of-4 2 2 2 5)

2 points

Step 2 (Drill)

Write a procedure, sum-func, that takes in three numeric arguments and returns the sum of the largest two arguments divided by the third argument.

STk> (sum-func 8 6 2)
STk> (sum-func 3 5 4)
STk> (sum-func 9 9 9)

3 points

Step 3 (Drill)

Complete Exercise 1.8 from the textbook. The code for using Newton's method to iteratively compute square roots is given in the textbook in Section 1.1.7. You may modify the existing code for square roots or do this exercise from scratch, this is your choice.

STk> (cube-root 8)
STk> (cube-root 27)
STk> (cube-root 3.375)
STk> (cube-root 0.015625)

2 points

The following two questions will have you write two predicates that we will be using next week to preform RSA encryption and decryption.

Step 4 (Application - RSA Encryption)

Write a predicate (a procedure that returns #t or #f) relatively-prime? that takes in two numbers and determines if they are relatively prime. Two numbers are relatively prime if the greatest common divisor of those two numbers is one.

STk> (relatively-prime? 2 3)
STk> (relatively-prime? 24 26)
STk> (relatively-prime? 8 9)
STk> (relatively-prime? 11 22)

You may make use of the following procedure, gcd, from the book which determines the greatest common divisor of two numbers using Euclid's algorithm.

(define (gcd a b)
  (if (= b 0)
      (gcd b (remainder a b))))

1 point

Step 5 (Application - RSA Encryption)

Write a predicate which takes in three numbers and determines if the first two numbers are the inverse of each other modulo the third number. Two numbers, say a and b are the inverse of each other modulo some number c if a*b mod c = 1. For this problem you may make use the builtin in procedure (remainder p m) which computes p mod m.

STk> (modulo-inverse? 3 4 11)
STk> (modulo-inverse? 3 4 13)
STk> (modulo-inverse? 342 453 30985)
STk> (modulo-inverse? 342 454 30985)

2 points

Bonus Question (RSA Encryption)

Next week you will write some procedures for preforming RSA encryption, a very common encryption and decryption method. It will not be necessary to understand that mathematics behind RSA encryption, but understanding the math will make the lab more interesting. For this bonus question read through the following description of RSA encryption and answer the following questions:

Congratulations on completing Lab 2!
Lab 2 point total: 10 pts

This is what you will learn in this lab

Copyright: © 2000-2006 by the Regents of the University of Minnesota
Department of Computer Science and Engineering. All rights reserved.
Comments to: Maria Gini
Changes and corrections are in red.