18
1
Challenge:
In the programming language of your choice, accept an integer as input in base 10, and output it in the negadecimal notation, which is also known as base -10
Example algorithm:
This is an algorithm taken from Wikipedia to convert base 10 to any negative base in VB.NET:
Function toNegativeBase(Number As Integer , base As Integer) As System.Collections.Generic.List(Of Integer)
Dim digits As New System.Collections.Generic.List(Of Integer)
while Number <> 0
Dim remainder As Integer= Number Mod base
Number = CInt(Number / base)
if remainder < 0 then
remainder += system.math.abs(base)
Number+=1
end if
digits.Insert(0, remainder)
end while
return digits
end function
Obviously, you can use any algorithm, as long as it fulfills the challenge
Example inputs / outputs:
Input:
12
Output:
192
Another example:
Input:
2048
Output:
18168
Rule:
You must not use any built-in methods that solve this problem that exist in your programming language
This is a code-golf, so shortest code wins!
3I think you only want to forbit built-ins that solve this specifiic problem and not all existing builltins. – Denker – 2017-01-07T15:08:02.423
Related OEIS: A039723
– devRicher – 2017-01-07T15:08:39.330Whoops you are right @DenkerAffe , fixed. – P. Ktinos – 2017-01-07T15:09:17.567
Must we handle negative integers as well as positive ones? – user41805 – 2017-01-07T15:31:06.877
Yup, otherwise I woud say whole numbers @KritixiLithos – P. Ktinos – 2017-01-07T15:39:52.147
@P.Ktinos Negative whole numbers are as much whole numbers as positive whole numbers. – Toothbrush – 2017-01-07T16:14:44.830
@P.Ktinos I just looked up the definition of “whole number”. It seems that some people refer to (so-called) natural numbers when they say whole numbers while others (including me) refer to integers when they say whole numbers. I guess it is a matter of perspective. I've always been taught that “integer” is a synonym of “whole number”. – Toothbrush – 2017-01-07T16:19:19.343
@Toothbrush I guess you are right. I'll edit the question to avoid confusion. – P. Ktinos – 2017-01-07T16:32:58.813
6You should add a negative test case. – xnor – 2017-01-07T21:48:10.647
1Would
[0, 1, 8, 1, 6, 8]
be an acceptable output for input2048
? – Dennis – 2017-01-08T02:20:37.727@Dennis No, sorry. You should join the digits and output it as an integer. – P. Ktinos – 2017-01-08T23:39:40.803
2That might be worth mentioning in the spec. Your VB code looks like it returns a list. – Dennis – 2017-01-08T23:46:50.360
@Dennis It's an example of an algorithm used to do the conversion, as some people may not know the way to convert decimal to negadecimal. It's not meant to showcase the way it should be done, and it is not mine, it is taken from wikipedia. – P. Ktinos – 2017-01-09T12:23:06.083