The problem is to determine the integers $n\ge0$ that can be represented in the form
$$n=\sum\limits_{i=0}^{\max\left(\log_m(n),0\right)}b_i\ m^i\tag{1}$$
where $b_i\in\{0,1\}$ and $m$ is the base.
There are two questions to be addressed:
- Can an integer $n\ge 0$ be expressed in the form of formula (1) above where $b_i\in\{0,1\}$ and $m$ is the base, and where $n\neq m$ and $m\neq 2$?
- If so, are there multiple bases? Is there a way to find all of them?
Question (1) is simpler and answered further below with the algorithms baseRepresentationQ(n,m) and findBaseRepresentation(n,m) which loop over powers of $m$, and the algorithm getKthBaseRepresentation(k,m) which loops over the bits of $k$. All three of these algorithms are of trivial complexity and capable of handling fairly large integer values of $n$.
Question (2) is a bit more complicated. A necessary but not sufficient condition for $n>1$ to be expressible in base $m$ per formula (1) above is either $m|n$ or $m|(n-1)$. Therefore all base $m$ representations of $n$ can be found by factoring both $n$ and $n-1$ and then trying all divisors of both $n$ and $n-1$ as the base $m$.
There are more efficient factoring methods than brute force (see Integer factorization), but factoring large integers is far from trivial. The RSA cryptosystem is based on the difficulty of factoring large integers.
In OP's question above, the OP excluded the base $m=2$ since all integer values of $n$ can be represented in the form of formula (1) above for $m=2$, and also excluded the trivial case $m=n$ ($n=10_{\ n}$), but there are also the trivial cases $n=0$ ($0=0_m$), $n=1$ ($1=1_m$), and $m=n-1$ ($n=11_{\ n-1}$).
The remainder of this answer defines algorithms that address and resolve question (1) above.
Here's an algorithm that returns True if an integer $n\ge0$ can be represented in the form of formula (1) above where $b_i\in\{0,1\}$ and $m$ is the base. The algorithm below is written in the Wolfram/Mathematica language with some comments of the form (* comment *) to help clarify the algorithm and syntax.
baseRepresentationQ[n_,m_]:=Block[
{basePower=m^Floor[Max[Log[m,n],0]],y=n}, (* initialize local variables *)
While[basePower>=1,
If[y>=basePower,y=y-basePower]; (* If y>=basePower Then y=y-basePower *)
basePower=basePower/m];
If[y==0,True,False]]
Here's a slightly more complicated algorithm that returns the $b_i\in\{0,1\}$ values associated with formula (1) above if an integer $n\ge0$ can be represented in the form of formula (1) where $m$ is the base.
findBaseRepresentation[n_,m_]:=Block[
{basePower=m^Floor[Max[Log[m,n],0]],y=n,outList={}}, (* initialize local variables *)
While[basePower>=1,
If[y>=basePower,y=y-basePower;b=1,b=0]; (* If y>=basePower Then {y=y-basePower;b=1} Else b=0 *)
outList=AppendTo[outList,b]; (* add bit to outList *)
basePower=basePower/m];
If[y==0,outList,{"Remainder",y}]]
Here's a table of output values for the base $m=2$ where every integer $n\ge 0$ has a representation.
$\begin{array}{ccc} n & \text{findBaseRepresentation[n,2]} & \text{baseRepresentationQ[n,2]} \\ 0 & \{0\} & \text{True} \\ 1 & \{1\} & \text{True} \\ 2 & \{1,0\} & \text{True} \\ 3 & \{1,1\} & \text{True} \\ 4 & \{1,0,0\} & \text{True} \\ 5 & \{1,0,1\} & \text{True} \\ 6 & \{1,1,0\} & \text{True} \\ 7 & \{1,1,1\} & \text{True} \\ 8 & \{1,0,0,0\} & \text{True} \\ 9 & \{1,0,0,1\} & \text{True} \\ 10 & \{1,0,1,0\} & \text{True} \\ 11 & \{1,0,1,1\} & \text{True} \\ 12 & \{1,1,0,0\} & \text{True} \\ 13 & \{1,1,0,1\} & \text{True} \\ 14 & \{1,1,1,0\} & \text{True} \\ 15 & \{1,1,1,1\} & \text{True} \\ 16 & \{1,0,0,0,0\} & \text{True} \\ 17 & \{1,0,0,0,1\} & \text{True} \\ 18 & \{1,0,0,1,0\} & \text{True} \\ 19 & \{1,0,0,1,1\} & \text{True} \\ 20 & \{1,0,1,0,0\} & \text{True} \\ 21 & \{1,0,1,0,1\} & \text{True} \\ 22 & \{1,0,1,1,0\} & \text{True} \\ 23 & \{1,0,1,1,1\} & \text{True} \\ 24 & \{1,1,0,0,0\} & \text{True} \\ 25 & \{1,1,0,0,1\} & \text{True} \\ 26 & \{1,1,0,1,0\} & \text{True} \\ 27 & \{1,1,0,1,1\} & \text{True} \\ 28 & \{1,1,1,0,0\} & \text{True} \\ 29 & \{1,1,1,0,1\} & \text{True} \\ 30 & \{1,1,1,1,0\} & \text{True} \\ 31 & \{1,1,1,1,1\} & \text{True} \\ 32 & \{1,0,0,0,0,0\} & \text{True} \\ \end{array}$
Here's a table of output values for the base $m=3$ where only a subset of integers $n\ge 0$ have a representation. Note the integers $n\ge 0$ that have a representation cover all possible bit patterns 0, 1, 10, 11, 100, 101 etc.
$\begin{array}{ccc} n & \text{findBaseRepresentation[n,3]} & \text{baseRepresentationQ[n,3]} \\ 0 & \{0\} & \text{True} \\ 1 & \{1\} & \text{True} \\ 2 & \{\text{Remainder},1\} & \text{False} \\ 3 & \{1,0\} & \text{True} \\ 4 & \{1,1\} & \text{True} \\ 5 & \{\text{Remainder},1\} & \text{False} \\ 6 & \{\text{Remainder},2\} & \text{False} \\ 7 & \{\text{Remainder},3\} & \text{False} \\ 8 & \{\text{Remainder},4\} & \text{False} \\ 9 & \{1,0,0\} & \text{True} \\ 10 & \{1,0,1\} & \text{True} \\ 11 & \{\text{Remainder},1\} & \text{False} \\ 12 & \{1,1,0\} & \text{True} \\ 13 & \{1,1,1\} & \text{True} \\ 14 & \{\text{Remainder},1\} & \text{False} \\ 15 & \{\text{Remainder},2\} & \text{False} \\ 16 & \{\text{Remainder},3\} & \text{False} \\ 17 & \{\text{Remainder},4\} & \text{False} \\ 18 & \{\text{Remainder},5\} & \text{False} \\ 19 & \{\text{Remainder},6\} & \text{False} \\ 20 & \{\text{Remainder},7\} & \text{False} \\ 21 & \{\text{Remainder},8\} & \text{False} \\ 22 & \{\text{Remainder},9\} & \text{False} \\ 23 & \{\text{Remainder},10\} & \text{False} \\ 24 & \{\text{Remainder},11\} & \text{False} \\ 25 & \{\text{Remainder},12\} & \text{False} \\ 26 & \{\text{Remainder},13\} & \text{False} \\ 27 & \{1,0,0,0\} & \text{True} \\ 28 & \{1,0,0,1\} & \text{True} \\ 29 & \{\text{Remainder},1\} & \text{False} \\ 30 & \{1,0,1,0\} & \text{True} \\ 31 & \{1,0,1,1\} & \text{True} \\ 32 & \{\text{Remainder},1\} & \text{False} \\ 33 & \{\text{Remainder},2\} & \text{False} \\ 34 & \{\text{Remainder},3\} & \text{False} \\ 35 & \{\text{Remainder},4\} & \text{False} \\ 36 & \{1,1,0,0\} & \text{True} \\ 37 & \{1,1,0,1\} & \text{True} \\ 38 & \{\text{Remainder},1\} & \text{False} \\ 39 & \{1,1,1,0\} & \text{True} \\ 40 & \{1,1,1,1\} & \text{True} \\ 41 & \{\text{Remainder},1\} & \text{False} \\ 42 & \{\text{Remainder},2\} & \text{False} \\ 43 & \{\text{Remainder},3\} & \text{False} \\ 44 & \{\text{Remainder},4\} & \text{False} \\ 45 & \{\text{Remainder},5\} & \text{False} \\ 46 & \{\text{Remainder},6\} & \text{False} \\ 47 & \{\text{Remainder},7\} & \text{False} \\ 48 & \{\text{Remainder},8\} & \text{False} \\ 49 & \{\text{Remainder},9\} & \text{False} \\ 50 & \{\text{Remainder},10\} & \text{False} \\ 51 & \{\text{Remainder},11\} & \text{False} \\ 52 & \{\text{Remainder},12\} & \text{False} \\ 53 & \{\text{Remainder},13\} & \text{False} \\ 54 & \{\text{Remainder},14\} & \text{False} \\ 55 & \{\text{Remainder},15\} & \text{False} \\ 56 & \{\text{Remainder},16\} & \text{False} \\ 57 & \{\text{Remainder},17\} & \text{False} \\ 58 & \{\text{Remainder},18\} & \text{False} \\ 59 & \{\text{Remainder},19\} & \text{False} \\ 60 & \{\text{Remainder},20\} & \text{False} \\ 61 & \{\text{Remainder},21\} & \text{False} \\ 62 & \{\text{Remainder},22\} & \text{False} \\ 63 & \{\text{Remainder},23\} & \text{False} \\ 64 & \{\text{Remainder},24\} & \text{False} \\ 65 & \{\text{Remainder},25\} & \text{False} \\ 66 & \{\text{Remainder},26\} & \text{False} \\ 67 & \{\text{Remainder},27\} & \text{False} \\ 68 & \{\text{Remainder},28\} & \text{False} \\ 69 & \{\text{Remainder},29\} & \text{False} \\ 70 & \{\text{Remainder},30\} & \text{False} \\ 71 & \{\text{Remainder},31\} & \text{False} \\ 72 & \{\text{Remainder},32\} & \text{False} \\ 73 & \{\text{Remainder},33\} & \text{False} \\ 74 & \{\text{Remainder},34\} & \text{False} \\ 75 & \{\text{Remainder},35\} & \text{False} \\ 76 & \{\text{Remainder},36\} & \text{False} \\ 77 & \{\text{Remainder},37\} & \text{False} \\ 78 & \{\text{Remainder},38\} & \text{False} \\ 79 & \{\text{Remainder},39\} & \text{False} \\ 80 & \{\text{Remainder},40\} & \text{False} \\ 81 & \{1,0,0,0,0\} & \text{True} \\ \end{array}$
Another way to look at it is here's a table of integers $n\ge 0$ that have a representation for the base $m=3$ consistent with formula (1) above where $b_i\in\{0,1\}$.
$\begin{array}{ccc} n & \text{findBaseRepresentation[n,3]} & \text{baseRepresentationQ[n,3]} \\ 0 & \{0\} & \text{True} \\ 1 & \{1\} & \text{True} \\ 3 & \{1,0\} & \text{True} \\ 4 & \{1,1\} & \text{True} \\ 9 & \{1,0,0\} & \text{True} \\ 10 & \{1,0,1\} & \text{True} \\ 12 & \{1,1,0\} & \text{True} \\ 13 & \{1,1,1\} & \text{True} \\ 27 & \{1,0,0,0\} & \text{True} \\ 28 & \{1,0,0,1\} & \text{True} \\ 30 & \{1,0,1,0\} & \text{True} \\ 31 & \{1,0,1,1\} & \text{True} \\ 36 & \{1,1,0,0\} & \text{True} \\ 37 & \{1,1,0,1\} & \text{True} \\ 39 & \{1,1,1,0\} & \text{True} \\ 40 & \{1,1,1,1\} & \text{True} \\ 81 & \{1,0,0,0,0\} & \text{True} \\ \end{array}$
Here's a python version of the algorithm baseRepresentationQ(n,m) along with some test code for the base $m=3$.
import math
def baseRepresentationQ(n,m):
# declare and define local variables y="local" basePower="local" y=n if n>1: basePower=m**math.floor(math.log(n,m)) else: basePower=1 # loop through the powers of the base m while basePower>=1: if y>=basePower: y=y-basePower basePower=basePower/m return y==0
for x in range(82):
if baseRepresentationQ(x,3): print(x)
Here's the output of the python code version of the algorithm baseRepresentationQ(n,m) above which is consistent with the last table above generated by the Mathematica version of the algorithm.
0 1 3 4 9 10 12 13 27 28 30 31 36 37 39 40 81
Here's a python version of the algorithm getBaseRepresentation(n,m) along with some test code for the base $m=3$.
import math
def getBaseRepresentation(n,m):
# declare and define local variables y="local" basePower="local" b="local" bitList="local" retVal="local" y=n if n>1: basePower=m**math.floor(math.log(n,m)) else: basePower=1 bitList=[] # loop through the powers of the base m while basePower>=1: if y>=basePower: y=y-basePower b=1 else: b=0 basePower=basePower/m bitList.append(b) if y==0: retVal=bitList else: retVal=y return retVal
for x in range(82):
print(x,getBaseRepresentation(x,3))
Here's a python version of an algorithm getKthBaseRepresentation(k,m) along with some test code for the base $m=3$. This algorithm returns the $k^{th}$ integer $n$ (and the bits of it's binary representation to the base $m$ where counting starts at $k=0$) which can be represented in the form of formula (1) above where $b\in\{0,1\}$.
import math
def getKthBaseRepresentation(k,m):
# declare and define local variables y="local" n="local" basePower="local" b="local" bitList="local" retVal="local" y=k n=0 basePower=1 bitList=[] retVal=[] if y==0: bitList.append(0) # loop through the bits of y while y>=1: if y&1==1: n=n+basePower b=1 else: b=0 basePower=basePower*m bitList.insert(0, b) if y!=0: y=y>>1 else: y=-1 retVal.append(n) retVal.append(bitList) return retVal
for k in range(17):
print(k,getKthBaseRepresentation(k,3))
Here's the output of the python code version of the algorithm getKthBaseRepresentation(k,m) and test code above for the base $m=3$ which is consistent with the last table above generated by the Mathematica version of the getBaseRepresentation(n,m) algorithm.
k [n, [bitList]]
0 [0, [0]]
1 [1, 1]
2 [3, [1, 0]]
3 [4, [1, 1]]
4 [9, [1, 0, 0]]
5 [10, [1, 0, 1]]
6 [12, [1, 1, 0]]
7 [13, [1, 1, 1]]
8 [27, [1, 0, 0, 0]]
9 [28, [1, 0, 0, 1]]
10 [30, [1, 0, 1, 0]]
11 [31, [1, 0, 1, 1]]
12 [36, [1, 1, 0, 0]]
13 [37, [1, 1, 0, 1]]
14 [39, [1, 1, 1, 0]]
15 [40, [1, 1, 1, 1]]
16 [81, [1, 0, 0, 0, 0]]