%mathpiper,def="FactorGaussianInteger"

// Algorithm adapted from: Number Theory: A Programmer's Guide
//			Mark Herkommer
// Program 8.7.1c, p 264
// This function needs to be modified to return the factors in
// data structure instead of printing them out

// THIS FUNCTION IS DEPRECATED NOW!
// Use GaussianFactors instead (Pablo)
// I've leave this here so that you can compare the eficiency of one
// function against the other

Function("FactorGaussianInteger",{x}) [
	Check( IsGaussianInteger(x), "Argument", "FactorGaussianInteger: argument must be a Gaussian integer");
	Local(re,im,norm,a,b,d,i,j);

	re:=Re(x);im:=Im(x);

	If(re<0, re:=(-re) );
	If(im<0, im:=(-im) );
	norm:=re^2+im^2;

	if( IsComposite(norm) )[
		For(i:=0, i^2 <= norm, i++ )[	// real part
			For(j:=0, i^2 + j^2 <= norm, j++)[	// complex part
				if( Not( (i = re And j = im) Or
					 (i = im And j = re) ) )[ // no associates
					d:=i^2+j^2;
					if( d > 1 )[
						a := re * i + im * j;
						b := im * i - re * j;
						While( (Modulo(a,d) = 0) And  (Modulo(b,d) = 0) ) [
							FactorGaussianInteger(Complex(i,j));
							re:= a/d;
							im:= b/d;
							a := re * i + im * j;
							b := im * i - re * j;
							norm := re^2 + im^2;
						];
					];
				];
			];
		];
		If( re != 1 Or im != 0, Echo(Complex(re,im)) );
	] else [
		Echo(Complex(re,im));
	];
];

%/mathpiper