当前位置: 首页>>代码示例>>Java>>正文


Java BigInteger.divideAndRemainder方法代码示例

本文整理汇总了Java中java.math.BigInteger.divideAndRemainder方法的典型用法代码示例。如果您正苦于以下问题:Java BigInteger.divideAndRemainder方法的具体用法?Java BigInteger.divideAndRemainder怎么用?Java BigInteger.divideAndRemainder使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.math.BigInteger的用法示例。


在下文中一共展示了BigInteger.divideAndRemainder方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: extgcd

import java.math.BigInteger; //导入方法依赖的package包/类
/**
 * Extended euclidian algorithm (computes gcd and representation).
 *
 * @param a - the first integer
 * @param b - the second integer
 * @return <tt>(d,u,v)</tt>, where <tt>d = gcd(a,b) = ua + vb</tt>
 */
public static BigInteger[] extgcd(BigInteger a, BigInteger b)
{
    BigInteger u = ONE;
    BigInteger v = ZERO;
    BigInteger d = a;
    if (b.signum() != 0)
    {
        BigInteger v1 = ZERO;
        BigInteger v3 = b;
        while (v3.signum() != 0)
        {
            BigInteger[] tmp = d.divideAndRemainder(v3);
            BigInteger q = tmp[0];
            BigInteger t3 = tmp[1];
            BigInteger t1 = u.subtract(q.multiply(v1));
            u = v1;
            d = v3;
            v1 = t1;
            v3 = t3;
        }
        v = d.subtract(a.multiply(u)).divide(b);
    }
    return new BigInteger[]{d, u, v};
}
 
开发者ID:Appdome,项目名称:ipack,代码行数:32,代码来源:IntegerFunctions.java

示例2: writeSmallBig

import java.math.BigInteger; //导入方法依赖的package包/类
public ETFWriter writeSmallBig(BigInteger num) {
    if (num.equals(BigInteger.ZERO)) {
        writeToBuffer(SMALL_BIG_EXT, (byte) 0, (byte) 0);
    } else {
        byte signum = num.signum() == -1 ? (byte) 1 : (byte) 0;
        num = num.abs();
        int n = (int) Math.ceil(num.bitLength()/8)+1; //Equivalent to Math.ceil(log256(num)) + 1
        byte[] bytes = new byte[n];
        writeToBuffer(SMALL_BIG_EXT, (byte) (n & 0xFF), signum);
        n -= 1;
        while (n >= 0) {
            BigInteger[] res = num.divideAndRemainder(BigInteger.valueOf(256).pow(n));
            bytes[n] = res[0].byteValue(); //Quotient
            num = res[1]; //Remainder
            n--;
        }
        writeToBuffer(bytes);
    }
    return this;
}
 
开发者ID:austinv11,项目名称:ETF-Java,代码行数:21,代码来源:ETFWriter.java

示例3: toString

import java.math.BigInteger; //导入方法依赖的package包/类
/**
 * Algorithm 4.6: ToString
 *
 * @param x       the integer to convert
 * @param k       the required String size
 * @param upper_a the alphabet to be used
 * @return a string of length k, using alphabet A, and representing x
 */
public String toString(BigInteger x, int k, List<Character> upper_a) {
    Preconditions.checkArgument(x.signum() >= 0, "x should be a non-negative integer");
    int alphabetSize = upper_a.size();
    BigInteger N = BigInteger.valueOf(alphabetSize);
    Preconditions.checkArgument(N.pow(k).compareTo(x) >= 0,
            "x is too large to be encoded with k characters of alphabet upper_a");

    StringBuilder sb = new StringBuilder(k);
    BigInteger current = x;
    for (int i = 1; i <= k; i++) {
        BigInteger[] divideAndRemainder = current.divideAndRemainder(N);
        current = divideAndRemainder[0];
        // always insert before the previous character
        sb.insert(0, upper_a.get(divideAndRemainder[1].intValue()));
    }

    return sb.toString();
}
 
开发者ID:republique-et-canton-de-geneve,项目名称:chvote-protocol-poc,代码行数:27,代码来源:Conversion.java

示例4: main

