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) 1 STk> (min-of-4 9 0 21 -2) -2 STk> (min-of-4 2 2 2 5) 2
2 points
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) 7 STk> (sum-func 3 5 4) 3 STk> (sum-func 9 9 9) 2
3 points
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) 2.0000049116755 STk> (cube-root 27) 3.00000054106418 STk> (cube-root 3.375) 1.50000111249087 STk> (cube-root 0.015625) 0.251206551211726
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.
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) #t STk> (relatively-prime? 24 26) #f STk> (relatively-prime? 8 9) #t STk> (relatively-prime? 11 22) #f
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) a (gcd b (remainder a b))))
1 point
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) #t STk> (modulo-inverse? 3 4 13) #f STk> (modulo-inverse? 342 453 30985) #t STk> (modulo-inverse? 342 454 30985) #f
2 points
(expmod a b c)
from the book which efficiently
computes the value a^b mod c
(define (square x) (* x x)) (define (expmod base exp m) (cond ((= exp 0) 1) ((even? exp) (remainder (square (expmod base (/ exp 2) m)) m)) (else (remainder (* base (expmod base (- exp 1) m)) m))))
expmod
to verify that the cipher text you found
above decrypts properly back to the numeric encoding for
abstract.
Congratulations on completing Lab 2!
Lab 2 point total: 10 pts