JavaScript Program to Divide Large Number Represented as String
Last Updated :
31 Oct, 2023
In this article, we will explore dividing large numbers represented as strings in JavaScript. Dividing a large number represented as a string in JavaScript is a common computational task that involves dividing a numerical line by another numerical value.
Approach 1: Brute Force
We will use basic maths, as the dividend and result can be huge we store them in string. We first take digits which are divisible by a number. After this take each digit and store the result in a string.
- This JavaScript program simulates long division to calculate the quotient of two numbers, dividend, and divisor, without using the division operator.
- It iteratively processes each digit in the dividend, determining the quotient digit for each step and updating the remainder.
- The result is stored as a string and returned, making it a versatile solution for division operations, especially when precise division is required or when dealing with large numbers.
- This approach effectively emulates manual long division, enabling the computation of the quotient without using the division operator and is particularly useful when dealing with large numbers or precision-sensitive calculations.
Syntax:
function divide(dividend, divisor) {
// Implementation
return result;
}
Example: Below is the implementation of the program.
Javascript
function divide(dividend, divisor) {
if (divisor === 0) {
throw new Error(
"Division by zero is not allowed."
);
}
let quotient = "" ;
let currentIndex = 0;
let currentDividendPart =
dividend[currentIndex] - "0" ;
while (currentDividendPart < divisor) {
currentDividendPart =
currentDividendPart * 10 +
dividend[currentIndex + 1].
charCodeAt(0) - "0" .charCodeAt(0);
currentIndex += 1;
}
currentIndex += 1;
while (dividend.length > currentIndex) {
quotient += String.fromCharCode(
Math.floor(currentDividendPart / divisor)
+ "0" .charCodeAt(0)
);
currentDividendPart =
(currentDividendPart % divisor) * 10 +
dividend[currentIndex].charCodeAt(0) -
"0" .charCodeAt(0);
currentIndex += 1;
}
quotient += String.fromCharCode(
Math.floor(currentDividendPart / divisor)
+ "0" .charCodeAt(0)
);
if (quotient.length === 0) {
return "0" ;
}
return quotient;
}
let dividend = "1322145464651" ;
let divisor = 125;
console.log(divide(dividend, divisor));
|
Appraoch 2: Using BigInt() in JavaScript
- Convert the input strings
num1
and num2
to BigInt.
- Check for division by zero (if
bigint2
is zero), and throw an error if necessary.
- Perform the division using the
/
operator between bigint1
and bigint2
, and store the result.
- Return the result of the division as a string.
Syntax:
const bigint1 = BigInt(num1);
const bigint2 = BigInt(num2);
Example: Below is the implementation of the above approach.
Javascript
function divideLargeNumbers(num1, num2) {
const bigint1 = BigInt(num1);
const bigint2 = BigInt(num2);
if (bigint2 === 0) {
throw new Error( "Division by zero is not allowed." );
}
const result = bigint1 / bigint2;
return result.toString();
}
const num1 = "1322145464651" ;
const num2 = "125" ;
const result = divideLargeNumbers(num1, num2);
console.log(result);
|
- Parse the input strings
num1
and num2
to regular numbers using parseInt()
.
- Check for division by zero by verifying if
number2
is equal to zero, and throw an error if it is.
- Perform the division using the
/
operator between number1
and number2
, and store the result.
- Convert the result to a string, split it at the decimal point, and keep only the integer part for the final result. Return this integer part as a string.
Syntax:
const number1 = parseInt(num1, 10);
const number2 = parseInt(num2, 10);
Example: Below is the implementation of the above approach.
Javascript
function divideLargeNumbers(num1, num2) {
const number1 = parseInt(num1, 10);
const number2 = parseInt(num2, 10);
if (number2 === 0) {
throw new Error( "Division by zero is not allowed." );
}
const result = number1 / number2;
return result.toString().split( '.' )[0];
}
const num1 = "1322145464651" ;
const num2 = "125" ;
const result = divideLargeNumbers(num1, num2);
console.log(result);
|
Share your thoughts in the comments
Please Login to comment...