Skip to content

Instantly share code, notes, and snippets.

@Shaddyjr
Created May 31, 2022 07:26
Show Gist options
  • Save Shaddyjr/3cff3e7ae8a5632e1262cb4a864fb44b to your computer and use it in GitHub Desktop.
Save Shaddyjr/3cff3e7ae8a5632e1262cb4a864fb44b to your computer and use it in GitHub Desktop.
# source: https://www.hackerrank.com/challenges/kaprekar-numbers/problem
# video: https://youtu.be/UoHhr2dwP6g
def is_mod_kap(num):
'''Returns True if num is a modified kaprekar number'''
sq_num_str = str(num ** 2)
mid = len(sq_num_str) // 2 # floor division
left = sq_num_str[:mid] or 0
right = sq_num_str[mid:] or 0
return num == int(left) + int(right)
def kaprekarNumbers(p, q):
mod_kap_nums = [str(num) for num in range(p, q + 1) if is_mod_kap(num)]
if not mod_kap_nums:
print("INVALID RANGE")
else:
print(' '.join(mod_kap_nums))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment