Created
May 31, 2022 07:26
-
-
Save Shaddyjr/3cff3e7ae8a5632e1262cb4a864fb44b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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