def gen_ndig_pal(n): listS = [str(i) for i in range(10)] # single digits as str listD = [ listS[i]+listS[i] for i in range(10)] # double digits as str # special case n==1 if n==1: return listS # special case isOdd = n % 2; # 0 or 1 nTimes = int(n/2) - 1 # number of insertions of listD, after initialization if isOdd==1: nTimes += 1 # one final, special-case insertion if n is odd palOut = listD; palTmp = [] for j in range(nTimes): for thisPal in palOut: # perform insertion in the middle of each list item end1 = thisPal[:j+1]; end2 = thisPal[j+1:] insertionList = listD if isOdd==1 and j==(nTimes-1): insertionList = listS #special case-- final insertion of single char if isOdd for mid1 in insertionList: palTmp.append( end1 + mid1 + end2 ) palOut = palTmp # replace palTmp = [] # re-init empty return palOut def triplePal(n): # create a length "n" palindrome with 3 consituent palindromes len>=2 try: halfN = int(n/2) pal3Out = [] for i in range(2,halfN): midLen = n - 2*i if midLen<2: break endList = gen_ndig_pal(i) midList = gen_ndig_pal(midLen) for end1 in endList: if end1[0:1]=="0": continue # no leading zero values for mid1 in midList: val1 = end1+mid1+end1 if val1 not in pal3Out: pal3Out.append( int(val1) ) except Exception as e: print(f"An unexpected error occured: {e}") return sorted(pal3Out) pal6List = triplePal(6) prtCount = 0 for str1 in pal6List: print(str1+",",end="") prtCount+=1 if prtCount>100: break