package de.viathinksoft.immortal.gen2.math;
import java.math.BigInteger;
public class MathUtils2 {
// TODO: größer als MAX_INTEGER erlauben!
if (z.signum() == -1) {
throw new Exception("Integer overflow! (BigInteger-Length)");
}
return z;
}
if (exponent.signum() == -1) {
}
while (i.compareTo(exponent) != 0) {
res = res.multiply(base);
}
return res;
}
/**
* Division with remainder method - based on the original Java<br>
* 'divideAndRemainder' method<br>
* returns an object of results (of type BigInteger):<br>
* -> the division result (q=a/b)<br>
* result[1] -> the remainder (r=a-q*b)<br>
* If b==0 then result will be 0. No ArithmeticException!
*
* @param a
* @param b
* @return
* @see http://tupac.euv-frankfurt-o.de/www/kryptos/demos/Demos.java
**/
// if (b==0) {
// -> divideAndRemainder() throws ArithmeticException when b==0
if (b.signum() == 0) {
return new DivisionAndRemainderResult
(BigInteger.
ZERO,
}
return new DivisionAndRemainderResult(x[0], x[1]);
}
/**
*
* @param a
* @param b
* @return
* @see http://www.arndt-bruenner.de/mathe/scripts/chineRestsatz.js
*/
if (b.signum() == 0) {
}
if (rem.signum() == 0) {
}
return new BigInteger[] { y, x.
subtract(y.
multiply(a.
divide(b
))) };
}
/**
* Erweiterter euklidscher Algorithmus
*
* @param a
* @param b
* @return Erzeugt Objekt mit mit gcd=ggT(a,b) und gcd=alpha*a+beta*b
* @see http://www.arndt-bruenner.de/mathe/scripts/chineRestsatz.js
*/
if (a.signum() == -1) {
a = a.negate();
}
if (b.signum() == -1) {
b = b.negate();
}
return new ExtEuclResult(c[0].multiply(aa), c[1].multiply(bb), g);
}
/**
* Allgemeines Lösen zweier Kongruenzen (Chinesischer Restsatz)
*
* @param a
* @param n
* @param b
* @param m
* @return
* @throws CRTNotSolveableException
* @throws RemainderNotSmallerThanModulusException
* @see http://de.wikipedia.org/wiki/Chinesischer_Restsatz#Direktes_L.C3.B6sen_von_simultanen_Kongruenzen_ganzer_Zahlen
*/
// Frage: Ist es notwendig, dass wir divRem() verwenden, das von a%0==0 ausgeht?
if (a.signum() == -1) {
a = a.negate();
}
if (b.signum() == -1) {
b = b.negate();
}
if (n.signum() == -1) {
n = n.negate();
}
if (m.signum() == -1) {
m = m.negate();
}
if ((a.compareTo(n) >= 0) || (b.compareTo(m) >= 0)) {
throw new RemainderNotSmallerThanModulusException();
}
// d = ggT(n,m)
// a === b (mod d) erfüllt?
if (!divRem(a, d).getRemainder().equals(divRem(b, d).getRemainder())) {
throw new CRTNotSolveableException();
}
// ggT(n,m) = yn + zm
// x = a - y*n*((a-b)/d) mod (n*m/d)
a.subtract(y.multiply(n).multiply(a.subtract(b).divide(d))), N)
.getRemainder();
x = a.subtract(y.multiply(n).multiply(a.subtract(b).divide(d)));
while (x.signum() == -1) {
x = x.add(N);
}
return x;
}
private MathUtils2() {
}
}