27
\$\begingroup\$

Task

Given two positive integers, output the number of carries needed to add them together in long addition in base 10.

Examples

¹¹¹   <-- carries
 999
+  1
----
1000

Three carries are needed.

 ¹
 348
+ 91
----
 439

One carry is needed.

Testcases

999,   1 -> 3
398,  91 -> 1
348,  51 -> 0
348,  52 -> 2
  5,  15 -> 1
999, 999 -> 3
505, 505 -> 2

Scoring

This is . Shortest answer in bytes wins. Standard loopholes apply.

\$\endgroup\$
6
  • \$\begingroup\$ Related. \$\endgroup\$
    – Leaky Nun
    Jun 11, 2017 at 10:31
  • 14
    \$\begingroup\$ Suggested test case: 190192, 90909 (has a break in the carries). \$\endgroup\$ Jun 11, 2017 at 11:14
  • 5
    \$\begingroup\$ From @Jenny_mathy 's answer: the number of carries is equal to the difference between (1) the sum of the digit sum of the two inputs and (2) the digit sum of the sum of the two inputs, divided by nine. This is because when there is a carry, you subtract 10 from and add 1 to the digit sum. For instance, 9+9 gives you 18, but the digit sum is 9+9-10+1 because there is a carry. \$\endgroup\$ Jun 11, 2017 at 14:14
  • \$\begingroup\$ Related anarchy endless challenge \$\endgroup\$
    – xnor
    Jun 11, 2017 at 17:00
  • \$\begingroup\$ Can we assume the numbers fit in our language's int type? In particular for Python 2, should we deal with repr appending an L for numbers above 2**63-1? \$\endgroup\$
    – xnor
    Jun 11, 2017 at 17:04

11 Answers 11

21
\$\begingroup\$

Mathematica, 46 39 bytes

