MCPcopy Create free account
hub / github.com/TheAlgorithms/Go / add

Function add

project_euler/problem_13/problem13.go:124–150  ·  view source on GitHub ↗
(a, b string)

Source from the content-addressed store, hash-verified

122}
123
124func add(a, b string) string {
125 if len(a) < len(b) {
126 a, b = b, a
127 }
128
129 carry := 0
130 sum := make([]byte, len(a)+1)
131
132 for i := 0; i < len(a); i++ {
133 d := int(a[len(a)-1-i] - '0')
134 if i < len(b) {
135 d += int(b[len(b)-1-i] - '0')
136 }
137 d += carry
138
139 sum[len(sum)-1-i] = byte(d%10) + '0'
140 carry = d / 10
141 }
142
143 if carry > 0 {
144 sum[0] = byte(carry) + '0'
145 } else {
146 sum = sum[1:]
147 }
148
149 return string(sum)
150}

Callers 1

Problem13Function · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected