Java Utililty Methods Long Number Mod

List of utility methods to do Long Number Mod

Description

The list of methods to do Long Number Mod are organized into topic(s).

Method

longmod(long a, long b)
mod
long mod = a % b;
return mod >= 0 ? mod : mod + b;
longmod(long pNum, long pDivisor)
mod
long divisor = Math.abs(pDivisor);
return (pNum % divisor + divisor) % divisor;
longmod(long v, long m)
Returns v (mod m).
The value returned lies in range [ 0 ..
if (m == 0L)
    return NOT_FOUND;
v %= m;
if (v < 0L) {
    if (m < 0L)
        v -= m;
    else
        v += m;
...
longmod(long x, long y)
mod
return (x - y * floorDivide(x, y));
long[]modDivide(long a, long b, long m)
Modular division.
if (Integer.MIN_VALUE < m && m <= Integer.MAX_VALUE) {
    if (m == 0L)
        return new long[] { NOT_FOUND, 0L, 0L };
    int ret[] = modDivide((int) (a % m), (int) (b % m), (int) m);
    return new long[] { ret[0], ret[1], ret[2] };
a %= m;
long x = 0L;
...
intmodifiedCalcElementOffset(long index, long mask)
This method assumes index is actually (index << 1) because lower bit is used for resize hence the >> 1
return (int) (index & mask) >> 1;
longmodifyValue(long value, char op, long modifier, long max, long min)
Modify the value, return = op(value, modifier).
long realValue = value;
if (realValue > max) {
    realValue = max;
if (realValue < min) {
    realValue = min;
switch (op) {
...
longmods(long v, long m)
Signed mod.
The value returned lies in range [ -( |m| - 1 ) / 2 ..
if (m <= 0L) {
    if (m == 0L)
        return NOT_FOUND;
    if (m == Long.MIN_VALUE) {
        if (v < -4611686018427387903L || v > 4611686018427387904L)
            v += m;
        return v;
    m = -m;
v %= m;
if (v > m >> 1)
    v -= m;
else if (v < -((m - 1L) >> 1))
    v += m;
return v;
longmodularReciprocal(long a, long mod)
Computes the reciprocal or multiplicative inverse of a (modulo mod).
final long origA = a;
final long origMod = mod;
long x = 0;
long y = 1;
long lastx = 1;
long lasty = 0;
while (mod != 0) {
    long temp = mod;
...
longmodulo(final long x, final long y)
Modulo, with correction for negative dividends.
return ((x % y) + y) % y;