r/dailyprogrammer • u/Elite6809 1 1 • Jan 20 '15
[2015-01-21] Challenge #198 [Intermediate] Base-Negative Numbers
(Intermediate): Base-Negative Numbers
"Don't be stupid, Elite6809!", I hear you say. "You can't have a negative base." Well, why not? Let's analyse what we mean by base. Given a base-r system, the column p places from the right (starting from zero), which contains the digit n, has the value n×rp. The binary columns 1, 2, 4, 8, 16, ... is the same as 20, 21, 22, 23, 24. Nothing stops you from using a negative base with this system, except perhaps the understanding of the concept and practicality of its usage.
Let's imagine base -10 (negadecimal). Here, the place values for each column are now 1, -10, 100, -1000 and so on. Therefore, the negadecimal number 7211:
-Thousands Hundreds -Tens Units
7 2 1 1
(-7000) + (200) + (-10) + (1) = -6809Is equivalent to -6809 in standard decimal.
Your challenge is, given a negative base and a value, convert it to the representation in the corresponding positive base, and vice versa.
Input and Output Description
Challenge Input
You will accept two numbers: r and n. n is a number in base r. For example:
-4 1302201This input means 1302201 in base -4.
Challenge Output
Print the value of the input number in the corresponding opposite-signed base, for example, for the input above:
32201As 1302201 in base -4 equals 32201 in base 4.
Sample Inputs and Outputs
Input: -10 12345678 (convert from base -10 to base 10)
Output: -8264462
Input:-7 4021553
Output: 4016423
Similarly, if the given base is positive, convert back to the corresponding negative base.
Input: 7 4016423 (convert from base 7 to base -7)
Output: 4021553
Input: 6 -3014515
Output: 13155121
Extension (Hard)
Extend your program to support imaginary bases. Imaginary bases can represent any complex number. The principle is the same; for example, base 4i can be used to represent complex numbers much the same way as a cartesian representation like a+bi. If you have forgotten the principles of imaginary numbers, re-read the challenge description for The Complex Number - you might want to re-use some code from that challenge anyway.
Notes
Try and do both the main challenge and extension without looking for the conversion algorithms on the internet. This is part of the challenge!
7
u/Godspiral 3 3 Jan 21 '15 edited Jan 21 '15
In J,
I don't get the signs, but think they invert if there is an odd number of digits. absolute number version
signed version
2
u/sabrepiez Jan 21 '15 edited Jan 21 '15
Hi, i'm not sure if its error but i've noticed some inconsistency in your examples. http://prntscr.com/5v2ovh Cheers.
Edit* C++ Done the conversion from negative base to positive base, but it doesn't work the other way, trying to figure it out now. (Would love some help on it, been stuck on it for a while)
2
u/zck Jan 21 '15
In Arc (try it online!), in a style that I consider pretty nice functionality:
And how it's used:
Note that the second example (Input:-7 4021553) is incorrect, as given. The answer should be 475268.
4
u/Armavica Jan 21 '15
My solution in Haskell, using a slightly modified Horner's method to convert the input to a decimal integer, and the standard division method to do the second conversion. It took me a little time to get the signs right but I think that they are correct now.
1
u/frozensunshine 1 0 Jan 21 '15
I am so confused. Let us consider -1 in base 10.
In base (-3), I can express this as (-1). But I can also express it as (12) in base (-3). So which is correct?
In binary we specify a number as signed or unsigned, so the number 1111 (base 2) can be 15 or -1 depending on how you interpret it. I'm completely lost as to how to describe a signed number in a negative one :(
3
u/frozensunshine 1 0 Jan 22 '15 edited Jan 24 '15
C99 Mine is ugly, but I'm really pleased I could get the all combinations of bases/numbers to work. I did one method for pos->neg base, and another for neg->pos. Working on generalizing pos->neg code to the other case too.
EDIT: I managed to get a generalized method! So now I have only one function that converts from positive base to negative base, regardless of whether the given number is negative or positive. Check it out here. Involves a slight modification to below code.
Loved the challenge, by the way, /u/Elite6809! Deceptive looking for sure.
Explanation for convert_to_neg function:
Consider the example provided, 4016423 in base 7.
This number, in decimal, can be written as a polynomial of 7 with coefficients being the digits of this number:
Now if we want to express x in base (-7), that means we want to write this polynomial as powers of (-7). The even powers are fine, because it's the same for -7 and 7. So we only need to rewrite the above polynomial so that the odd powers of 7 get converted to odd powers of -7. This can be done as follows:
Let us take the first odd power of 7 : 2. We can rewrite 2 as (7-5). So in the above expression, the last but one term becomes:
Thus we now have our first odd power of (-7) in the expression for x. Note that we have an extra power of 2, so that gets taken into the coefficient for 72. We thus continue the process, rewriting odd power coefficients as "base - something" so that the coefficients of the odd power bases become negative.
Doing this for a few more steps, continuing from above (and readjusting coeff of 72 from above step):
Thus, we now have the number x expressed as a polynomial of (-7). Reading off the coeffs gives us the number in (-7) base. So the number is: 4021553, which is correct.
Thus, we can very easily convert a number from positive base to negative base. Note that if the given number is negative, you don't have to change anything, just work with the absolute value and the result is same. Proof for that is quite straightforward.
2
u/Godspiral 3 3 Jan 21 '15 edited Jan 21 '15
does the first negative sign start at the msb? ie. is this right for challenge? or is it 2nd lsb?
_1 3 0 2 _2 0 _1
and example should be -6806 (for 7214)?... just noticed you asked in text for 7211 which is -6809
2
u/fvandepitte 0 0 Jan 21 '15 edited Jan 21 '15
C++, as usual, any comment is appriciated.
Output
EDIT: Did some cleanup
2
u/swingtheory Jan 21 '15
Here is my solution in Haskell... it took me MUCH longer than I'm willing to admit, and I apologize in advance if it's a little messy. I pushed through and found a solution eventually, but sacrificed neatness near the end because of my frustration. Overall a GREAT challenge and I feel like I really pushed the limits of my Haskell knowledge.
2
u/Gronner Jan 21 '15 edited Jan 22 '15
Here is my Python 2.7 Solution. At first i had some problems with the conversion from pos to neg bases, but wikipedia gave a valuable hint on this:
Hint from Wikipedia
2
u/NoobOfProgramming Jan 21 '15
I could not figure this one out and googled it most shamefully, but i really like that it was posted a day early. It would be totally cool if you posted the hard challenge early, too.
1
u/Leipreachan Jan 21 '15 edited Jan 22 '15
Decided to try my knowledge of Ruby. Like a few others, couldn't get the positive base to negabase figured out.
Will edit once I figure that out. Would love any feedback
Edit: If negabase converted to a negative base-10 value it produced the incorrect newBase result.
1
u/ChiefSnoopy Jan 21 '15
My solution in C:
1
u/AquitaineHungerForce Jan 22 '15
I used C++, first submission entry. It converts from any integer base to any other integer base, but isn't pretty.
It does convert positive to negative bases correctly though, which seems to have been the harder part of the problem.
Since I generalized the problem a little you need to enter 6 -3014515 -6 instead of 6 -3014515 for example.
1
u/gatorviolateur Jan 22 '15
Scala. I give up after hours of struggling with the signs. This works fine for going -ve -> +ve, but blows up on +ve -> -ve. I had a hard time grasping how mod and div works for -ve numbers. Here's a partial solution:
1
u/[deleted] Jan 22 '15
In Ruby. Totally stole /u/mcslane's way of doing the problem. Credit to him! Also, when I figure out positive → negative, I'll edit.
1
u/Qyaffer Jan 22 '15
Java
OUTPUT:
1
u/voodooChickenCoding Jan 23 '15
My solution is in c++. It seems to have worked with all the test cases, but I feel it's a bit long, contorted and hard to read. Pointers on what I could improve are much appreciated - this challenge took me to the limits as an inexperienced programmer.
1
u/Zifre Jan 23 '15
Just like 198_easy, c++ and Go solutions. The Go solution is mostly a copy/paste from the c++ one, just started learning the language so I do this to get more familiar with its syntax.
C++:
Go:
1
u/ichivictus Jan 24 '15
}
So I just learned a bit of Java and thought I'd try this. It doesn't give the right answer for positive to negative bases. I'm pretty stumped, but I'll take a better look after I get some sleep.
1
u/wizao 1 0 Jan 28 '15 edited Jan 28 '15
Haskell
I'm working on generalizing it to work with Num instead of just Int, so it can work with Data.Complex for imaginary numbers.
1
u/unfeelingtable Jan 21 '15
My solution in C#.
https://gist.github.com/JordanConley/3025687150766d9fadb9
1
u/[deleted] Jan 21 '15 edited Jan 02 '16
0
u/kaderx Jan 21 '15
So for -4 ...201 it would be:
1 * (-40 ) + 0 * (-41 ) + 2 * (-42 ) and so on.
Am I understanding that correctly?