lib.digital Module

A collection of various functions concerning properties of the digital representation of integers. Where sensible, these functions may consider an integer in various bases such as binary (\(2\)), decimal (\(10\)) or hexadecimal (\(16\)).

lib.digital – A collection of digital functions

lib.digital.digit_sum(n, base=10)

Compute the digit-sum of \(n\) in the given base (the default is decimal)

Let \(b\) be base. The digit-sum of \(n\) is the summation of each digit in \(n\) when it is interpreted in base \(b\).

That is, consider \(n=d_l \times b^l + \dots + d_1 \times b^1 + d_0 \times b^0\) for base \(b\). Then, \(\mbox{digit_sum}(n,b) = \Sigma_{i=0}^l d_i\)

Parameters:
  • n (int) – digit-sum input
  • base (int) – base to interpret \(n\) in
Return type:

int

Returns:

the digit-sum of \(n\) in the given base

Raises:
  • TypeError – if \(n\) or base are not int variables
  • ValueError – if \(n\) is negative
  • ValueError – if base is not positive
>>> digit_sum(123)
6  # 1 + 2 + 3 = 6
>>> digit_sum(7, base=2)
3  # 1 + 1 + 1 = 3
lib.digital.digits_of(n, base=10)

Construct the list of digits in \(n\) for the given base

Note

the digits are returned in big-endian order.

Parameters:
  • n (int) – input number
  • base (int) – the base
Return type:

List[int]

Returns:

a list of the digits of \(n\) in the given base

Raises:
  • TypeError – if \(n\) or base are not int variables
  • ValueError – if \(n\) is negative
  • ValueError – if base is not positive
>>> digits_of(1078)
[1, 0, 7, 8]
>>> digits_of(10, base=2)
[1, 0, 1, 0]
lib.digital.digits_to_num(digits, base=10)

Convert the given sequence of digits into the integer they represent

Parameters:
  • digits (List[int]) – the sequence of digits
  • base (int) –
Return type:

int

Returns:

the integer they represent

Raises:
  • TypeError – if base is not an int variable
  • TypeError – if digits is not a list of int variables
  • ValueError – if base is not positive
  • ValueError – if the elements of the list of digits aren’t valid for the given base
lib.digital.is_palindrome(n, base=10)

Determine whether the integer \(n\) is a palindrome when represented in the given base (default decimal)

This function iteratively removes the first and last digits from \(n\) if they are equivalent, when represented in the given base. If \(n\) is a palindrome, this procedure will leave only one digit (if \(n\) originally had an odd number of digits) or no digits (if \(n\) originally had an even number of digits). Any other number of remaining digits implies that n is not a palindrome.

Parameters:
  • n (int) – number to test for being a palindrome
  • base (int) – consider \(n\) in the given base
Return type:

bool

Returns:

whether \(n\) is a palindrome or not

Raises:
  • TypeError – if \(n\) or base are not int variables
  • ValueError – if \(n\) is negative
  • ValueError – if base is not positive
  • ValueError – if base is not supported (i.e. not one of 2, 8, 10, 16)
>>> is_palindrome(121)
True
>>> is_palindrome(123)
False
>>> is_palindrome(5, base=2)
True
lib.digital.is_pandigital(n, d, lower=1, base=10)

Determine whether \(n\) is \(d\)-pandigital (the default base is decimal)

A \(d\)-pandigital number \(n\) contains the digits \(1\) through \(d\) precisely once.

Parameters:
  • n (Union[int, List[int]]) – number(s) to test for \(d\)-pandigital-ness
  • d (int) – parameter in \(d\)-pandigital
  • lower (int) – re-define pandigital to contain the digits \(\mbox{lower},\dots,d\)
  • base (int) – consider \(n\) in the given \(base\)
Return type:

bool

Returns:

whether \(n\) is \(d\)-pandigital or not

Raises:
  • TypeError – if \(n\) is not an int variable, or a list of int variables
  • TypeError – if \(d\), lower or base are not int variables
  • ValueError – if \(n\) is negative, or has negative elements
  • ValueError – if \(d\) is not positive
  • ValueError – if lower is negative
  • ValueError – if base is not positive

Note

n may be an integer, or a list of integers. If n is a list of integers, this function returns True if all digits in the elements of the list constitute a d-pandigital number.

>>> is_pandigital(123, 3)
True
>>> is_pandigital(200, 3)
False
>>> is_pandigital(123, 4)
False
>>> is_pandigital([1, 25, 43], 5)
True
lib.digital.num_digits(n, base=10)

Compute the number of digits in \(n\) in the given \(base\) (the default is decimal)

Parameters:
  • n (int) – number to count digits from
  • base (int) – base to interpret \(n\) in
Return type:

int

Returns:

the number of digits in \(n\) in the given base

Raises:
  • TypeError – if \(n\) or base are not int variables
  • ValueError – if \(n\) is negative
  • ValueError – if base is not positive
>>> num_digits(123)
3  # ||123|| = 3
>>> num_digits(7, base=2)
3  # ||111|| = 3