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) = (ax) + (by) i.

Multiplication: cross-multiply, with i2 = −1: (a + b i) × (x + y i) = (a xb 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 xa y) / ni, where n = x2 + y2.

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.


Pages: 1 2