# How many carries do I need to add these two numbers?

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.

• – Leaky Nun Jun 11 '17 at 10:31
• Suggested test case: 190192, 90909 (has a break in the carries). – Jonathan Allan Jun 11 '17 at 11:14
• 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. – JungHwan Min Jun 11 '17 at 14:14
• Related anarchy endless challenge – xnor Jun 11 '17 at 17:00
• 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? – xnor Jun 11 '17 at 17:04

# Mathematica, 46 39 bytes

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


input

[348,51]

-7 bytes from JungHwan

• 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. – JungHwan Min Jun 11 '17 at 14:10
• Me too! thanks for the golfing tips – J42161217 Jun 11 '17 at 14:12
• Testcases say [348,51] should return 0 but I'm getting 56/3 when I run this...? – numbermaniac Jun 12 '17 at 3:07
• Welp, looks like it's working now. Not sure what Mathematica was doing before... – numbermaniac Jun 12 '17 at 7:19

# 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')); • 348 , 52 should be 2 – Toto Jun 11 '17 at 11:46 • How does this work? Could you add an explanation? – Arjun Jun 11 '17 at 12:28 # 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! • You don't need to initialise global variables. – user1502040 Jun 11 '17 at 11:02 • @user1502040 you need to, if they're used inside the function without being initialized. – Leaky Nun Jun 11 '17 at 11:05 • 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. – user62131 Jun 11 '17 at 17:50 # 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


### 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

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:
9  · literal 9
>   · greater than?                           [ 1, 1,0]
S · sum                                     2

• The many uses of D and S... – Erik the Outgolfer Jun 11 '17 at 17:29

# 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.

• Can't you use a+b<m as your terminating condition? – Neil Jun 12 '17 at 18:25
• @Neil It needs to be <= which is longer. – xnor Jun 12 '17 at 19:12
• 1e99and is nasty. – Jonas Schäfer Jun 12 '17 at 19:24

## 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)


# 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!

• 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 – Mr. Xcoder Jun 11 '17 at 12:18
• @Mr.Xcoder fixed it – ovs Jun 11 '17 at 14:30

# 05AB1E, 11 10 bytes

|DO‚€SOÆ9÷


Try it online!

• @JonathanAllan Fixed. – Okx Jun 12 '17 at 9:52
• |DO‚€SOÆ9÷ for 10 bytes. – Emigna Jun 12 '17 at 10:49
• @Emigna That's pretty cool. – Okx Jun 12 '17 at 10:50

# 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!

# 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 • When does $i become INF? – cat Jun 11 '17 at 15:45
• @cat I am not sure why it is important. I have not use this. 1.0E+309is the first INF value Try it online! – Jörg Hülsermann Jun 11 '17 at 15:53

# 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