Skip to content

Commit 1c7da7a

Browse files
authored
Add LongDivision (TheAlgorithms#3691)
1 parent 37a1659 commit 1c7da7a

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

DIRECTORY.md

+3
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@
276276
* [LeonardoNumber](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/LeonardoNumber.java)
277277
* [LinearDiophantineEquationsSolver](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/LinearDiophantineEquationsSolver.java)
278278
* [LiouvilleLambdaFunction](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/LiouvilleLambdaFunction.java)
279+
* [LongDivision](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/LongDivision.java)
279280
* [LucasSeries](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/LucasSeries.java)
280281
* [MagicSquare](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/MagicSquare.java)
281282
* [MatrixUtil](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/MatrixUtil.java)
@@ -567,11 +568,13 @@
567568
* [FindMinTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/FindMinTest.java)
568569
* [GaussianTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/GaussianTest.java)
569570
* [GCDTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/GCDTest.java)
571+
* [HarshadNumberTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/HarshadNumberTest.java)
570572
* [HeronsFormulaTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/HeronsFormulaTest.java)
571573
* [JosephusProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/JosephusProblemTest.java)
572574
* [KaprekarNumbersTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/KaprekarNumbersTest.java)
573575
* [LeastCommonMultipleTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/LeastCommonMultipleTest.java)
574576
* [LiouvilleLambdaFunctionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/LiouvilleLambdaFunctionTest.java)
577+
* [LongDivisionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/LongDivisionTest.java)
575578
* [MobiusFunctionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/MobiusFunctionTest.java)
576579
* [PascalTriangleTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/PascalTriangleTest.java)
577580
* [PerfectCubeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/PerfectCubeTest.java)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Given two integers dividend and divisor, divide two integers without using multiplication, division, and mod operator.
2+
//
3+
// The integer division should truncate toward zero, which means losing its fractional part. For example, 8.345 would be truncated to 8,
4+
// and -2.7335 would be truncated to -2.
5+
// My method used Long Division, here is the source "https://en.wikipedia.org/wiki/Long_division"
6+
7+
package com.thealgorithms.maths;
8+
9+
public class LongDivision {
10+
public static int divide(int dividend, int divisor) {
11+
long new_dividend_1 = dividend;
12+
long new_divisor_1 = divisor;
13+
14+
if (dividend < 0) {
15+
new_dividend_1 = new_dividend_1 * -1;
16+
}
17+
if (divisor < 0) {
18+
new_divisor_1 = new_divisor_1 * -1;
19+
}
20+
21+
if (dividend == 0 || new_dividend_1 < new_divisor_1) {
22+
return 0;
23+
}
24+
25+
StringBuilder answer = new StringBuilder();
26+
27+
String dividend_string = "" + new_dividend_1;
28+
int last_index = 0;
29+
30+
String remainder = "";
31+
32+
33+
for (int i = 0; i < dividend_string.length(); i++) {
34+
String part_v1 = remainder + "" + dividend_string.substring(last_index, i + 1);
35+
long part_1 = Long.parseLong(part_v1);
36+
if (part_1 > new_divisor_1) {
37+
int quotient = 0;
38+
while (part_1 >= new_divisor_1) {
39+
part_1 = part_1 - new_divisor_1;
40+
quotient++;
41+
}
42+
answer.append(quotient);
43+
} else if (part_1 == new_divisor_1) {
44+
int quotient = 0;
45+
while (part_1 >= new_divisor_1) {
46+
part_1 = part_1 - new_divisor_1;
47+
quotient++;
48+
}
49+
answer.append(quotient);
50+
} else if (part_1 == 0) {
51+
answer.append(0);
52+
} else if (part_1 < new_divisor_1) {
53+
answer.append(0);
54+
}
55+
if (!(part_1 == 0)) {
56+
remainder = String.valueOf(part_1);
57+
}else{
58+
remainder = "";
59+
}
60+
61+
last_index++;
62+
}
63+
64+
if ((dividend < 0 && divisor > 0) || (dividend > 0 && divisor < 0)) {
65+
try {
66+
return Integer.parseInt(answer.toString()) * (-1);
67+
} catch (NumberFormatException e) {
68+
return -2147483648;
69+
}
70+
}
71+
try {
72+
return Integer.parseInt(answer.toString());
73+
} catch (NumberFormatException e) {
74+
return 2147483647;
75+
}
76+
77+
}
78+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.thealgorithms.maths;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
public class LongDivisionTest {
8+
9+
@Test
10+
void testOne() {
11+
assertEquals(3, LongDivision.divide(10,3));
12+
}
13+
14+
@Test
15+
void testTwo() {
16+
assertEquals(-2, LongDivision.divide(7,-3));
17+
}
18+
19+
20+
@Test
21+
void testThree() {
22+
assertEquals(10, LongDivision.divide(105,10));
23+
}
24+
25+
}

0 commit comments

Comments
 (0)