Java examples for java.math:BigInteger
BigInteger Jacobi
/*// w w w . ja va2 s . c o m * Copyright 2013 Valentyn Kolesnikov * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.math.BigDecimal; import java.math.BigInteger; import java.math.MathContext; import java.math.RoundingMode; public class Main{ public static final BigInteger TWO = new BigInteger("2"); public static final BigInteger FOUR = TWO.add(TWO); public static final BigInteger SEVEN = FOUR.add(THREE); public static final BigInteger EIGHT = FOUR.add(FOUR); static int Jacobi(BigInteger m, BigInteger n) { if (m.compareTo(n) >= 0) { m = m.mod(n); return Jacobi(m, n); } if (n.equals(BigInteger.ONE) || m.equals(BigInteger.ONE)) { return 1; } if (m.equals(BigInteger.ZERO)) { return 0; } int twoCount = 0; while (m.mod(TWO) == BigInteger.ZERO) { twoCount++; m = m.divide(TWO); } int J2n = n.mod(EIGHT).equals(BigInteger.ONE) || n.mod(EIGHT).equals(SEVEN) ? 1 : -1; int rule8multiplier = (twoCount % 2 == 0) ? 1 : J2n; int tmp = Jacobi(n, m); int rule6multiplier = n.mod(FOUR).equals(BigInteger.ONE) || m.mod(FOUR).equals(BigInteger.ONE) ? 1 : -1; return tmp * rule6multiplier * rule8multiplier; } }