Table of Contents

Class NumberTheory

Namespace
MathAssertions
Assembly
MathAssertions.dll

Exact integer-arithmetic predicates over long: divisibility, primality, greatest common divisor and least common multiple, coprimality, power-of-base check, perfect square, modular congruence. All checks are exact (no floating-point tolerance); the methods throw ArgumentOutOfRangeException for the inputs where the underlying mathematical operation is undefined (zero divisor, base <= 1, modulus <= 0).

public static class NumberTheory
Inheritance
NumberTheory
Inherited Members

Remarks

0.1.0 Cluster 6. Sits alongside the floating-point clusters in the same package; the MathAssertions.TUnit adapter delegates to these helpers from its [GenerateAssertion] extensions.

Methods

AreCoprime(long, long)

Returns true when a and b are coprime, that is gcd(a, b) == 1.

public static bool AreCoprime(long a, long b)

Parameters

a long

First value.

b long

Second value.

Returns

bool

true if the two values share no common divisor other than 1.

Exceptions

ArgumentOutOfRangeException

Either argument is MinValue.

GreatestCommonDivisor(long, long)

Returns the greatest common divisor of a and b computed by the Euclidean algorithm. The result is non-negative. gcd(0, 0) == 0 by convention; gcd(0, b) == |b| for any b.

public static long GreatestCommonDivisor(long a, long b)

Parameters

a long

First value.

b long

Second value.

Returns

long

The greatest common divisor.

Remarks

MinValue is rejected: its absolute value does not fit in long, so the standard Euclidean implementation produces a wrong-sign result for some input pairs. Callers needing GCD across the full long range should widen to BigInteger.

Exceptions

ArgumentOutOfRangeException

Either argument is MinValue.

IsCongruent(long, long, long)

Returns true when a and b are congruent modulo modulus: a ≡ b (mod modulus).

public static bool IsCongruent(long a, long b, long modulus)

Parameters

a long

First value.

b long

Second value.

modulus long

Modulus. Must be positive.

Returns

bool

true if the two values share the same residue modulo modulus.

Remarks

Implemented by comparing the canonical non-negative residues of a and b rather than by checking (a - b) % modulus == 0: the subtraction would overflow long for inputs straddling the signed range (for example MaxValue and a negative value). Working through the residues keeps every intermediate within the input range.

Exceptions

ArgumentOutOfRangeException

modulus is less than or equal to zero.

IsDivisibleBy(long, long)

Returns true when value is exactly divisible by divisor.

public static bool IsDivisibleBy(long value, long divisor)

Parameters

value long

Dividend.

divisor long

Divisor. Must not be zero.

Returns

bool

true if value % divisor == 0.

Exceptions

ArgumentOutOfRangeException

divisor is zero.

IsPerfectSquare(long)

Returns true when value is a non-negative perfect square. 0 and 1 are perfect squares; negative values are not.

public static bool IsPerfectSquare(long value)

Parameters

value long

Value to test.

Returns

bool

true if value is a non-negative perfect square.

Remarks

Uses Sqrt(double) truncated to long to obtain a candidate, then checks both the candidate and its successor against value to accommodate the rounding error Math.Sqrt can introduce for very large inputs. The successor check is skipped when squaring would overflow long; in that regime the candidate is the only admissible answer.

IsPowerOf(long, long)

Returns true when value is a non-negative integer power of baseValue; that is, when there exists a non-negative integer k such that baseValue^k == value. By convention baseValue^0 == 1, so IsPowerOf(long, long) returns true for value == 1 and any valid base.

public static bool IsPowerOf(long value, long baseValue)

Parameters

value long

Value to test.

baseValue long

Base. Must be greater than 1; 0, 1, and negative bases are rejected because the resulting power sequence is degenerate or non-monotonic.

Returns

bool

true if value is a power of baseValue.

Exceptions

ArgumentOutOfRangeException

baseValue is less than or equal to 1.

IsPrime(long)

Returns true when value is prime. Values less than two return false; 2 and 3 are special-cased to true; otherwise the wheel-of-six trial-division loop tests candidates of the form 6k +/- 1.

public static bool IsPrime(long value)

Parameters

value long

Value to test.

Returns

bool

true if value is prime.

Remarks

The loop bound is written i <= value / i rather than i * i <= value so the candidate-square computation never overflows when value approaches MaxValue. Algebraically equivalent for the positive values reached here.

LeastCommonMultiple(long, long)

Returns the least common multiple of a and b. lcm(0, b) == 0 for any b by convention. The result is non-negative.

public static long LeastCommonMultiple(long a, long b)

Parameters

a long

First value.

b long

Second value.

Returns

long

The least common multiple.

Remarks

Computed via |a / gcd(a, b) * b|, dividing first to keep the intermediate as small as possible. The final multiplication runs in a checked context; when the LCM does not fit in long (large coprime inputs near the signed range, for example lcm(long.MaxValue, 2)) an OverflowException is thrown rather than the wrapped negative result the unchecked computation would silently produce. Callers needing LCM across the full long range should widen to BigInteger.

MinValue is rejected at entry for the same reason GreatestCommonDivisor(long, long) rejects it. The check happens before the lcm(0, b) == 0 fast-path so the contract holds even when one argument is zero and the other is MinValue.

Exceptions

ArgumentOutOfRangeException

Either argument is MinValue.

OverflowException

The least common multiple does not fit in long.