Class Natural
java.lang.Object
java.lang.Number
org.episteme.core.mathematics.numbers.integers.Natural
- All Implemented Interfaces:
Serializable, Comparable<Natural>, AbelianMonoid<Natural>, Magma<Natural>, Monoid<Natural>, Semiring<Natural>, Set<Natural>
Abstract base class for natural numbers (â„• = {0, 1, 2, ...}).
Natural numbers form a semiring under addition and multiplication. This class provides a smart factory that automatically chooses the most efficient backing implementation:
- Since:
- 1.0
- Author:
- Silvere Martin-Michiellot, Gemini AI (Google DeepMind)
- See Also:
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionabstract NaturalAdds another natural number.abstract BigIntegerConverts this natural number to a BigInteger.abstract intabstract NaturalDivides by another natural number (integer division).abstract booleanabstract inthashCode()abstract booleanisOne()Returns true if this natural number is one.abstract booleanisZero()Returns true if this natural number is zero.abstract longConverts this natural number to a long.abstract NaturalComputes this modulo another natural number.abstract NaturalMultiplies by another natural number.static Naturalof(int value) Creates a natural number from an int value.static Naturalof(long value) Creates a natural number from a long value.static Naturalof(BigInteger value) Creates a natural number from a BigInteger.abstract NaturalSubtracts another natural number.Alias forbigIntegerValue()for compatibility.abstract StringtoString()Methods inherited from class Number
byteValue, doubleValue, floatValue, intValue, shortValueMethods inherited from interface AbelianMonoid
add, identity, isCommutative, zeroMethods inherited from interface Monoid
isAssociativeMethods inherited from interface Semiring
isMultiplicationCommutative, multiply, one, powMethods inherited from interface Set
contains, description, isEmpty
-
Field Details
-
ZERO
The natural number 0 -
ONE
The natural number 1
-
-
Method Details
-
of
Creates a natural number from an int value.- Parameters:
value- the value (must be non-negative)- Returns:
- the Natural instance
- Throws:
IllegalArgumentException- if value is negative
-
of
Creates a natural number from a long value. Automatically selects optimal backing implementation.- Parameters:
value- the value (must be non-negative)- Returns:
- the Natural instance
- Throws:
IllegalArgumentException- if value is negative
-
of
Creates a natural number from a BigInteger. Automatically selects optimal backing implementation.- Parameters:
value- the value (must be non-negative)- Returns:
- the Natural instance
- Throws:
IllegalArgumentException- if value is negative or null
-
add
-
subtract
Subtracts another natural number.- Parameters:
other- the subtrahend- Returns:
- this - other
- Throws:
ArithmeticException- if the result would be negative
-
multiply
-
divide
Divides by another natural number (integer division).- Parameters:
other- the divisor- Returns:
- this ÷ other (rounded down)
- Throws:
ArithmeticException- if other is zero
-
modulo
Computes this modulo another natural number.- Parameters:
other- the modulus- Returns:
- this mod other
- Throws:
ArithmeticException- if other is zero
-
isZero
public abstract boolean isZero()Returns true if this natural number is zero. -
isOne
public abstract boolean isOne()Returns true if this natural number is one. -
longValue
public abstract long longValue()Converts this natural number to a long.- Specified by:
longValuein classNumber- Returns:
- the long value
- Throws:
ArithmeticException- if the value doesn't fit in a long
-
bigIntegerValue
Converts this natural number to a BigInteger.- Returns:
- the BigInteger value (never null)
-
toBigInteger
Alias forbigIntegerValue()for compatibility.- Returns:
- the BigInteger value (never null)
-
equals
-
hashCode
-
toString
-
compareTo
- Specified by:
compareToin interfaceComparable<Natural>
-