Python for arbitrarily large numbers represented as strings:
def large_sum(a: str, b: str) -> str: if len(b) < len(a): return large_sum(b, a) alist = [int(c) for c in reversed(a)] blist = [int(c) for c in reversed(b)] for _ in range(0, len(alist) - len(blist)): blist.append(0) overflow = 0 output = [] for ax, bx in zip(alist, blist): sumval = ax + bx + overflow overflow = int((sumval) / 10) output.append(sumval % 10) if overflow != 0: output.append(overflow) return "".join(str(i) for i in reversed(output)) print(large_sum("1234", "3456")) print( large_sum( "1234567890123456789012345678901234567890", "456789012345678901234567890123456789012345678901234567890123", ) ) print(large_sum("44", "2"))
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Python for arbitrarily large numbers represented as strings: