For this challenge, forget everything you know about adding two large numbers together.
Like in this photo, you'll be adding two numbers together...
For further actions, you may consider blocking this person and/or reporting abuse
A quickie in Typescript
Each digit is enumerated in turn by skimming off the ones digit off each number (using
num%10
, then dividing the number by 10) until there are no more digits.The accumulated sum is simply a string with each sum added to its left-hand side.
Testn
You already did the skimming, but you could also avoid stringification altogether: dev.to/qm3ster/comment/14en6
Thanks, that's a fair point. Here's the tweaked code:
Benchmark here -- numeric method has it by a nose most of the time you run it, sometimes a bit more. For smaller inputs they're pretty much even.
In C or Rust or something closer to the metal Mihail's post is definitely the way to go (imo).
In JS strings are pretty quick and numbers are surprisingly slow to deal with (there's no support for integers, etc) and I find the language doesn't really reward that kind of close-knit optimization, so I felt it wasn't worth the additional complication.
I wonder what the result with asm.js would be.
There are some claims that in the process of implementing asm.js V8 in particular just optimized normal code to the speed of corresponding asm.js, but some extra assertions might still help?
EDIT:
I quite liked purely arithmetic solutions by @willsmart and @qm3ster , so I reimplemented same idea in Haskell:
Solution in Haskell:
Essentially
zip
s up digit by digit in reverse order, sums and concats the result.zipRemain
utility function helps retrieving leftover part of the longer of two zipped lists.Example for the zipping problem:
Solution in Bash and some general commands:
This reminds me of the quote: "Whatever you do, there is an asian doing it better."
Nice solution!!
Took a slightly bigger approach, but it works :)
Ruby, because Ruby has built-in
divmod
, because you need both at least as often as you need either.Python 🐍
Not the solution i like but it does the job.
Rust
(without stringification)
// test
(look at it go)
Here is my simple solution with Python:
Old school JS version, not super optimized, but should be faster than, map, join, split etc
Hardcore? Yeah, I'm diggin' into that.
Technically correct, but I tended to think of the solution backward, so it does way more reversing then it should to get things back in place to finish it.