import java.math.BigInteger; //导入方法依赖的package包/类
public static void main(String[] args) {
    try {
        BigInteger a = BigInteger.ONE.shiftLeft(2147483646);
        BigInteger b = BigInteger.ONE.shiftLeft(1568);
        BigInteger[] qr = a.divideAndRemainder(b);
        BigInteger q = qr[0];
        BigInteger r = qr[1];
        if (!r.equals(BigInteger.ZERO)) {
            throw new RuntimeException("Incorrect signum() of remainder " + r.signum());
        }
        if (q.bitLength() != 2147482079) {
            throw new RuntimeException("Incorrect bitLength() of quotient " + q.bitLength());
        }
        System.out.println("Division of large values passed without overflow.");
    } catch (OutOfMemoryError e) {
        // possible
        System.err.println("DivisionOverflow skipped: OutOfMemoryError");
        System.err.println("Run jtreg with -javaoption:-Xmx8g");
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:21,代码来源:DivisionOverflow.java

示例5: main

import java.math.BigInteger; //导入方法依赖的package包/类
public static void main(String[] args) {
    try {
        BigInteger a = BigInteger.ONE.shiftLeft(2147483646);
        BigInteger b = BigInteger.ONE.shiftLeft(1568);
        BigInteger[] qr = a.divideAndRemainder(b);
        BigInteger q = qr[0];
        BigInteger r = qr[1];
        if (!r.equals(BigInteger.ZERO))
            throw new RuntimeException("Incorrect singum() of remainder " + r.signum());
        if (q.bitLength() != 2147482079)
            throw new RuntimeException("Incorrect bitLength() of quotient " + q.bitLength());
        System.out.println("Division of large values passed without overflow.");
    } catch (OutOfMemoryError e) {
        // possible
        System.out.println("OutOfMemoryError");
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:18,代码来源:DivisionOverflow.java

示例6: calculate

import java.math.BigInteger; //导入方法依赖的package包/类
/**
 * Runs the EEA on two <code>BigInteger</code>s<br/>
 * Implemented from pseudocode on <a href="http://en.wikipedia.org/wiki/Extended_Euclidean_algorithm">Wikipedia</a>.
 *
 * @param a
 * @param b
 * @return a <code>BigIntEuclidean</code> object that contains the result in the variables <code>x</code>, <code>y</code>, and <code>gcd</code>
 */
public static BigIntEuclidean calculate(BigInteger a, BigInteger b)
{
    BigInteger x = BigInteger.ZERO;
    BigInteger lastx = BigInteger.ONE;
    BigInteger y = BigInteger.ONE;
    BigInteger lasty = BigInteger.ZERO;
    while (!b.equals(BigInteger.ZERO))
    {
        BigInteger[] quotientAndRemainder = a.divideAndRemainder(b);
        BigInteger quotient = quotientAndRemainder[0];

        BigInteger temp = a;
        a = b;
        b = quotientAndRemainder[1];

        temp = x;
        x = lastx.subtract(quotient.multiply(x));
        lastx = temp;

        temp = y;
        y = lasty.subtract(quotient.multiply(y));
        lasty = temp;
    }

    BigIntEuclidean result = new BigIntEuclidean();
    result.x = lastx;
    result.y = lasty;
    result.gcd = a;
    return result;
}
 
开发者ID:Appdome,项目名称:ipack,代码行数:39,代码来源:BigIntEuclidean.java

示例7: encode

import java.math.BigInteger; //导入方法依赖的package包/类
/**
 * Encodes a number using Base62 encoding.
 *
 * @param number a positive integer
 * @return a Base62 string
 *
 * @throws IllegalArgumentException if <code>number</code> is a negative integer
 */
static String encode(BigInteger number) {
	if (number.compareTo(BigInteger.ZERO) < 0) {
		throwIllegalArgumentException("number must not be negative");
	}
	StringBuilder result = new StringBuilder();
	while (number.compareTo(BigInteger.ZERO) > 0) {
		BigInteger[] divmod = number.divideAndRemainder(BASE);
		number = divmod[0];
		int digit = divmod[1].intValue();
		result.insert(0, DIGITS.charAt(digit));
	}
	return (result.length() == 0) ? DIGITS.substring(0, 1) : result.toString();
}
 
开发者ID:Devskiller,项目名称:friendly-id,代码行数:22,代码来源:Base62.java

示例8: findDigits

import java.math.BigInteger; //导入方法依赖的package包/类
private short[] findDigits(final BigInteger i, final short[] current) {
  if (i.signum() != 0) {
    final BigInteger[] array = i.divideAndRemainder(BI_BASE);
    final short[] next = new short[current.length + 1];
    next[0] = (short) array[1].intValue();
    System.arraycopy(current, 0, next, 1, current.length);
    return findDigits(array[0], next);
  } else
    return current;
}
 
开发者ID:traneio,项目名称:ndbc,代码行数:11,代码来源:BigDecimalEncoding.java

示例9: base58Encode

import java.math.BigInteger; //导入方法依赖的package包/类
static String base58Encode(final byte[] bytes) {
    final int leadingZeros = countLeadingZeros(bytes);
    BigInteger acc = new BigInteger(1, bytes);
    StringBuilder builder = new StringBuilder();
    while (acc.compareTo(BigInteger.ZERO) > 0) {
        BigInteger[] quotientAndRemainder = acc.divideAndRemainder(FIFTY_EIGHT);
        acc = quotientAndRemainder[0];
        builder.append(REMAINDER_SET[quotientAndRemainder[1].intValue()]);
    }
    for (int i = leadingZeros; i > 0; i--) {
        builder.append(REMAINDER_SET[0]);
    }
    return builder.reverse().toString();
}
 
开发者ID:mgrand,项目名称:bigchaindb-java-driver,代码行数:15,代码来源:Base58.java

示例10: divideLarge

import java.math.BigInteger; //导入方法依赖的package包/类
/**
 * Sanity test for Burnikel-Ziegler division.  The Burnikel-Ziegler division
 * algorithm is used when each of the dividend and the divisor has at least
 * a specified number of ints in its representation.  This test is based on
 * the observation that if {@code w = u*pow(2,a)} and {@code z = v*pow(2,b)}
 * where {@code abs(u) > abs(v)} and {@code a > b && b > 0}, then if
 * {@code w/z = q1*z + r1} and {@code u/v = q2*v + r2}, then
 * {@code q1 = q2*pow(2,a-b)} and {@code r1 = r2*pow(2,b)}.  The test
 * ensures that {@code v} is just under the B-Z threshold, that {@code z} is
 * over the threshold and {@code w} is much larger than {@code z}. This
 * implies that {@code u/v} uses the standard division algorithm and
 * {@code w/z} uses the B-Z algorithm.  The results of the two algorithms
 * are then compared using the observation described in the foregoing and
 * if they are not equal a failure is logged.
 */
public static void divideLarge() {
    int failCount = 0;

    BigInteger base = BigInteger.ONE.shiftLeft(BITS_BURNIKEL_ZIEGLER + BITS_BURNIKEL_ZIEGLER_OFFSET - 33);
    for (int i=0; i<SIZE; i++) {
        BigInteger addend = new BigInteger(BITS_BURNIKEL_ZIEGLER + BITS_BURNIKEL_ZIEGLER_OFFSET - 34, random);
        BigInteger v = base.add(addend);

        BigInteger u = v.multiply(BigInteger.valueOf(2 + random.nextInt(Short.MAX_VALUE - 1)));

        if(random.nextBoolean()) {
            u = u.negate();
        }
        if(random.nextBoolean()) {
            v = v.negate();
        }

        int a = BITS_BURNIKEL_ZIEGLER_OFFSET + random.nextInt(16);
        int b = 1 + random.nextInt(16);
        BigInteger w = u.multiply(BigInteger.ONE.shiftLeft(a));
        BigInteger z = v.multiply(BigInteger.ONE.shiftLeft(b));

        BigInteger[] divideResult = u.divideAndRemainder(v);
        divideResult[0] = divideResult[0].multiply(BigInteger.ONE.shiftLeft(a - b));
        divideResult[1] = divideResult[1].multiply(BigInteger.ONE.shiftLeft(b));
        BigInteger[] bzResult = w.divideAndRemainder(z);

        if (divideResult[0].compareTo(bzResult[0]) != 0 ||
                divideResult[1].compareTo(bzResult[1]) != 0) {
            failCount++;
        }
    }

    report("divideLarge", failCount);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:51,代码来源:BigIntegerTest.java

示例11: toString

import java.math.BigInteger; //导入方法依赖的package包/类
private static String toString(BigInteger number) {
  StringBuilder builder = new StringBuilder();
  while (number.compareTo(BigInteger.ZERO) >= 1) {
    BigInteger[] mod = number.divideAndRemainder(BASE);
    builder.append(ALPHABET[mod[1].intValue()]);
    number = mod[0];
  }
  return builder.toString();
}
 
开发者ID:Nexmo,项目名称:comms-router,代码行数:10,代码来源:Uuid.java

示例12: testQuoRemIteration

import java.math.BigInteger; //导入方法依赖的package包/类
private static void testQuoRemIteration(FDBigInteger t, FDBigInteger s) throws Exception {
    BigInteger bt = t.toBigInteger();
    BigInteger bs = s.toBigInteger();
    int q = t.quoRemIteration(s);
    BigInteger[] qr = bt.divideAndRemainder(bs);
    if (!BigInteger.valueOf(q).equals(qr[0])) {
        throw new Exception("quoRemIteration returns incorrect quo");
    }
    check(qr[1].multiply(BigInteger.TEN), t, "quoRemIteration returns incorrect rem");
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:11,代码来源:TestFDBigInteger.java

示例13: create

import java.math.BigInteger; //导入方法依赖的package包/类
/**
 * Creates an instance of {@code Duration} from a number of seconds.
 * 
 * @param seconds the number of seconds, up to scale 9, positive or negative
 * @return a {@code Duration}, not null
 * @throws ArithmeticException if numeric overflow occurs
 */
private static Duration create(BigDecimal seconds) {

  BigInteger nanos = seconds.movePointRight(9).toBigIntegerExact();
  BigInteger[] divRem = nanos.divideAndRemainder(BI_NANOS_PER_SECOND);
  if (divRem[0].bitLength() > 63) {
    throw new ArithmeticException("Exceeds capacity of Duration: " + nanos);
  }
  return ofSeconds(divRem[0].longValue(), divRem[1].intValue());
}
 
开发者ID:kiegroup,项目名称:optashift-employee-rostering,代码行数:17,代码来源:Duration.java

示例14: shift

import java.math.BigInteger; //导入方法依赖的package包/类
public static void shift(int order) {
    int failCount1 = 0;
    int failCount2 = 0;
    int failCount3 = 0;

    for (int i=0; i<100; i++) {
        BigInteger x = fetchNumber(order);
        int n = Math.abs(random.nextInt()%200);

        if (!x.shiftLeft(n).equals
            (x.multiply(BigInteger.valueOf(2L).pow(n))))
            failCount1++;

        BigInteger y[] =x.divideAndRemainder(BigInteger.valueOf(2L).pow(n));
        BigInteger z = (x.signum()<0 && y[1].signum()!=0
                        ? y[0].subtract(BigInteger.ONE)
                        : y[0]);

        BigInteger b = x.shiftRight(n);

        if (!b.equals(z)) {
            System.err.println("Input is "+x.toString(2));
            System.err.println("shift is "+n);

            System.err.println("Divided "+z.toString(2));
            System.err.println("Shifted is "+b.toString(2));
            if (b.toString().equals(z.toString()))
                System.err.println("Houston, we have a problem.");
            failCount2++;
        }

        if (!x.shiftLeft(n).shiftRight(n).equals(x))
            failCount3++;
    }
    report("baz shiftLeft for " + order + " bits", failCount1);
    report("baz shiftRight for " + order + " bits", failCount2);
    report("baz shiftLeft/Right for " + order + " bits", failCount3);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:39,代码来源:BigIntegerTest.java

示例15: create

import java.math.BigInteger; //导入方法依赖的package包/类
/**
 * Creates an instance of {@code Duration} from a number of seconds.
 *
 * @param seconds  the number of seconds, up to scale 9, positive or negative
 * @return a {@code Duration}, not null
 * @throws ArithmeticException if numeric overflow occurs
 */
private static Duration create(BigDecimal seconds) {
    BigInteger nanos = seconds.movePointRight(9).toBigIntegerExact();
    BigInteger[] divRem = nanos.divideAndRemainder(BI_NANOS_PER_SECOND);
    if (divRem[0].bitLength() > 63) {
        throw new ArithmeticException("Exceeds capacity of Duration: " + nanos);
    }
    return ofSeconds(divRem[0].longValue(), divRem[1].intValue());
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:16,代码来源:Duration.java


注:本文中的java.math.BigInteger.divideAndRemainder方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。