x=Tr@*IntegerDigits;(x@#+x@#2-x@+##)/9&

input

[348,51]

-7 bytes from JungHwan

\$\endgroup\$
4
  • \$\begingroup\$ Dang, I really like this method. The difference between (1) the sum of the digit sum of the two inputs and (2) the digit sum of the sum of the two inputs, is nine times the number of carries because when there is a carry, you subtract 10 from the digit sum and add 1 to the digit sum. \$\endgroup\$ Jun 11, 2017 at 14:10
  • \$\begingroup\$ Me too! thanks for the golfing tips \$\endgroup\$
    – ZaMoC
    Jun 11, 2017 at 14:12
  • \$\begingroup\$ Testcases say [348,51] should return 0 but I'm getting 56/3 when I run this...? \$\endgroup\$ Jun 12, 2017 at 3:07
  • \$\begingroup\$ Welp, looks like it's working now. Not sure what Mathematica was doing before... \$\endgroup\$ Jun 12, 2017 at 7:19
6
\$\begingroup\$

JavaScript (ES6), 50 bytes

Fixed Stolen from ovs' solution

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

Explanation

f=(a,b,c=0)=>                                      Function taking two numbers and optional carry
             a|b|c                                 If a number or the carry are nonzero
                  &&                               Then
                    c+f(a/10,b/10,a%10+b%10+c>=10) Return carry plus all other carries

Carry explanation

a%10+b%10+c     Sum of mod 10 of the numbers and c - remember these are not floordiv'ed
           >=10 Greater than or equals to 10 (we can't use greater than 9 here)

f=(a,b,c=0)=>a|b|c&&c+f(a/10,b/10,a%10+b%10+c>=10)
console.log([[999,1],[398,91],[348,51],[348,52],[5,15],[999,999],[256,64],[190192,90909]].map(test=>`${(test[0]+'').padStart(6,' ')}, ${(test[1]+'').padStart(6,' ')} -> ${f(...test)}`).join('\n'));

\$\endgroup\$
2
  • 1
    \$\begingroup\$ 348 , 52 should be 2 \$\endgroup\$
    – Toto
    Jun 11, 2017 at 11:46
  • \$\begingroup\$ How does this work? Could you add an explanation? \$\endgroup\$
    – Arjun
    Jun 11, 2017 at 12:28
5
\$\begingroup\$

C (gcc), 65 bytes

i,l;f(a,b){for(i=l=0;a+b;a/=10,b/=10)i+=a%10+b%10+l>9?l=1:0;a=i;}

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ You don't need to initialise global variables. \$\endgroup\$ Jun 11, 2017 at 11:02
  • \$\begingroup\$ @user1502040 you need to, if they're used inside the function without being initialized. \$\endgroup\$
    – Leaky Nun
    Jun 11, 2017 at 11:05
  • 1
    \$\begingroup\$ Just getting my head around the initialization here: the variables are zero-initialized automatically, but only once, so because function submissions on PPCG have to work if the function's run more than once, they need to be manually zeroed for the benefit of the second and subsequent runs. \$\endgroup\$
    – user62131
    Jun 11, 2017 at 17:50
5
\$\begingroup\$

Jelly,  13 12 11  9 bytes

-1 byte by porting Jenny_mathy's Mathematic answer.
-2 more bytes by better golfing :p

;SN$DFS:9

See the test suite.

How?

;SN$DFS:9 - Main link: list of numbers, [a,b]     e.g.   [348,53]
   $      - last two links as a monad
 S        -   sum                                            401
  N       -   negate                                        -401
;         - concatenate                             [348,53,-401] 
    D     - convert to decimal lists     [[3,4,8],[5,3],[-4,0,-1]]
     F    - flatten                           [3,4,8,5,3,-4,0,-1]
      S   - sum                                               18
       :9 - integer divide by nine                             2

My 12 byte solution...

:⁵+
DUSç\>9S

A monadic link taking a pair of integers and returning the number of carries as an integer.

There is probably a shorter way though! There was!

Try it online! or see the test suite.

How

:⁵+ · Link 1: perform a carry: right-column's-digit-sum, a; left-colum's-digit-sum; b
 ⁵  · literal 10
:   · a integer-divided by 10 - the carry amount
  + · add to b

DUSç\>9S · Main link: list of summands        e.g. [348,52]
D        · convert to decimal lists                [[3,4,8],[5,2]]
 U       · upend (reverse each)                    [[8,4,3],[2,5]]
  S      · sum (add the digits up)                 [10,9,3]
    \    · cumulative reduce with:
   ç     ·   last link (1) as a dyad               [10,10,4]
      9  · literal 9
     >   · greater than?                           [ 1, 1,0]
       S · sum                                     2
\$\endgroup\$
1
  • \$\begingroup\$ The many uses of D and S... \$\endgroup\$ Jun 11, 2017 at 17:29
4
\$\begingroup\$

Python, 48 bytes

f=lambda a,b,m=1:m<1e99and(~a%m<b%m)+f(a,b,m*10)

Try it online!

For each place value m=1, 10, 100, ..., 10**99, checks if there is a carry at that place value. The overflow check a%m+b%m>=m is shortened to ~a%m<b%m.

A nicer 45-byte variant where floats a and b shifted down instead

f=lambda a,b:a+b and(a%1+b%1>=1)+f(a/10,b/10)

sadly runs into float precision issues.

\$\endgroup\$
3
  • \$\begingroup\$ Can't you use a+b<m as your terminating condition? \$\endgroup\$
    – Neil
    Jun 12, 2017 at 18:25
  • \$\begingroup\$ @Neil It needs to be <= which is longer. \$\endgroup\$
    – xnor
    Jun 12, 2017 at 19:12
  • \$\begingroup\$ 1e99and is nasty. \$\endgroup\$ Jun 12, 2017 at 19:24
4
\$\begingroup\$

JavaScript (ES6), 53 45 bytes

f=(a,b,d=1)=>a+b<d?0:(a%d+b%d>=d)+f(a,b,d*10)

Saved 1 byte by adding an extra do-nothing iteration for carries into 1's place. Saved 7 bytes by appropriating @xnor's carry check. I also had a more elegant 45-byte version but it suffers from floating-point inaccuracy; it would work great translated to a language with exact decimal arithmetic:

f=(a,b,c=a+b)=>c<1?0:a%1+b%1-c%1+f(a/10,b/10)
\$\endgroup\$
3
\$\begingroup\$

Python 2, 55 bytes

f=lambda a,b,c=0:c+a+b and c+f(a/10,b/10,a%10+b%10+c>9)

Try it online!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ As @JonathanAllan pointed out, if you used a call to your function, you must declare it as well. That being said, your answer is 55 bytes \$\endgroup\$
    – Mr. Xcoder
    Jun 11, 2017 at 12:18
  • \$\begingroup\$ @Mr.Xcoder fixed it \$\endgroup\$
    – ovs
    Jun 11, 2017 at 14:30
2
\$\begingroup\$

05AB1E, 11 10 bytes

|DO‚€SOÆ9÷

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ @JonathanAllan Fixed. \$\endgroup\$
    – Okx
    Jun 12, 2017 at 9:52
  • \$\begingroup\$ |DO‚€SOÆ9÷ for 10 bytes. \$\endgroup\$
    – Emigna
    Jun 12, 2017 at 10:49
  • \$\begingroup\$ @Emigna That's pretty cool. \$\endgroup\$
    – Okx
    Jun 12, 2017 at 10:50
2
\$\begingroup\$

Neim, 10 bytes

𝔸𝐣𝐬₁₂𝔻𝐬𝕊λ𝕍

Explanation:

𝔸            𝔸ppend the two inputs into a list
 𝐣            𝐣oin them together
  𝐬           𝐬um the characters
   ₁₂         Push the first input, then the second
     𝔻        A𝔻d.
      𝐬       𝐬um the characters
       𝕊      𝕊ubtract
         𝕍    Integer di𝕍ision by
        λ     nine (variable)

Try it!

Alternative solution, also 10 bytes:

𝔸D𝐣𝐬S𝐬𝐬𝕊9𝕍

Explanation:

𝔸             𝔸ppend the two inputs into a list
 D            Duplicate
  𝐣            𝐣oin
   𝐬           𝐬um
    S          Swap
     𝐬         𝐬um
      𝐬        𝐬um the characters
       𝕊       𝕊ubtract
         𝕍     integer di𝕍ide by
        λ       nine (variable)

Try it!

\$\endgroup\$
1
\$\begingroup\$

PHP>=7.1, 81 Bytes

for([,$x,$y]=$argv,$i=1;($x+$y)/$i|0;$i*=10)$d+=$c=$x/$i%10+$y/$i%10+$c>9;echo$d;

-2 Bytes removing |0 In this case the loop runs until $i is INF

Test Cases

\$\endgroup\$
2
  • \$\begingroup\$ When does $i become INF? \$\endgroup\$
    – cat
    Jun 11, 2017 at 15:45
  • \$\begingroup\$ @cat I am not sure why it is important. I have not use this. 1.0E+309is the first INF value Try it online! \$\endgroup\$ Jun 11, 2017 at 15:53
0
\$\begingroup\$

Braingolf, 20 bytes

VR{.M}d<d&+v+d&+c-9/

Try it online!

Uses the same method as everyone else.

Could've saved a byte or 2 had I had the foresight to allow d to use the greedy modifier, then I could've replaced d<d with &d ah well, next time.

Explanation

VR{.M}d<d&+v+d&+c-9/  Implicit input from commandline args
VR                    Create stack2 and return to stack1
  {..}                Foreach loop, runs on each item in stack1
   .M                 Duplicate and move duplicate to stack2
      d<d             Split both items on stack into digits
         &+           Sum entire stack
           v+         Switch to stack2 and sum last 2 items on stack
             d&+      Split result into digits and sum all digits
                c     Collapse stack2 into stack1
                 -    Subtract last item from 2nd to last
                  9/  Divide by 9
                      Implicit output of last item on stack
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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