DEV Community

dev.to staff
dev.to staff

Posted on

13 5

Daily Challenge #143 - Big Arithmetic

Doing arithmetic with big numbers is impossible to do with regular integer types. In JavaScript (which represents numbers as 64-bit floats), anything beyond 2^53-1 becomes increasingly less accurate.

Write a two functions (bigAdd and bigSub) to accurately represent these large integers as strings. They will both take two arguments: either a valid representation of an integer as a string, or a regular number. They will return the correct answer as a string. bigAdd will sum, bigSub will subtract.

For example:
bigAdd(1, "123456789012345678901234567890") === "123456789012345678901234567891";
bigSub("123456789012345678901234567890", 1) === "123456789012345678901234567889";

Remember, the values could be negative, and so the calculations should be made accordingly.
bigAdd(-1, "123456789012345678901234567890") === "123456789012345678901234567889";
bigSub("123456789012345678901234567890", -1) === "123456789012345678901234567891";

Tests


bigAdd(1, "123456789012345678901234567890")
bigAdd(-1, "123456789012345678901234567890")
bigSub("123456789012345678901234567890", 1)
bigSub("123456789012345678901234567890", -1)

This challenge comes from wthit56 on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

Warp.dev image

The best coding agent. Backed by benchmarks.

Warp outperforms every other coding agent on the market, and gives you full control over which model you use. Get started now for free, or upgrade and unlock 2.5x AI credits on Warp's paid plans.

Download Warp

Top comments (4)

Collapse
 
yechielk profile image
Yechiel Kalmenson

Thankfully, Ruby automatically converts ints to Bignums when it detects an overflow, so the Ruby solution is as simple as:

def big_add(a, b)
  (a.to_i + b.to_i).to_s
end

def big_sub(a, b)
  (a.to_i - b.to_i).to_s
end
Collapse
 
craigmc08 profile image
Craig McIlwrath

Haskell

bigAdd :: Integer -> Integer -> Integer
bigAdd = (+) 

bigSub :: Integer -> Integer -> Integer
bigSub = (-) 

It isn't feaaible/doesn't make sense to define similar methods that take various combinations of strings and integers. Especially in a language with support for literals of arbitrarily sized integers.

Collapse
 
vaibhavyadav1998 profile image
Vaibhav Yadav

In JavaScript.

const bigAdd = (a, b) => (BigInt(a) + BigInt(b)).toString();
const bigSub = (a, b) => (BigInt(a) - BigInt(b)).toString();
Collapse
 
cssrajput profile image
Chandra Shekhar

Java :)

import java.math.BigInteger;

public class BigArithmetic {

public static void main(String[] args) {
    System.out.println(bigAdd(1, "123456789012345678901234567890"));
    System.out.println(bigAdd(-1, "123456789012345678901234567890"));
    System.out.println(bigSub("123456789012345678901234567890", 1));
    System.out.println(bigSub("123456789012345678901234567890", -1));
}

private static String bigSub(Object a, Object b) {
    return new BigInteger(a.toString()).add(new BigInteger(b.toString())).toString();
}

private static String bigAdd(Object a, Object b) {
    return new BigInteger(a.toString()).add(new BigInteger(b.toString())).toString();
}

}

Feature flag article image

Create a feature flag in your IDE in 5 minutes with LaunchDarkly’s MCP server 🏁

How to create, evaluate, and modify flags from within your IDE or AI client using natural language with LaunchDarkly's new MCP server. Follow along with this tutorial for step by step instructions.

Read full post

👋 Kindness is contagious

Explore this insightful write-up embraced by the inclusive DEV Community. Tech enthusiasts of all skill levels can contribute insights and expand our shared knowledge.

Spreading a simple "thank you" uplifts creators—let them know your thoughts in the discussion below!

At DEV, collaborative learning fuels growth and forges stronger connections. If this piece resonated with you, a brief note of thanks goes a long way.

Okay