Rational numbers

A rational number is a ratio of two integers $\frac{n}{d}$ where $n$ is called the numerator, and $d$ is called the denominator. The denominator cannot be zero.

Because many rational numbers represent the same ratio or value, such as $\frac{-2}{1}$, $\frac{4}{-2}$, $\frac{-8}{4}$, etc., it is usually best practice to always write rational numbers in a normal form. The normal form may be found by applying the following two rules:

  1. If the greatest common divisor of $n$ and $d$ is not one, then divide both the numerator and denominator by that divisor.
  2. If the denominator is negative, multiply both the numerator and the denominator by $-1$.

If the numerator of a rational number in normal form is a positive integer, then the rational number is said to be positive, and if the numerator is negative, the rational number is said to be negative. For example, $\frac{-4}{-2}$ is positive because the normal form for this ratio is $\frac{2}{1}$ and $4 > 0$.

The normal form for zero is $\frac{0}{1}$.

Algorithm 7.A(a): Addition of non-negative rational numbers

Input: two non-negative rational numbers $p \ge 0$ and $q \ge 0$. Output: a non-negative rational number.

Describe an algorithm for adding two positive rational numbers. You may assume the computer can add and multiply two integers and can calculate the greatest common divisor of two integers.

Algorithm 7.A(b): Subtraction of a smaller or equal positive rational number from a larger or equal rational number

Input: two non-negative rational numbers $p \ge q \ge 0$.
Output: a non-negative rational number.

Describe an algorithm for subtracting a smaller positive rational number from a larger positive rational number. You may assume the computer can subtract and multiply two integers and calculate the greatest common divisor of two integers.

Algorithm 7.A: Adding two rational numbers

Input: two rational numbers $p$ and $q$.
Output: a rational number.

Describe an algorithm for adding two rational numbers. You may assume the computer can perform the operations described in Problems 1 and 2 above.

Algorithm 7.B: Multiplying two rational numbers

Input: two rational numbers $p$ and $q$.
Output: a rational number.

Describe an algorithm for multiplying two rational numbers. You may assume the computer can multiply two positive integers, determine the greatest common divisor of two positive integers, and divide a positive integer by another positive integer.

Algorithm 7.C: Dividing one rational number by another if it is possible

Input: two rational numbers $p$ and $q$.
Output: 'yes' or 'no', and if 'yes', a rational number.

Describe an algorithm for dividing one rational number by another. You may assume the computer can multiply two rational numbers.

Algorithm 7.D: Raising a rational number to an integer power

Input: a rational numbers $p$ and an integer $n$.
Output: a rational number.

Describe an algorithm for raising a rational number to an integer power. That integer power may be either positive, zero or negative.

Recall that we define $p^0 = 1$ even if $p = 0$.

Algorithm 7.E: Arithmetic operations with integer operands

Describe algorithms for all of the above problems but where one of the operands may be an integer as opposed to a rational number. For example, how do you calculate $2 + \frac{3}{4}$, $\frac{3}{4} - 2$, $3 - \frac{5}{7}$, $2\cdot\frac{3}{4}$, $2\div\frac{3}{4}$ and $\frac{3}{4} \div 2$.

Note, there are two significantly different approaches to describing such a algorithms. The easier algorithm would be to interpret an integer $n$ as $\frac{n}{1}$ and then call one of Algorithms 7.A-C; however, this would require more work, as this would require calculating

$5 + \frac{7}{3} = \frac{5}{1} + \frac{7}{3} = \frac{3 \cdot 5 + 7 \cdot 1}{1 \cdot 3} = \frac{15 + 7}{3} = \frac{22}{3}$.

It would be easier to describe a different algorithms that has you calculate

$5 + \frac{7}{3} = \frac{3 \cdot 5 + 7}{3} = \frac{15 + 7}{3} = \frac{22}{3}$,

which requires only one multiplication of integers, and not three.

Algorithm 7.F: Comparing two rational numbers

Input: two rational numbers $p$ and $q$.
Output: 'less than', 'equal to' or 'greater than'.

Given two rational numbers $p$ and $q$, describe an algorithm that allows you to determine if $p < q$, $p = q$ or $p > q$.

You may assume the computer knows how to compare two integers.