Java examples for java.math:BigInteger
BigInteger inverse Mod
/*// ww w. j a v a 2 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. */ //package com.java2s; import java.math.BigInteger; public class Main { static BigInteger inverseMod(BigInteger a, BigInteger b) { BigInteger b0 = b, t, q; BigInteger x0 = BigInteger.ZERO, x1 = BigInteger.ONE; if (b.equals(BigInteger.ONE)) return BigInteger.ONE; while (a.subtract(BigInteger.ONE).signum() > 0) { q = a.divide(b); t = b; b = a.mod(b); a = t; t = x0; x0 = x1.subtract(q.multiply(x0)); x1 = t; } if (x1.signum() < 0) x1 = x1.add(b0); return x1; } }