def euler_346(): n_max = 10**12 repunits = set() result = 1 # Our algorithm doesn't catch 1 so we add it here for base in range(2, n_max): n = base ** 0 + base ** 1 for exp in range(2, n_max): n += base**exp if exp == 2 and n > n_max: # There are no more repunits below n_max. return result if n >= n_max: break if not n in repunits: repunits.add(n) result += n if __name__ == "__main__": solution = euler_346() print("e346.py: " + str(solution)) assert(solution == 336108797689259276)