static byte[] buildDH(DHPublicKey key) { DataByteOutputStream out = new DataByteOutputStream(); BigInteger p = key.getParams().getP(); BigInteger g = key.getParams().getG(); BigInteger y = key.getY(); int pLength, gLength, yLength; if (g.equals(TWO) && (p.equals(DHPRIME768) || p.equals(DHPRIME1024))) { pLength = 1; gLength = 0; } else { pLength = BigIntegerLength(p); gLength = BigIntegerLength(g); } yLength = BigIntegerLength(y); out.writeShort(pLength); if (pLength == 1) { if (p.bitLength() == 768) out.writeByte((byte) 1); else out.writeByte((byte) 2); } else out.writeBigInteger(p); out.writeShort(gLength); if (gLength > 0) out.writeBigInteger(g); out.writeShort(yLength); out.writeBigInteger(y); return out.toByteArray(); }
public static BigInteger factorial(long n) { BigInteger one = new BigInteger("1"); BigInteger zero = new BigInteger("0"); BigInteger m = new BigInteger((n - 1) + ""); BigInteger result = new BigInteger(n + ""); if (result.equals(one) || result.equals(zero)) { return one; } else { return result.multiply(factorial(n - 1)); } }
public static boolean check(long n, int p) { BigInteger P = BigInteger.TEN.pow(p), Q = BigInteger.TEN.pow(p - 1); BigInteger N = BigInteger.ONE, tmp = BigInteger.valueOf(2); while (n > 0) { if ((n % 2) == 1) { N = N.multiply(tmp); N = N.mod(P); } tmp = tmp.multiply(tmp); tmp = tmp.mod(P); n >>= 1; } N = N.divide(Q); if (N.equals(BigInteger.ONE) || N.equals(BigInteger.valueOf(2))) return true; else return false; }
public static List range(BigInteger start, BigInteger end) { List l = new List(); long st = start.longValue(); long en = start.longValue(); if (BigInteger.valueOf(st).equals(start) && BigInteger.valueOf(en).equals(end)) { int dir = st < en ? 1 : -1; while (st != en) { l.add(BigInteger.valueOf(st)); st = st + dir; } } else { BigInteger dir; if (start.compareTo(end) < 0) { dir = BigInteger.ONE; } else { dir = BigInteger.valueOf(-1); } while (!start.equals(end)) { l.add(start); start = start.add(dir); } } return l; }
void solve(int caseNum) { int n = in.nextInt(); BigInteger[] past = new BigInteger[n], d = new BigInteger[n]; for (int i = 0; i < n; i++) past[i] = new BigInteger(in.next()); Arrays.sort(past); for (int i = 0; i < n; i++) d[i] = past[i].subtract(past[0]); for (int i = 2; i < n; i++) d[i] = d[i].gcd(d[i - 1]); BigInteger gcd = d[n - 1], mod = past[0].mod(gcd); System.out.printf("Case #%d: ", caseNum); if (mod.equals(BigInteger.ZERO)) System.out.println(0); else System.out.println(gcd.subtract(mod)); }
public static void process() { Arrays.sort(bookHeight); BigInteger sum = BigInteger.valueOf(0L); BigInteger temp; int iCount = 0; for (int i = bookHeight.length - 1; i >= 0; i--) { sum = sum.add(BigInteger.valueOf((long) (bookHeight[i]))); temp = sum.max(b); iCount++; if (temp.equals(sum)) break; } System.out.print(iCount); }
/** * @return whether a BigDecimal is a valid Farrago decimal. If a BigDecimal's unscaled value * overflows a long, then it is not a valid Farrago decimal. */ public static boolean isValidDecimal(BigDecimal bd) { BigInteger usv = bd.unscaledValue(); long usvl = usv.longValue(); return usv.equals(BigInteger.valueOf(usvl)); }