a harshad number or more specifically an nharshad number is a number that s divisible by the sum of its digits in some given base n reference https en wikipedia orgwikiharshadnumber convert a given positive decimal integer to base base where base ranges from 2 to 36 examples inttobase23 2 10111 inttobase58 5 213 inttobase167 16 a7 bases below 2 and beyond 36 will error inttobase98 1 traceback most recent call last valueerror base must be between 2 and 36 inclusive inttobase98 37 traceback most recent call last valueerror base must be between 2 and 36 inclusive calculate the sum of digit values in a positive integer converted to the given base where base ranges from 2 to 36 examples sumofdigits103 12 13 sumofdigits1275 4 30 sumofdigits6645 2 1001 bases below 2 and beyond 36 will error sumofdigits543 1 traceback most recent call last valueerror base must be between 2 and 36 inclusive sumofdigits543 37 traceback most recent call last valueerror base must be between 2 and 36 inclusive finds all harshad numbers smaller than num in base base where base ranges from 2 to 36 examples harshadnumbersinbase15 2 1 10 100 110 1000 1010 1100 harshadnumbersinbase12 34 1 2 3 4 5 6 7 8 9 a b harshadnumbersinbase12 4 1 2 3 10 12 20 21 bases below 2 and beyond 36 will error harshadnumbersinbase234 37 traceback most recent call last valueerror base must be between 2 and 36 inclusive harshadnumbersinbase234 1 traceback most recent call last valueerror base must be between 2 and 36 inclusive determines whether n in base base is a harshad number where base ranges from 2 to 36 examples isharshadnumberinbase18 10 true isharshadnumberinbase21 10 true isharshadnumberinbase21 5 false bases below 2 and beyond 36 will error isharshadnumberinbase45 37 traceback most recent call last valueerror base must be between 2 and 36 inclusive isharshadnumberinbase45 1 traceback most recent call last valueerror base must be between 2 and 36 inclusive convert a given positive decimal integer to base base where base ranges from 2 to 36 examples int_to_base 23 2 10111 int_to_base 58 5 213 int_to_base 167 16 a7 bases below 2 and beyond 36 will error int_to_base 98 1 traceback most recent call last valueerror base must be between 2 and 36 inclusive int_to_base 98 37 traceback most recent call last valueerror base must be between 2 and 36 inclusive calculate the sum of digit values in a positive integer converted to the given base where base ranges from 2 to 36 examples sum_of_digits 103 12 13 sum_of_digits 1275 4 30 sum_of_digits 6645 2 1001 bases below 2 and beyond 36 will error sum_of_digits 543 1 traceback most recent call last valueerror base must be between 2 and 36 inclusive sum_of_digits 543 37 traceback most recent call last valueerror base must be between 2 and 36 inclusive finds all harshad numbers smaller than num in base base where base ranges from 2 to 36 examples harshad_numbers_in_base 15 2 1 10 100 110 1000 1010 1100 harshad_numbers_in_base 12 34 1 2 3 4 5 6 7 8 9 a b harshad_numbers_in_base 12 4 1 2 3 10 12 20 21 bases below 2 and beyond 36 will error harshad_numbers_in_base 234 37 traceback most recent call last valueerror base must be between 2 and 36 inclusive harshad_numbers_in_base 234 1 traceback most recent call last valueerror base must be between 2 and 36 inclusive determines whether n in base base is a harshad number where base ranges from 2 to 36 examples is_harshad_number_in_base 18 10 true is_harshad_number_in_base 21 10 true is_harshad_number_in_base 21 5 false bases below 2 and beyond 36 will error is_harshad_number_in_base 45 37 traceback most recent call last valueerror base must be between 2 and 36 inclusive is_harshad_number_in_base 45 1 traceback most recent call last valueerror base must be between 2 and 36 inclusive
def int_to_base(number: int, base: int) -> str:
    

    if base < 2 or base > 36:
        raise ValueError("'base' must be between 2 and 36 inclusive")

    digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    result = ""

    if number < 0:
        raise ValueError("number must be a positive integer")

    while number > 0:
        number, remainder = divmod(number, base)
        result = digits[remainder] + result

    if result == "":
        result = "0"

    return result


def sum_of_digits(num: int, base: int) -> str:
    

    if base < 2 or base > 36:
        raise ValueError("'base' must be between 2 and 36 inclusive")

    num_str = int_to_base(num, base)
    res = sum(int(char, base) for char in num_str)
    res_str = int_to_base(res, base)
    return res_str


def harshad_numbers_in_base(limit: int, base: int) -> list[str]:
    

    if base < 2 or base > 36:
        raise ValueError("'base' must be between 2 and 36 inclusive")

    if limit < 0:
        return []

    numbers = [
        int_to_base(i, base)
        for i in range(1, limit)
        if i % int(sum_of_digits(i, base), base) == 0
    ]

    return numbers


def is_harshad_number_in_base(num: int, base: int) -> bool:
    

    if base < 2 or base > 36:
        raise ValueError("'base' must be between 2 and 36 inclusive")

    if num < 0:
        return False

    n = int_to_base(num, base)
    d = sum_of_digits(num, base)
    return int(n, base) % int(d, base) == 0


if __name__ == "__main__":
    import doctest

    doctest.testmod()