Ad

The base, or radix, is the number of unique digits, including the digit zero, used to represent numbers.

from https://en.wikipedia.org/wiki/Radix

Normally, this is the digits 0-9 and this is conventionally followed by letters (a -> 10, up to z -> 36). This is the system employed for this kumite; however other symbols may be used.

Numpy offers a base_repr function, but this is not part of basic python. As numpy is an external module, it may not always be available. The aim is to produce a function in native python that gives equivalent functioinality. A further challenge is to avoid use of the math module (or the bin, hex, oct functions for that matter).

The function to_base is intended to provide the same functionality. The string uses uppercase, because numpy.base_repr does the same.

The function to_base may be tested by:

  • Comparing its output to that of numpy.base_repr
  • Converting the output of to_base back to the original number using Python's native int() function.
def to_base (n, base):
    """
    A simple function to replicate the functionality of numpy's base_repr
    using native python. Returns upper case.
    """
    assert base >= 2, "Minimum base 2"
    assert base <= 36, "Maximum base 36"
    assert type(base) == int, "Base must be an integer"
    
    chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"

    digits = []
    while n:
        digits.append(chars[n%base])
        n //= base

    digits.reverse()

    return ''.join(digits)

Some info about Armstrong numbers

from https://oeis.org/A005188
Armstrong (or pluperfect, or Plus Perfect, or narcissistic) numbers:
m-digit positive numbers equal to sum of the m-th powers of their digits.

===

For purposes of this kumite (at least this iteration), base 10 will be used.
Other bases may be available. Oh wait, they are.

===

Narcissistic number
From Wikipedia, the free encyclopedia
(Redirected from Armstrong number)
https://en.wikipedia.org/wiki/Narcissistic_number

In number theory, a narcissistic number (also known as a pluperfect
digital invariant (PPDI), an Armstrong number (after Michael F. Armstrong)
or a plus perfect number)[6] in a given number base b is a number that is
the sum of its own digits each raised to the power of the number of digits.

#This works for base-10 only. Other bases might require using text-type objects.
def is_Armstrong(n):

    m = n # Make a copy of n for use obtaining digits.
    #Bearing in mind that n must be kept available.

    digits = [] #Store digits here

    #Get digits
    while (m):
        digits.append(m%10)
        m //= 10

    #Get count of digits
    x = len(digits)

    #Compare original value to sum of digit ^ count
    return n == sum(d ** x for d in digits)