Negative base numbers are an alternate way to encode numbers without the need for a minus sign. Various negative bases may be used including negadecimal (base -10), negabinary (-2) and negaternary (-3).[1][2]



Task

Encode the decimal number 10 as negabinary (expect 11110)
Encode the decimal number 146 as negaternary (expect 21102)
Encode the decimal number 15 as negadecimal (expect 195)
In each of the above cases, convert the encoded number back to decimal.


extra credit

 supply an integer, that when encoded to base   -62   (or something "higher"),   expresses the 
name of the language being used   (with correct capitalization).   If the computer language has 
non-alphanumeric characters,   try to encode them into the negatory numerals,   or use other 
characters instead.


F encode_neg_base(=n, b)
   I n == 0
      R ‘0’
   [Int] out
   L n != 0
      (n, V rem) = divmod(n, b)
      I rem < 0
         n++
         rem -= b
      out.append(rem)
   R reversed(out).map(String).join(‘’)
 
F decode_neg_base(nstr, b)
   I nstr == ‘0’
      R 0
   V total = 0
   L(ch) reversed(nstr)
      V i = L.index
      total += Int(ch) * b ^ i
   R total
 
print(‘Encode 10 as negabinary (expect 11110)’)
V result = encode_neg_base(10, -2)
print(result)
I decode_neg_base(result, -2) == 10
   print(‘Converted back to decimal’)
E
   print(‘Error converting back to decimal’)
print(‘Encode 146 as negaternary (expect 21102)’)
result = encode_neg_base(146, -3)
print(result)
I decode_neg_base(result, -3) == 146
   print(‘Converted back to decimal’)
E
   print(‘Error converting back to decimal’)
print(‘Encode 15 as negadecimal (expect 195)’)
result = encode_neg_base(15, -10)
print(result)
I decode_neg_base(result, -10) == 15
   print(‘Converted back to decimal’)
E
   print(‘Error converting back to decimal’)