# This program identifies all non-negative integers n from 2 to N...
# that are expressible with only 1 and 0...
# in at most three different bases b >= 2.

# Thomas Oleron Evans, 2015


N = 10000

import math as ma

a = {}

# 2 to 8 must be part of the sequence, because...
# their representations in all bases b > 2...
# have at most 2 digits.
a[1] = 2
a[2] = 3
a[3] = 4
a[4] = 5
a[5] = 6
a[6] = 7
a[7] = 8

i = 8

for n in range(9,N + 1):

    if i > 10000:
        break

    # We need only check bases b from 3 to sqrt(n)
    # (We already know that n can be expressed using only 0,1 for b = 2, n-1, n)
    
    for b in range(3,int(ma.sqrt(n)) + 1):

        is_n_good = 0
        
        test_val = n

        while test_val > 0:

            digit = test_val % b     # rightmost unchecked digit of n in base b
            test_val = test_val // b # stores remaining unchecked digits

            if digit == 0 or digit == 1:
                continue

            else:
                is_n_good = 1
                break


        if is_n_good == 1:
            continue

        else:
            break

    if is_n_good == 1:
        a[i] = n
        i += 1

for j in range(1,i):
    print j, a[j]