5
\$\begingroup\$

Your task is to get two numbers, and output the digit sum of them. We'll define digit sum as the following:

  • take the decimal (base 10) digits of each of them, and pad with 0s the shortest number (e.g. [1,9], [0, 7])
  • sum the digits as vectors (e.g. [1, 16])
  • convert the number back from base 19, and output it (e.g. 1*19+16=35)

Input

You can take the input as two numbers, as a list containing both of them or in any other reasonable format (like an array of digits). You can assume the shortest number is padded with zeros so it fits the length of the longer one

Output

You can output the digit sum in any reasonable format.

Test cases

1, 2 -> 3
5, 10 -> 24
42, 32 -> 137
15717, 51358 -> 826819
1000, 1 -> 6860

explanation of 5, 10: the digits of these are [0, 5] and [1, 0]. Their sum is [1, 5], which when converted from base 19 results in 24.

Scoring

This is code golf, so the shortest answer wins, good luck!

\$\endgroup\$

20 Answers 20

7
\$\begingroup\$

J, 6 bytes

19#.+/

Try it online!

  • 19#. convert the following to base 19...
  • +/ elementwise sum of the input digit lists.
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ You can take a digit list as an input. \$\endgroup\$ – user92069 May 4 at 11:17
  • \$\begingroup\$ Thanks, not much content at this point though. \$\endgroup\$ – Jonah May 4 at 11:20
6
\$\begingroup\$

Jelly, 4 bytes

ḅ19S

A monadic Link accepting a list of lists of digits which yields an integer.

Try it online!

| improve this answer | |
\$\endgroup\$
5
\$\begingroup\$

Python, 30 bytes

lambda a,b:int(a,19)+int(b,19)

An unnamed function accepting two strings of (base ten) digit characters which returns an integer.

Try it online!

| improve this answer | |
\$\endgroup\$
4
\$\begingroup\$

Japt -mx, 6 3 bytes

Takes input as an array of digit arrays.

ì19

Try it

| improve this answer | |
\$\endgroup\$
4
\$\begingroup\$

05AB1E, 4 bytes

-1 thanks to Command Master.

+19β

Try it online!

| improve this answer | |
\$\endgroup\$
  • 2
    \$\begingroup\$ 4 bytes \$\endgroup\$ – Command Master May 6 at 13:41
  • \$\begingroup\$ + vectorizes!? I was an idiot. \$\endgroup\$ – user92069 May 6 at 13:43
3
\$\begingroup\$

Python 2, 46 bytes

f=lambda a,b:a+b and a%10+b%10+19*f(a/10,b/10)

Try it online!

Recursive function that sums each digit pair and convert them to base 19.

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Wolfram Language (Mathematica), 24 bytes

Tr[#~FromDigits~19&/@#]&

Unnamed function taking as input a list that contains two lists of digits. The main point here is that converting to base 19 first and then adding means that padding on the left is unnecessary.

Try it online!

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Wolfram Language (Mathematica), 30 bytes

Total@PadLeft@#~FromDigits~19&

Try it online!

Takes input as list of digits as @my pronoun is monicareinstate suggested and saved 13 bytes (IntegerDigits)

-1 byte @LegionMammal978

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ I think you can take two lists of digits as input. \$\endgroup\$ – the default. May 4 at 13:41
  • \$\begingroup\$ -1 byte: Total@PadLeft@IntegerDigits@#~FromDigits~19& \$\endgroup\$ – LegionMammal978 May 5 at 2:33
2
\$\begingroup\$

Bash, 54 53 19 bytes

echo $[19#$1+19#$2]

Try the test cases online!


The input numbers are passed as two arguments.

Output is on stdout.

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

C (gcc), 67 65 bytes

Saved 2 bytes thanks to Mitchell Spector!!!

s;m;f(a,b){for(s=m=1;a+b;a/=10,b/=10,m*=19)s+=(a%10+b%10)*m;s--;}

Try it online!

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ 65 bytes \$\endgroup\$ – Mitchell Spector May 5 at 3:51
  • 1
    \$\begingroup\$ @MitchellSpector Nice, homogeneous init with a tweak at the end to make it the return value - thanks! :-) \$\endgroup\$ – Noodle9 May 5 at 9:34
2
\$\begingroup\$

K (oK), 5 bytes

Solution:

19/+/

Try it online!

Explanation:

Takes input as two lists of digits. Assumes the shorter number is padded with 0s.

19/+/ / the solution
   +/ / sum
19/   / decode from base 19
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Charcoal, 8 bytes

IΣE²⍘S¹⁹

Try it online! Link is to verbose version of code. Takes input as two strings. Explanation:

    ⍘S¹⁹    Convert the input from base 19
  E²        Repeated twice
IΣ          Summed and output in decimal
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

dc, 47 bytes

?[0q]sZ[sydsxly+0=ZlxA~rlyA~4R+_3Rlfx19*+]dsfxp

Try it online!

Or verify all the test cases online.

Input is on stdin (the two numbers on one line, separated by a space).

Output is on stdout.


This program makes use of a nice facility in dc, where the single-character command ~ will compute both the quotient and the remainder in a division operation.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Java (JDK), 40 bytes

a->b->a.valueOf(""+a,19)+a.valueOf(b,19)

Try it online!

Takes input as a:Integer and b:String.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

PHP, 75 bytes

for($n=strlen($a=$argv[1]);$n--;)$r+=($a[$i]+$argv[2][$i++])*19**$n;echo$r;

Try it online!

As often, getting arguments and dollars makes PHP the longest answer so far :D still i'm content with this

Takes input as strings already padded with zeroes

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

JavaScript (Babel Node), 43 bytes

f=(a,b)=>a+b&&a%10+b%10+19*f(a/10|0,b/10|0)

Try it online!

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ But (a,b)=>(p=parseInt)(a,19)+p(b,19) is only 33 bytes... \$\endgroup\$ – tsh May 7 at 5:07
1
\$\begingroup\$

Pyth, 7 6 5 bytes

Takes input as two lists of digits. Assumes the shorter number is padded with 0's.

siR19

Try it online!

siR19
  R       (Right-) Map each digit array of the input to:
 i 19     The digit array converted from base 19
s         Take the sum
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Ruby, 24 bytes

->a{a.sum{|x|x.to_i 19}}

Accepts an array of two strings.

Try it online!

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Brachylog, 6 bytes

~ḃ₁₉ᵐ+

Try it online!

Takes input as a digit list, but the testing header converts from single integers for convenience.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Python 3, 77 72 bytes

lambda x,y:sum(n*19**x for x,n in enumerate([*map(sum,zip(x,y))][::-1]))

Try it online!

Not one of the shortest approaches, but it works.

-5 bytes thanks to @ovs

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ You can use map for 72 bytes: enumerate([*map(sum,zip(x,y))][::-1]). \$\endgroup\$ – ovs May 4 at 17:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.