Runaround Numbers

runaround

Algorithm

The numbers are small enough that a brute-force solution is fine.

Python

def uniqueDigits(num):
    # Does it have unique digits?
    s = str(num)
    return len(s) == len(set(s))

def runaround(num):
    # Is it a runaround?
    if not(uniqueDigits(num)): return False
    s = str(num)
    touched = [0]
    index = 0
    while len(touched) < len(s)+1:
        # Get the next digit
        new = (index+int(s[index]))%len(s)
        if len(touched) == len(s):
            # Does it loop back to the beginning?
            if new != 0: return False
        # Has a digit been visited already?
        elif new in touched: return False
        touched.append(new)
        index = new
    return True

def main():
    with open('runround.in','r') as fIn: n = int(fIn.read())
    n += 1
    while not(runaround(n)): n += 1
    with open('runround.out','w') as fOut: fOut.write(str(n)+'\n')

if __name__ == '__main__':
    main()