TopCoder problem "ComplexIntegers" used in SRM 198 (Division II Level Three)



Problem Statement

    Warning: This problem statement makes extensive use of html superscripts and subscripts and will not be displayed properly with plugins. Use the applet window to read this problem statement.

A complex number can be represented by a+bi where a and b are real numbers and i is the square root of negative one (ie. i*i = -1), a so-called "imaginary" number. "a" is called the "real part" of the complex number, and "bi" is called the "imaginary part." Complex numbers where the real part and the imaginary part (ignoring the i) are both integers are called complex integers, or Gaussian integers. Two complex integers a+bi and c+di can be multiplied yielding a complex integer (ac-bd)+(ad+bc)i. Once we have complex integers and complex integer multiplication defined, one wonders if there are complex primes similar to the primes of the positive integers. Yes there are, but they are a little different.

Let us pause to define a few terms here.

  • A "prime number" is a positive integer greater than one that has no positive integer factors other than one and itself.
  • A "composite number" is a positive integer that has at least one positive integer factor other than one and itself.
  • The "norm" of the complex number z, where z=a+bi, is written |z|2 and is defined as a2+b2 (this is actually the L2 norm).
  • F and G are "complex integer factors" of z if F and G are complex integers and F * G = z (F may equal G).

Complex Integers can be divided into four categories.

  • Complex Zeros, 0, satisfy the equation z * 0 = 0 for all complex numbers z. There is one complex zero, (0+0i)
  • Complex Units, I, satisfy the equation |z * I|2 = |z|2 for all complex numbers z. There are four complex integers which are complex units (0+1i), (0-1i), (1+0i) and (-1+0i)
  • Complex Primes, P, have |P|2 > 1 and have no complex integer factor, F, such that |P|2 > |F|2 > 1
  • Complex Composites, C, have at least one complex integer factor, F, such that |C|2 > |F|2 > 1

After a bit of not particularly obvious algebraic manipulation we find the following somewhat easy-to-test rules:

  • For complex integers, z = a+bi
  • if |z|2 = 0, then z is a complex zero.
  • if |z|2 = 1, then z is a complex unit.
  • if |z|2 > 1 , and p is any prime number that also satisfies p % 4 = 3
    • if a (or b) is zero and b (or a) is equal to some p or -p, then z is a complex prime.
    • if a (or b) is zero and b (or a) is not equal to any p or -p, then z is a complex composite.
    • if a and b are both non-zero and |z|2 is a prime number then z is a complex prime.
    • if a and b are both non-zero and |z|2 is a composite number then z is a complex composite .

Given two int[]s, realPart and imaginaryPart, return a String[] where the k'th element of the answer is "zero", "unit", "prime", or "composite" corresponding to the complex integer (realPart[k]+imaginaryPart[k]i)

For example:

{ 0,     1,       0,     1,       -1,      2,           0,       0,          -3}
{ 0,     0,      -1,     1,        1,      0,          -3,       5,          -2}
returns
{"zero", "unit", "unit", "prime", "prime", "composite", "prime", "composite", "prime"}
Some factorizations of the composites are:
  • (2+0i) = (1+1i)*(1-1i)
  • (0+5i) = (1+2i)*(2+1i)
 

Definition

    
Class:ComplexIntegers
Method:classify
Parameters:int[], int[]
Returns:String[]
Method signature:String[] classify(int[] realPart, int[] imaginaryPart)
(be sure your method is public)
    
 

Constraints

-realPart will contain between 1 and 50 elements inclusive.
-imaginaryPart will contain the same number of elements as realPart
-each element of realPart will be between -10000 and 10000 inclusive.
-each element of imaginaryPart will be between -10000 and 10000 inclusive.
 

Examples

0)
    
{ 0,     1,       0,     1,       -1,      2,           0,       0,          -3}
{ 0,     0,      -1,     1,        1,      0,          -3,       5,          -2}
Returns: 
{ "zero",
 "unit",
 "unit",
 "prime",
 "prime",
 "composite",
 "prime",
 "composite",
 "prime" }
The example from above.
1)
    
{2,3,4,5,2,3,4,5,2,3,4,5,2,3,4,5}
{2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5}
Returns: 
{ "composite",
 "prime",
 "composite",
 "prime",
 "prime",
 "composite",
 "composite",
 "composite",
 "composite",
 "composite",
 "composite",
 "prime",
 "prime",
 "composite",
 "prime",
 "composite" }
2)
    
{0,0,0,0,0,0,0,0,0,-19,23,29,31,37,-41}
{15,2,-3,5,-7,11,-13,17,0,0,0,0,0,0,0}
Returns: 
{ "composite",
 "composite",
 "prime",
 "composite",
 "prime",
 "prime",
 "composite",
 "composite",
 "zero",
 "prime",
 "prime",
 "composite",
 "prime",
 "composite",
 "composite" }
3)
    
{99,-39,0,0,97,1003,9998,1119}
{0,0,35,-129,-2,-232,9997,1120}
Returns: 
{ "composite",
 "composite",
 "composite",
 "composite",
 "prime",
 "prime",
 "prime",
 "prime" }
4)
    
{0,  0,0,1,-1,   1,1,-1,-1,   2,-2,0,0,  1,1,2,2,-1,-1,-2,-2, 
     3,-3,0,0,   1,1,3,3,-1,-1,-3,-3,    2,2,-2,-2,  0,0,4,-4,
                 1,1,4,4,-1,-1,-4,-4 }
{0,  1,-1,0,0,   1,-1,1,-1,   0,0,2,-2,  2,-2,1,-1,2,-2,1,-1,  
     0,0,-3,3,   3,-3,1,-1,3,-3,1,-1,     2,-2,2,-2,  4,-4,0,0,
                 4,-4,1,-1,4,-4,1,-1 }
Returns: 
{ "zero",
 "unit",
 "unit",
 "unit",
 "unit",
 "prime",
 "prime",
 "prime",
 "prime",
 "composite",
 "composite",
 "composite",
 "composite",
 "prime",
 "prime",
 "prime",
 "prime",
 "prime",
 "prime",
 "prime",
 "prime",
 "prime",
 "prime",
 "prime",
 "prime",
 "composite",
 "composite",
 "composite",
 "composite",
 "composite",
 "composite",
 "composite",
 "composite",
 "composite",
 "composite",
 "composite",
 "composite",
 "composite",
 "composite",
 "composite",
 "composite",
 "prime",
 "prime",
 "prime",
 "prime",
 "prime",
 "prime",
 "prime",
 "prime" }

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=2439

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=5073&pm=2439

Writer:

Rustyoldman

Testers:

brett1479 , schveiguy

Problem categories:

Advanced Math