The question is regarding to the python code for following problem. We consider two type of digits, negative and positive to represent integers in given base. We are presenting theorem (self made), that also says, its representation is unique.
Let $n=1997$ so, it is equal to $2000-3= 2\cdot10^3+0\cdot10^2+0\cdot10^1+(-3)\cdot10^0=(2,0,0,-3)_{10}$ similarly it is equal to $(2,0,-1,7)_{10},(2,0,-0,-3)_{10},(2,-0,0,-3)_{10},(2,-1,9,7)_{10},(1,-8,-1,9,7)_{10}$
If we consider digits, $-0$ and $0$ are as distinct digits but its value are same and equal to zero, then (Theorem:) any integer can written uniquely with negative and positive digits with its preserve order of negative positive position respectively.
PROBLEM : let $(\ldots,+,+,-)$ preserve order of positive and negative digits, i.e. only fist digit from right should be negative, for example, $23=(3,-7)$. write the program.
I have attempted following, but it not give correct output for single digit number and for number whose leading digit is 9 which again mention at the end as a error. Can you please give better code to get correct output for all integer.
def D1(num,n2):
if num == 1:
return 1
if num == 0:
return 0
rem_array = [0]*3
le1 = len(str(n2))
while n2 != 0:
le = len(str(n2))
le2 = le1-le
mod = n2%num
if mod != 0 and le2 != 0:
rem = mod
n2 = n2 - rem
rem_array[le2]=rem
n2 = n2//num
elif mod != 0 and n2 != 1:
rem = num - mod
n2 = n2 + rem
rem_array[le2]=-rem
n2 = n2//num
else:
n2 = n2//num
rem_array[le2]=0
return rem_array[::-1]
for i in range(3,128,13):
print(i,D1(10,i))
output
3 [0, 0, 0]
16 [0, 2, -4]
29 [0, 3, -1]
42 [0, 5, -8]
55 [0, 6, -5]
68 [0, 7, -2]
81 [0, 9, -9]
94 [0, 1, 0]
107 [1, 1, -3]
120 [1, 2, 0]
Error : for 3 and 94 output is not correct.