Introduction to Algorithms
Week 1: Exercises

These are optional exercises for practicing the concepts we learned this week.

1. Powers of Two

What is the approximate value of

a) 214

b) 232

c) 88

2. Logarithms

What is the approximate value of

a) log2(8,000,000,000)

b) log16(16,000,000)

3. Slow Connection

A notebook computer's disk holds 500 Gb (gigabytes) of data. We would like to send the data across an extremely slow network that transfers only 1 byte/second. Approximately how long will the transfer take?

4. Squares Mod 4

Is this statement true or false?

For all n ∈ ℤ, n2 mod 4 < 2.

Prove your answer.

5. Bases

  1. What is 1101002 in base 10? What is ab16 in base 10?

  2. What is 9510 in base 2? In base 16?

  3. What is 678 in base 10?

  4. What is 6710 in base 8?

  5. A byte holds a value from 010 to 25510. In hexadecimal, what is the smallest and largest value that can be stored in a byte?

  6. How many digits does 10010 have when written in base 5? How many digits does 50010 have when written in the same base?

6. Counting in Binary

Write a program that reads an integer N (in decimal), and prints out all values from 0 to N in binary representation, with one number per line.

7. Zeroes versus Ones

Let's say that a number is

Write a program that reads a decimal integer and reports whether it is light, heavy, or balanced.

8. Base 7 to Base 3

Write a program that reads a number written in base 7, and writes the number in base 3.

9. Reverse the Digits

Write a program that reads an integer N and prints out N with its digits reversed. Do not use any strings (except for reading the input) or lists. You must reverse the digits using arithmetic operations only.

Hint: extract N's digits in order from least significant to most significant; use those to build an integer K in which the least significant digit of N becomes the most significant digit of K.