A sealed trait representing natural numbers (non-negative integers). Natural numbers are either Zero or a Successor of another natural number. This trait provides methods for basic arithmetic operations including addition, subtraction, and multiplication.
Multiplies the current natural number by the given natural number.
Multiplies the current natural number by the given natural number.
This method performs multiplication using a tail-recursive helper function. It repeatedly adds the current number to an accumulator based on the value of the given n.
Value parameters
n
the natural number to multiply with the current number
Attributes
Returns
the result of the multiplication as a natural number
Subtracts the given natural number n from this natural number. The subtraction operation is defined for natural numbers (non-negative integers). If the result would be negative, the method returns None.
Subtracts the given natural number n from this natural number. The subtraction operation is defined for natural numbers (non-negative integers). If the result would be negative, the method returns None.
Value parameters
n
the natural number to subtract from this natural number
Attributes
Returns
an Option[Natural] containing the result of the subtraction if non-negative, otherwise None