# Convert negadecimal to decimal (and back)

Negadecimal, also known as base -10, is a non-standard positional numeral system.

Take the number $$\1337_{10}\$$. In decimal, this has the value one thousand three hundred thirty seven, and can be expanded to:

$$1\cdot10^3+3\cdot10^2+3\cdot10^1+7\cdot10^0$$

$$(1000)+(300)+(30)+(7)$$

In negadecimal, instead of $$\10^n\$$, each digit would be multiplied by $$\(-10)^n\$$:

$$1\cdot(-10)^3+3\cdot(-10)^2+3\cdot(-10)^1+7\cdot(-10)^0$$

$$(-1000)+(300)+(-30)+(7)$$

Thus, $$\1337_{-10}\$$ is $$\-723_{10}\$$ in decimal. (An interesting consequence of this is that negative signs are unnecessary in negadecimal; any integer can be represented as /[0-9]+/.)

In this challenge, your score is the sum of the lengths of two programs or functions:

• One to convert a number in negadecimal to decimal
• One to convert a number in decimal to negadecimal

All inputs will be integers. You must be able to handle negative numbers. You can take input and output in any reasonable way. This is code golf, so the shortest answer in bytes per language wins.

## Test cases

1337   -> -723
0      -> 0
30     -> -30
101    -> 101
12345  -> 8265


-723   -> 1337
0      -> 0
1      -> 1
21     -> 181
-300   -> 1700
1000   -> 19000
-38493 -> 179507

• Can I take/output the negadecimal number as an array of single digit numbers? Can I take it in reverse? – Bubbler Dec 1 '20 at 0:16
• @Bubbler Yes and yes. – Redwolf Programs Dec 1 '20 at 0:21
• If I use an array of digits, may I output the negadecimal number with leading zeros? – Bubbler Dec 1 '20 at 0:47
• @Bubbler Seems reasonable. I guess you can. – Redwolf Programs Dec 1 '20 at 0:49
• @user Sounds reasonable – Redwolf Programs Dec 1 '20 at 15:01

# 05AB1E, 6 bytes

## Nega to Deca

