0
$\begingroup$

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.

$\endgroup$
5
  • $\begingroup$ This is hard to follow. I think there is a language issue here...in any case, it's not clear what "with its preserve order respectively" might mean. $23=2\times 10+3$ and $23=3\times 10-7$, right? Isn't that two distinct ways to write $23$? And you give several ways to write $1997$...so what sort of uniqueness did you have in mind? $\endgroup$
    – lulu
    Commented Jun 6, 2023 at 9:28
  • $\begingroup$ @lulu I Apologise for my English, it is distinct because Oder of sequence of negative postive place are distinct as, $23=(2,3)$ which have $(+,+)$ Oder and $23=(3,-7)$ have $(+,-)$ oder. I will try to edit it for better. $\endgroup$
    – Pruthviraj
    Commented Jun 6, 2023 at 9:37
  • $\begingroup$ Ok, got it. So...what's the question? If you only allow the units digit to be negative, then you first round up to a multiple of $10$, write that in the usual way, and subtract what you need (including $0$ possibly). Is that what you were asking? $\endgroup$
    – lulu
    Commented Jun 6, 2023 at 9:40
  • $\begingroup$ @lulu I'm asking for python code to get such output for every integer positive base$\ge 2$. Where Oder of nigative and positive digits places already given. I already attempted my try but not get exact output. Which i already mentioned. $\endgroup$
    – Pruthviraj
    Commented Jun 6, 2023 at 9:46
  • 1
    $\begingroup$ Don't know that people will write code for you here, but the algorithm is clear. Just round up to the nearest multiple of $10$ and subtract as needed. $\endgroup$
    – lulu
    Commented Jun 6, 2023 at 10:03

0

You must log in to answer this question.