We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
def kaprekarNumbers(p, q):
arr = []
if p == 1:
arr.append(1)
start = p if p >= 9 else 9
for i in range(start, q+1):
left = str(i**2)[:-len(str(i))]
right = str(i**2)[-len(str(i)):]
if int(left) + int(right) == i:
arr.append(i)
print(*arr) if arr else print("INVALID RANGE")
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Modified Kaprekar Numbers
You are viewing a single comment's thread. Return to all comments →
def kaprekarNumbers(p, q): arr = [] if p == 1: arr.append(1) start = p if p >= 9 else 9 for i in range(start, q+1): left = str(i**2)[:-len(str(i))] right = str(i**2)[-len(str(i)):] if int(left) + int(right) == i: arr.append(i) print(*arr) if arr else print("INVALID RANGE")