Week 5: Exercises

1. Googol

A googol is the number 10100.

Write a program that computes and prints the number

10100 mod 47

2. Very Big Number

Consider the number N = 2^(2300).

a) Suppose that we want to write N as a decimal number. There are approximately 1080 atoms in the observable universe. If we write one digit on each atom, are there enough atoms to write N?

b) Write a program that computes and prints the value of (N mod 1,000,003).

3. Minimal Multiplication

Write a static method int pow47(int n) that computes

n100 mod 47

Your program should perform at most 10 multiplication operations. Do not call any library functions.

4. Long Exponentiation

Write a static method int pow47(long a, long b) that computes

ab mod 47

Your method should run quickly even for large values of b.

5. Set of Longs

Write a class Set that represents a set of values of type 'long'. Your class should have the following members:

What will be the average-case big-O running time of add() and contains()?

6. Polynomials

Design and implement a C# class Polynomial representing a polynomial of a single variable. Your class should include the following:

7. Queue

Write a class Queue implementing a queue of integers. A queue should have an unlimited size. The Queue class should have these members:

All operations should run in O(1).

8. Double-Ended Queue

Write a class Deque implementing a double-ended queue of integers:

Your class should have these members:

All operations should run in O(1). In dequeueFirst() and dequeueLast(), you may assume that the queue is not empty.

9. Array to Tree

Write a method that takes an array containing a sorted list of integers and returns a binary search tree containing all the values in the array. The tree should be as balanced as possible, i.e. it should have the minimum possible height.

10. Tree Check

Write a method that takes a binary tree and returns true if the tree satisfies the ordering requirements of a binary search tree.