Pythonic mine's:
import sys # isKaprekar(): {1, ..., sqrt(sys.maxint)} -> {1, ..., sys.maxint} def isKaprekar(i): powered = i*i s = str(powered) mid = len(s)/2 if mid == 0: return i == powered elif mid > 0: lefti = int(s[:mid]) righti = int(s[mid:]) return i == lefti + righti i = int(sys.argv[1]) if i > 0 and i<=int(pow(sys.maxint, 1.0/2.0)): kaprekar = [] for n in xrange(1, i): if isKaprekar(n): kaprekar.append(n) print("{}".format(kaprekar)) else: # bad input print("Bad input: {}".format(i)) sys.exit(-1)
Usage: python script.py max max is the maximum integer to look up to (3037000499):
python script.py max
max
>>> pow(sys.maxint, 1.0/2.0) 3037000499.97605
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Pythonic mine's:
Usage:
python script.py max
max
is the maximum integer to look up to (3037000499):