#This program uses an optimization where it jumps ahead to the next candidate after a failure nthTerm:=proc(n) local m, b, failures, failure, mset, brep: m:=5: while true do: failures:={}: for b from n by -1 to 3 do: brep:=convert(m, base, b): if evalb(2 in brep) then failures:=failures union {[b,brep]}: fi: od: if failures={} then return m: else mset:={}: for failure in failures do: mset:=mset union {nextNoTwo(failure[1], failure[2])}: od: m:=max(mset): fi: od: end: #The smallest number greater than the one with base-b representation #brep that contains no twos in base b nextNoTwo:=proc(b, brep) local brep2,i,j,m: #Convert to an array so nthTerm(11) doesn't run out of memory brep2:=Array(brep): for i from ArrayNumElems(brep2) by -1 to 1 do if brep2[i]=2 then #Increment the counter brep2[i]:=3: if brep2[i]=b then brep2[i]:=0: for j from i+1 to ArrayNumElems(brep2) do brep2[j]:=brep2[j]+1: if brep2[j]<>b then break: else: brep2[j]:=0: fi: od: fi: for j from i-1 by -1 to 1 do: brep2[j]:=0: od: break: fi: od: #Now convert back to an integer m:=0: for j from 1 to ArrayNumElems(brep2) do m:=m+brep2[j]*b^(j-1): od: if m=0 then m:=b^ArrayNumElems(brep2): fi: return m: end: