# Factorize Gaussian integer (JavaScript)

## Program

## Description

This JavaScript program calculates the prime factorization of the given Gaussian integer. A Gaussian integer is a complex number of the form `a` + `b``i`, where `a` and `b` are integers. This program has a limit of |`a`|, |`b`| < 2^{26}. If the number is large, the program may hang for a few seconds.

The factorization is put into the following canonical form:

If the number is 0, 1, −1,

`i`, or −`i`, then the factorization is the number itself.Otherwise the factorization begins with a factor of −1 or

`i`or −`i`if necessary. The rest are primes listed in ascending order of absolute value, breaking ties by lowest argument (angle); each of these factors have absolute value > 1 and are in the first quadrant (0 ≤ argument < π/2).

The source code is available for viewing.

## Examples

- −1 = (−1) (unit)
- 2 = (−
`i`) (1 +`i`) (1 +`i`) - 4 +
`i`= (4 +`i`) (prime) - 7 = (7) (prime)
- 5 + 9
`i`= (1 +`i`) (7 + 2`i`)

## Input format

The real or imaginary component can be suppressed, and they can be in either order. The 1 can be suppressed for

`i`. For example, all these are valid inputs: -0, 5i, 1+2i, -i-2Spaces are allowed anywhere in the Gaussian integer, except within a string of digits.

To indicate negative numbers, the ASCII hyphen-minus (U+002D) and the minus sign (U+2212) are both acceptable. (The output properly uses the minus sign, though.)

`j`is an acceptable synonym for`i`, for those electrical engineers out there.

## List of Gaussian primes

List of all Gaussian primes up to norm 1000 (with symmetries removed): (1 + 1`i`), (2 + 1`i`), (3 + 0`i`), (3 + 2`i`), (4 + 1`i`), (5 + 2`i`), (6 + 1`i`), (5 + 4`i`), (7 + 0`i`), (7 + 2`i`), (6 + 5`i`), (8 + 3`i`), (8 + 5`i`), (9 + 4`i`), (10 + 1`i`), (10 + 3`i`), (8 + 7`i`), (11 + 0`i`), (11 + 4`i`), (10 + 7`i`), (11 + 6`i`), (13 + 2`i`), (10 + 9`i`), (12 + 7`i`), (14 + 1`i`), (15 + 2`i`), (13 + 8`i`), (15 + 4`i`), (16 + 1`i`), (13 + 10`i`), (14 + 9`i`), (16 + 5`i`), (17 + 2`i`), (13 + 12`i`), (14 + 11`i`), (16 + 9`i`), (18 + 5`i`), (17 + 8`i`), (19 + 0`i`), (18 + 7`i`), (17 + 10`i`), (19 + 6`i`), (20 + 1`i`), (20 + 3`i`), (15 + 14`i`), (17 + 12`i`), (20 + 7`i`), (21 + 4`i`), (19 + 10`i`), (22 + 5`i`), (20 + 11`i`), (23 + 0`i`), (21 + 10`i`), (19 + 14`i`), (20 + 13`i`), (24 + 1`i`), (23 + 8`i`), (24 + 5`i`), (18 + 17`i`), (19 + 16`i`), (25 + 4`i`), (22 + 13`i`), (25 + 6`i`), (23 + 12`i`), (26 + 1`i`), (26 + 5`i`), (22 + 15`i`), (27 + 2`i`), (26 + 9`i`), (20 + 19`i`), (25 + 12`i`), (22 + 17`i`), (26 + 11`i`), (28 + 5`i`), (25 + 14`i`), (27 + 10`i`), (23 + 18`i`), (29 + 4`i`), (29 + 6`i`), (25 + 16`i`), (23 + 20`i`), (24 + 19`i`), (29 + 10`i`), (28 + 13`i`), (31 + 0`i`), (31 + 4`i`), (31 + 6`i`).

This list is included for reference because there doesn’t seem to exist a similar list elsewhere on the web. All these primes `a` + `b``i` are in the first quadrant (i.e. `a` > 0 and `b` ≥ 0) and have `a` ≥ `b`. They are listed in order of ascending norm, then ascending argument. (“Norm” means squared absolute value.)

For each prime, a set of symmetrical primes have been excluded from the list: If `z` = `a` + `b``i` is prime, then so are −`z`, `i``z`, −`i``z`, `z` (conjugate), −`z`, `i``z`, −`i``z`, and `b` + `a``i` (equal to `i``z`). For example, 3 is prime, therefore so are −3, 3`i`, and −3`i`. For example, 5 + 2`i` is prime, therefore so are 5 − 2`i`, −5 + 2`i`, −5 − 2`i`, 2 + 5`i`, 2 − 5`i`, −2 + 5`i`, and −2 − 5`i`.

## More info

- Wikipedia: Table of Gaussian integer factorizations
- Stack Overflow: What’s a nice method to factor Gaussian integers?