• + 0 comments

    Here is my rather long Python solution! The first function checks if a number is a modified kaprekar number, and the second function puts all numbers in the specified into the first function, and returns the results.

    def isk(num):
        d = len(str(num))
        num = str(num ** 2)
        if len(num) == 2 * d:
            if int(num[-d:]) + int(num[:d]) == math.sqrt(int(num)):
                return True
        elif len(num) == 2 * d - 1:
            if d != 1:
                if int(num[-d:]) + int(num[:d - 1]) == math.sqrt(int(num)):
                    return True
            elif num == "1":
                return True
        return False
    
    def kaprekarNumbers(p, q):
        knums = []
        for num in range(p, q + 1):
            if isk(num):
                knums.append(str(num))
        if len(knums) == 0:
            print("INVALID RANGE")
            return
        print(" ".join(knums))