T(ö


Try it online!

## Deca to Nega

T(в


Try it online!

• If my understanding is correct, this fails for $-723$? If the first program goes $x_{10} \to y_{-10}$ (decimal to negadecimal) then $-723$ should output $1337$ no? – caird coinheringaahing Dec 1 '20 at 0:32
• @cairdcoinheringaahing Looks like the first one is negadecimal to decimal, not the other way around. – Redwolf Programs Dec 1 '20 at 0:39

# JavaScript (Node.js), 59 bytes

f=(p,e=n=>n&&(i=(n%10+10)%10)-p*e((n-i)/p))=>e
f(10)
f(-10)


Try it online! f defines a helper function. f(10) returns a function that converts from negadecimal to decimal while f(-10) returns a function that converts from decimal to negadecimal.

• This is a really cool answer – user Dec 1 '20 at 14:24

# JavaScript (ES6), 25 + 55 = 80 bytes

## Negadecimal to decimal (25 bytes)

f=n=>n&&n%10-10*f(n/10|0)


Try it online!

### How?

This one is pretty straightforward. We recursively compute:

$$f(n) = \cases{ 0, n=0\\ (n \bmod 10) - 10\times f\left(\left\lfloor\dfrac{n}{10}\right\rfloor\right), n>0}$$

## Decimal to negadecimal (55 bytes)

f=(n,i=n%10,k=n>0?-n:~n-i+(i+=10))=>n?f(k/10|0)+i%10:''


Try it online!

### How?

In JS, the result of the % operator has the same sign as the dividend. For instance, -17 % 10 is -7 rather than 3 (like in Python), which is what we'd really like to get here. A possible workaround would be:

(n % 10 + 10) % 10


Besides, we also need to compute $$\\left\lfloor n/-10\right\rfloor\$$ with $$\n\$$ either positive or negative. This can be done with:

Math.floor(n / -10)


but this expression is a bit lengthy.

Rather than dealing with both issues separately, we explicitly test the sign of $$\n\$$ and compute the variables i and k accordingly:

• We unconditionally set i = n % 10.
• If $$\n>0\$$, we define k = -n and leave i unchanged.
• If $$\n\le 0\$$, we define k = ~n + 10 and add $$\10\$$ to i. Both operations are merged into k = ~n - i + (i += 10).

We then can use k / 10 | 0 and i % 10 in both cases.

• Second function returns an empty string for n=0. – att Dec 1 '20 at 1:40
• @att '' == 0 is true in JS. But I'll ask the OP if that's acceptable. – Arnauld Dec 1 '20 at 1:50
• Math.floor(n/-10) can be simplified to (n-i)/-10, resulting in the naïve solution of f=n=>n&&(i=(n%10+10)%10)+10*f((n-i)/-10). – Neil Dec 1 '20 at 11:26
• @Neil This is much easier indeed. You can save another byte with (i-n)/10. – Arnauld Dec 1 '20 at 11:32
• I'm not surprised, I didn't really try to golf it down. – Neil Dec 1 '20 at 11:38

# Jelly, 8 bytes

ḅ-10


Try it online!

b-10


Try it online!

Input as an integer, output as a list of digits. +2 total bytes (+1 to each) to I/O as integers

# Wolfram Language (Mathematica), 17+35 = 52 bytes

#~FromDigits~-10&


Try it online!

Straightforward base conversion from a string or list of digits.

f@0=0
f@a_:=f@-⌊a/10⌋||a~Mod~10


Try it online!

Outputs a list of digits, including a leading zero, wrapped in Or.

+1 byte to output as an integer: Try it online!

# Lisp Flavored Erlang, 33 + 59 + 59 = 153 151 bytes

(defun m(X Y)(rem(+(rem X Y)Y)Y))
(defun n(N)(if(== N 0)0(-(m N 10)(*(n(floor(/ N 10)))10))))
(defun d(N)(if(== N 0)0(+(m N 10)(*(d(ceil(/ N -10)))10))))


n computes the decimal, given a negadecimal. d computes the reverse.

Both functions take & return a regular integer.

Erlang's rem function works like JavaScript's instead of like Python's, which is why the m function is necessary. I save some total bytes by reusing it — if this were 2 separate challenges, it would be inlined as each function only uses it once.

Edit: Rearranged some of the function calls to save a couple bytes without changing logic

# Factor, 28 + 59 = 87 bytes

[ 0 [ swap 10 * - ] reduce ]


Try it online!

Takes an array of negadecimal digits and returns an integer.

[
0 [ ... ] reduce   ! Reduce over the digits with starting value of 0...
swap 10 * -      ! ( accum digit -- accum' ) Evaluate accum * -10 + digit
]


[ [ dup 0 = not ] [ -10 2dup / ceiling -rot rem ] produce ]


Try it online!

Takes an integer and returns an array of negadecimal digits in reverse order. Does not produce any leading zeros, which means the input of 0 gives an empty array.

[
[ cond ] [ loop ] produce   ! Starting with the value of n, loop until cond gives false
! and collect the top values during the loop
dup 0 = not               !   Cond: stop if top is 0
!   Loop: yield the last digit and keep the higher value
-10 2dup / ceiling        !   ( n -- n -10 keep ) keep = ceil(n/-10)
-rot rem                  !   ( keep yield ) yield = n%-10 (non-negative mod)
]


# Husk, 8 bytes

## Negadecimal to decimal, 4 bytes

B_10


Try it online!

input as list of digits, output as integer.

## Decimal to Negadecimal, 4 bytes

B_10


Try it online!

input as integer, output as list of digits.

# K (oK), 40 bytes

## Negadecimal to decimal, 10 bytes

{y+x*-10}/


Try it online!

## Decimal to negadecimal, 30 bytes

-8 bytes thanks to coltim!

{$[~x;0;(10*o@-_-x%-10)+10!x]}  Try it online! • The "canonical" ceil idiom is -_-, e.g. here – coltim Dec 1 '20 at 15:05 • @coltim Many thanks for your improvements! – Galen Ivanov Dec 1 '20 at 15:11 • Yep! In addition to individual characters, the k's also overload emoticons :) – coltim Dec 1 '20 at 15:24 • Although it's the same byte count, the $[;;] cond can be replaced, e.g. {(10!x)+10*(o;:)[~x;-_x%10]} (although this only works in the repl). It also turns out a couple more bytes can be saved by replacing -_-x%-10 with -_x%10! – coltim Dec 1 '20 at 19:44
• Yeh, it's a bit weird. (o;:) is a list containing the two functions we want to call, depending on the ~x condition. Since ~x returns 0 or 1 we use that to index into that list (thus giving us the "code" we want to run). I think : in this case is acting as an identity function, so it'll just return the input. Also, I think [~x;-_x%10] is equivalent to [~x][-_x%10] (i.e. pass the updated arg into whichever function we selected). It's basically a hack to mimic how \$[;;] doesn't evaluate its arguments. – coltim Dec 1 '20 at 19:58

# Python 2, 31 + 34 = 65 bytes

## Negadecimal to decimal, 32 31 bytes

Saved a byte thanks to xnor!!!

f=lambda n:n and~9*f(n/10)+n%10


Try it online!

## Decimal to negadecimal, 42 35 34 bytes

Saved 7 bytes thanks to att!!!
Saved another byte thanks to xnor!!!

g=lambda n:n and n%10+10*g(0-n/10)


Try it online!

• 35 bytes on the second. – att Dec 1 '20 at 19:19
• @att Nice one - thanks! :D – Noodle9 Dec 1 '20 at 19:35
• The second one can save a byte with 0-n/10. And the first one has this weird save: f=lambda n:n and~9*f(n/10)+n%10 – xnor Dec 2 '20 at 0:04
• @xnor Force the use of binary operator - nice one - thanks! : D – Noodle9 Dec 2 '20 at 0:10
• @xnor Are you a practitioner of the dark arts by any chance? -9=~9+1 so -10=~9, very nice - thanks! :D – Noodle9 Dec 2 '20 at 0:24

# Racket, 106 bytes

## Negadecimal to decimal, 39 bytes

(λ(x)(foldl(λ(a b)(+(* -10 b)a))0 x))


Try it online!

Takes an array of negadecimal digits and returns an integer.

## Decimal to negadecimal, 67 bytes

(define(d x)(if(= 0 x)0(+(modulo x 10)(*(d(ceiling(/ x -10)))10))))


Try it online!

Takes the input as an integer and returns an integer.

# Scala, 15 + 71 = 86 bytes

_.:\(0)(_-10*_)


Try them both online!

Takes input as a reversed list of digits.

This is rather straightforward - multiply the first digit by -10, add the second digit to that, multiply that by -10, and so on.

Seq.unfold(_){x=>val m=(x%10+10)%10;Option.when(x*x>0)(m->(m-x)/10)}:+0


Returns a reversed list of digits, with a leading zero (at the end).

This is not so trivial. Because of the way Scala's % works, we can't just use x%10, as it maintains the sign of x and not 10. 10 has to be added to x%10 to ensure it is positive, and then %10 is done again.

# Brachylog, 19 bytes

{×₁₀ʰ↔-}ˡ


Try it online!

≜{×₁₀ʰ↔-}ˡ


Try it online!

Negadecimal input and output is as digit lists. The decimal to negadecimal program uses reversed I/O and is also impressively slow, as it uses pure brute force (coming out 6 bytes shorter than a saner implementation).

{      }ˡ    Left fold over input negadecimal digits:
×           multiply
ʰ        the accumulator
₁₀         by 10,
↔-      subtract it from the next digit.

≜             Try outputting all integers until the output
{×₁₀ʰ↔-}ˡ    is the negadecimal representation of the input.


# C (gcc), 28 + 43 = 71 bytes

## Negadecimal to decimal, 28 bytes

f(n){n=n?n%10-10*f(n/10):0;}


Try it online!

## Decimal to negadecimal, 48 45 43 bytes

Saved 3 bytes thanks to xnor!!!
Saved 2 bytes thanks to rtpax!!!

g(n){n=n?n%~9+10*(g(n/~9+(n=n%~9<0))+n):0;}


Try it online!

• @rtpax Nice reuse of n - thanks! :D – Noodle9 Dec 2 '20 at 17:23

# R, 71 bytes

b=function(p)d=function(x)if(x,(y=x%%10)-p*d((x-y)/p),0)
b(10)
b(-10)


Try it online!

# R, 4947 41 + 47 = 88 bytes

Separate functions

negadecimal to decimal (49 47 41 bytes):

Edit: -2 bytes (and, as a consequence, -6 more bytes) thanks to att

d=function(x)if(x,x%%10-10*d(x%/%10),0)


Try it online!

d=function(x)if(x,(y=x%%10)+10*d((y-x)/10),0)


Try it online!

• 47 bytes from negadecimal – att Dec 2 '20 at 18:43
• @att - That's lovely! Not only was it right under my nose, but I even had to line-up the dec_to_neg and your neg_to_dec function to spot the differences! Thanks! – Dominic van Essen Dec 2 '20 at 23:02
• You can put them together, like Neil did, and get even fewer bytes – user Dec 2 '20 at 23:10
• @user - That is a very, very good idea! Thanks! And thanks too to Neil! – Dominic van Essen Dec 2 '20 at 23:24

# Charcoal, 5 + 18 = 23 bytes

Ｉ↨Ｓ±χ


Try it online! Link is to verbose version of code. This is just custom base conversion with a base of -10.

ＮθＰ0Ｗθ«←Ｉ﹪θχ≔±÷θχθ


Try it online! Link is to verbose version of code. Explanation:

Ｎθ


Input the decimal number.

Ｐ0


Print a 0 in case the input is zero.

Ｗθ«


Repeat until the input is zero.

←Ｉ﹪θχ


Print the next digit in right-to-left order.

≔±÷θχθ


Divide the input by 10 and change its sign. (Sadly this is not the same thing as dividing the input by -10.

# 24 23 bytes

-1 byte thanks to Bubbler

->x{x.reduce{_2-_1*10}}


Try it online!

TIO uses an older version of Ruby, while in Ruby 2.7, we've numbered parameters, which saves 3 bytes.

# 45 43 bytes

f=->x,s=''{x!=0?f[-~~(x/10),"#{x%10}"+s]:s}


Try it online!

• Your second function is invalid because taking extra parameter not specified in the challenge is not allowed. – Bubbler Dec 1 '20 at 8:05
• @Bubbler - Oops, I thought it would be allowed. – vrintle Dec 1 '20 at 8:12
• For the first function, I think _2-10*_1 should work (-1 byte)? – Bubbler Dec 1 '20 at 8:22