Gaussian Integers, Part 1
November 4, 2014
Gaussian integers are complex numbers of the form a + b i where both a and b are integers. They obey the normal laws of algebra for addition, subtraction and multiplication. Division works, too, but is a little bit complicated:
Addition: (a + b i) + (x + y i) = (a + x) + (b + y) i.
Subtraction: add the negative: (a + b i) − (x + y i) = (a − x) + (b − y) i.
Multiplication: cross-multiply, with i 2 = −1: (a + b i) × (x + y i) = (a x − b y) + (a y + b x) i.
Quotient: multiply by the conjugate of the divisor, then round: (a + b i) ÷ (x + y i) = ⌊(a x + b y) / n⌉ + ⌊(b x − a y) / n⌉ i, where n = x 2 + y 2.
Remainder: compute the quotient, then subtract quotient times divisor from dividend.
Your task is to write a small library of functions for operating on Gaussian integers. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.