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

27

3

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.

Leaky Nun

Posted 2017-06-11T10:31:29.730

Reputation: 45 011

Related. – Leaky Nun – 2017-06-11T10:31:35.367

14Suggested test case: 190192, 90909 (has a break in the carries). – Jonathan Allan – 2017-06-11T11:14:31.677

5

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 – 2017-06-11T14:14:50.623

Related anarchy endless challenge – xnor – 2017-06-11T17:00:52.973

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 – 2017-06-11T17:04:24.087

@xnor yes, you can. – Leaky Nun – 2017-06-11T17:18:29.393

Answers

21

Mathematica, 46 39 bytes

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

input

[348,51]

-7 bytes from JungHwan

J42161217

Posted 2017-06-11T10:31:29.730

Reputation: 15 931

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 – 2017-06-11T14:10:09.297

Me too! thanks for the golfing tips – J42161217 – 2017-06-11T14:12:38.277

Testcases say [348,51] should return 0 but I'm getting 56/3 when I run this...? – numbermaniac – 2017-06-12T03:07:52.463

Welp, looks like it's working now. Not sure what Mathematica was doing before... – numbermaniac – 2017-06-12T07:19:33.147

6

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'));

ASCII-only

Posted 2017-06-11T10:31:29.730

Reputation: 4 687

1348 , 52 should be 2 – Toto – 2017-06-11T11:46:57.773

How does this work? Could you add an explanation? – Arjun – 2017-06-11T12:28:59.930

5

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!

user41805

Posted 2017-06-11T10:31:29.730

Reputation: 16 320

You don't need to initialise global variables. – user1502040 – 2017-06-11T11:02:50.460

@user1502040 you need to, if they're used inside the function without being initialized. – Leaky Nun – 2017-06-11T11:05:59.940

1Just 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. – None – 2017-06-11T17:50:51.640

5

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

Jonathan Allan

Posted 2017-06-11T10:31:29.730

Reputation: 67 804

The many uses of D and S... – Erik the Outgolfer – 2017-06-11T17:29:02.457

4

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)

Neil

Posted 2017-06-11T10:31:29.730

Reputation: 95 035

4

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.

xnor

Posted 2017-06-11T10:31:29.730

Reputation: 115 687

Can't you use a+b<m as your terminating condition? – Neil – 2017-06-12T18:25:36.880

@Neil It needs to be <= which is longer. – xnor – 2017-06-12T19:12:20.117

1e99and is nasty. – Jonas Schäfer – 2017-06-12T19:24:42.893

3

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!

ovs

Posted 2017-06-11T10:31:29.730

Reputation: 21 408

1As @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 – 2017-06-11T12:18:17.687

@Mr.Xcoder fixed it – ovs – 2017-06-11T14:30:44.197

2

05AB1E, 11 10 bytes

|DO‚€SOÆ9÷

Try it online!

Okx

Posted 2017-06-11T10:31:29.730

Reputation: 15 025

@JonathanAllan Fixed. – Okx – 2017-06-12T09:52:41.730

|DO‚€SOÆ9÷ for 10 bytes. – Emigna – 2017-06-12T10:49:32.490

@Emigna That's pretty cool. – Okx – 2017-06-12T10:50:46.203

2

Neim, 10 bytes

₁₂λ

Explanation:

            ppend the two inputs into a list
             oin them together
             um the characters
   ₁₂         Push the first input, then the second
             Ad.
             um the characters
             ubtract
             Integer diision by
        λ     nine (variable)

Try it!

Alternative solution, also 10 bytes:

DS9

Explanation:

             ppend the two inputs into a list
 D            Duplicate
              oin
              um
    S          Swap
              um
              um the characters
              ubtract
              integer diide by
        λ       nine (variable)

Try it!

Okx

Posted 2017-06-11T10:31:29.730

Reputation: 15 025

1

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

Jörg Hülsermann

Posted 2017-06-11T10:31:29.730

Reputation: 13 026

When does $i become INF? – cat – 2017-06-11T15:45:41.803

@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 – 2017-06-11T15:53:44.253

0

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

Skidsdev

Posted 2017-06-11T10:31:29.730

Reputation: 9 656