22
1
From the Wikipedia article:
Location arithmetic (Latin arithmeticæ localis) is the additive (non-positional) binary numeral systems, which John Napier explored as a computation technique in his treatise Rabdology (1617), both symbolically and on a chessboard-like grid.
What?
Location numerals is a way of writing numbers using letters of the alphabet.
Binary notation had not yet been standardized, so Napier used what he called location numerals to represent binary numbers. Napier's system uses sign-value notation to represent numbers; it uses successive letters from the English alphabet to represent successive powers of two: a = 2^0 = 1, b = 2^1 = 2, c = 2^2 = 4, d = 2^3 = 8, e = 2^4 = 16 and so on.
An example
ab
= 1+2 = 3 in base 10
aabb
= 1+1+2+2 = 6 in base 10
Note that aabb
can be shortened to bc
by replacing any 2 instances of a letter by a higher one.
Addition
You just concatenate the two numbers and simplify.
acd
+ bde
= acdbde
= abcdde
= acebe
= abcf
= 39
in base 10
Subtraction
Just remove all digits appearing equally in both parts of the subtraction. Expanding (converting b
to aa
) may be necessary
abde
- ad
= be
= 18 in base 10
Multiplication
This is a bit harder.
Lets say we want to multiply acd
(13) by def
(56). First you arrange acd
vertically:
a
c
d
Then you add def
after the first a
:
a def
c
d
Now, c is 2 positions later in the alphabet than a, so we add 2 positions in the alphabet to def
to make fgh
. That is added to the second row.
a def
c fgh
d
Lastly, d is 1 position later in the alphabet than c, so we add 1 position in the alphabet to fgh
to make ghi
. That is added to the third row.
a def
c fgh
d ghi
Then you take the sum of the right: def
+ fgh
+ ghi
= deffgghhi
= deggghhi
= deghhhi
= deghii
= deghj
(728)
Another example of multiplication
Input:
bc * de
First:
b
c
Then
b ef
c
Then
b ef
c fg
Note that we wrote down ef
on the first line. That's because bc
starts with b
, and b
is the second letter in the alphabet, so we need to shift de
by 1 letter, so it becomes ef
.
Then
ef+fg
Output:
eh
Division
This is not part of this challenge, because it can get very complex.
Your actual challenge
Your program or function must take input as a string that looks like this:
a + b
And you must output:
ab
Of course, your program or function must support numbers of arbitrary length (up to the string or input limit of your language) with any of the operators +
, -
, or *
. Some more examples:
Input:
ab + bd
Output:
acd
Input:
d - ab
Output:
ac
Input:
ab * cd
Output:
cf
Notes:
- The order of letters in the output doesn't matter, but you can always assume that the order of letters in numbers in the input will be ascending (a before z).
- You may take input with a trailing newline and output with a trailing newline.
- You may not take input as a list of
ab
,*
andbd
forab * bd
. - The english alphabet is used (
abcdefghijklmnopqrstuvwxyz
) - Your output must be simplified (
aa
is not allowed,b
is required) - The input will be simplified (
b
+c
, notaa
+bb
oraa
+aaaa
) - You may require a space before and the operator (
+
,-
, or*
), or you may require there to be none. - There will only be one operator per input.
- You may assume that the output and the input will never go over 2^27-1 (
abcdefghijklmnopqrstuvwxyz
) - This is code-golf, so the shortest answer in bytes wins!
2
d is 2 positions later in the alphabet than c
is this wright? shouldn't it be1
?That is added to the second row.
on the same sentence, shouldn't it bethird
? – Felipe Nardi Batista – 2017-04-10T14:28:16.9401@FelipeNardiBatista the english alphabet is used here, edited that. – programmer5000 – 2017-04-10T14:29:35.000
@programmer5000 still,
bc*de==efgh
butefgh
is240
not144
– Felipe Nardi Batista – 2017-04-10T14:52:48.3431
bc*de
should beeh
– Felipe Nardi Batista – 2017-04-10T14:55:10.810@Dada there will only be one operator per input. – programmer5000 – 2017-04-10T14:57:53.300
May we use our languages native operators in the input string if they differ from the given
+-*
? – Jonathan Allan – 2017-04-10T17:50:20.497@JonathanAllan yes. Out of curiosity, what language uses something other than
+-*
for addition, subtraction, and multiplication that you would use for golfing? – programmer5000 – 2017-04-10T17:52:26.673Thanks - I asked because Jelly uses
×
for multiplication and_
for subtraction, I will post in a second (it would have cost seven bytes to perform the required translations). – Jonathan Allan – 2017-04-10T17:55:32.613Are we allowed to output an array of characters? – Arnauld – 2017-04-11T09:14:23.707
@Arnauld no, you may not. – programmer5000 – 2017-04-11T11:13:44.173