loading...

re: Challenge: find 'Kaprekar numbers' VIEW POST

FULL DISCUSSION
 

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):

>>> pow(sys.maxint, 1.0/2.0)
3037000499.97605
Code of Conduct Report abuse