Given a large number (represented as a string) which has to divide by another number (represented as int data type). The large number can be very large which does not even fit in long long in C++. The task is to find the division of these numbers.
Examples:
Input : number = 1260257
divisor = 37
Output : 34061
(See below diagram)
Input : number = 12313413534672234
divisor = 754
Output : 16330787181262
Input : number = 1248163264128256512
divisor = 125
Output : 9985306113026052
We have already discussed Multiply Large Numbers represented as Strings.
We use basic school mathematics as shown in below example.
As the dividend and result can be very large we store them in string. We first take digits which are divisible by number. After this take each digit and store result in string.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
string longDivision(string number, int divisor)
{
string ans;
int idx = 0;
int temp = number[idx] - '0' ;
while (temp < divisor)
temp = temp * 10 + (number[++idx] - '0' );
while (number.size() > idx) {
ans += (temp / divisor) + '0' ;
temp = (temp % divisor) * 10 + number[++idx] - '0' ;
}
if (ans.length() == 0)
return "0" ;
return ans;
}
int main()
{
string number = "1248163264128256512" ;
int divisor = 125;
cout << longDivision(number, divisor);
return 0;
}
|
Java
import java.io.*;
public class GFG {
public static String longDivision(
String number,
int divisor)
{
StringBuilder result
= new StringBuilder();
char [] dividend
= number.toCharArray();
int carry = 0 ;
for (
int i = 0 ;
i < dividend.length; i++) {
int x
= carry * 10
+ Character.getNumericValue(
dividend[i]);
result.append(x / divisor);
carry = x % divisor;
}
for (
int i = 0 ;
i < result.length(); i++) {
if (
result.charAt(i) != '0' ) {
return result.substring(i);
}
}
return "" ;
}
public static void main(
String[] args)
{
String number
= "1248163264128256512" ;
int divisor = 125 ;
System.out.println(
longDivision(
number, divisor));
}
}
|
Python3
import math
def longDivision(number, divisor):
ans = "";
idx = 0 ;
temp = ord (number[idx]) - ord ( '0' );
while (temp < divisor):
temp = (temp * 10 + ord (number[idx + 1 ]) -
ord ( '0' ));
idx + = 1 ;
idx + = 1 ;
while (( len (number)) > idx):
ans + = chr (math.floor(temp / / divisor) + ord ( '0' ));
temp = ((temp % divisor) * 10 + ord (number[idx]) -
ord ( '0' ));
idx + = 1 ;
ans + = chr (math.floor(temp / / divisor) + ord ( '0' ));
if ( len (ans) = = 0 ):
return "0" ;
return ans;
number = "1248163264128256512" ;
divisor = 125 ;
print (longDivision(number, divisor));
|
C#
using System;
class GFG {
static string longDivision( string number, int divisor)
{
string ans = "" ;
int idx = 0;
int temp = ( int )(number[idx] - '0' );
while (temp < divisor) {
temp = temp * 10 + ( int )(number[idx + 1] - '0' );
idx++;
}
++idx;
while (number.Length > idx) {
ans += ( char )(temp / divisor + '0' );
temp = (temp % divisor) * 10 + ( int )(number[idx] - '0' );
idx++;
}
ans += ( char )(temp / divisor + '0' );
if (ans.Length == 0)
return "0" ;
return ans;
}
static void Main()
{
string number = "1248163264128256512" ;
int divisor = 125;
Console.WriteLine(longDivision(number, divisor));
}
}
|
PHP
<?php
function longDivision( $number , $divisor )
{
$ans = "" ;
$idx = 0;
$temp = ord( $number [ $idx ]) - 48;
while ( $temp < $divisor )
$temp = $temp * 10 +
ord( $number [++ $idx ]) - 48;
++ $idx ;
while ( strlen ( $number ) > $idx )
{
$ans .= chr ((int)( $temp / $divisor ) + 48);
$temp = ( $temp % $divisor ) * 10 +
ord( $number [ $idx ]) - 48;
++ $idx ;
}
$ans .= chr ((int)( $temp / $divisor ) + 48);
if ( strlen ( $ans ) == 0)
return "0" ;
return $ans ;
}
$number = "1248163264128256512" ;
$divisor = 125;
print (longDivision( $number , $divisor ));
?>
|
Javascript
<script>
function longDivision(number,divisor)
{
let ans= "" ;
let idx = 0;
let temp=number[idx]- '0' ;
while (temp < divisor)
{
temp = (temp * 10 +
(number[idx + 1]).charCodeAt(0) -
( '0' ).charCodeAt(0));
idx += 1;
}
idx += 1;
while (number.length>idx)
{
ans += String.fromCharCode
(Math.floor(temp / divisor) +
( '0' ).charCodeAt(0));
temp = ((temp % divisor) * 10 +
(number[idx]).charCodeAt(0) -
( '0' ).charCodeAt(0));
idx += 1;
}
ans += String.fromCharCode
(Math.floor(temp / divisor) +
( '0' ).charCodeAt(0));
if (ans.length==0)
return "0" ;
return ans;
}
let number = "1248163264128256512" ;
let divisor = 125;
document.write(longDivision(
number, divisor));
</script>
|
Time Complexity: O(n2!), where n denoting length of string.
Auxiliary Space: O(n).
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!