Repository: TheAlgorithms/Java
Pull request #3691 (2022-10-26 20:54:57)

Create LongDivision.java
It's not a simple division, it's a division which is implemented using Long Division Principle, which saves us a lot of time, where some users approach using while loop.

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.


debasishbsws: Add Some JUnit test
adjamalbek: > 
I did: 
https://github.com/TheAlgorithms/Java/pull/3710
adjamalbek: Test and Main are combined, all the errors were fixed, please kindly review, thank you 
adjamalbek: Requesting review from @siriak and @yanglbme and @debasishbsws for approval and merging.
siriak: Then please call it LongDivision and add a link to Wikipedia
adjamalbek: I have a left a source and short description in the Main Java class and added a comment, also the name of the Class was changed
adjamalbek: This my 1st attempt by using integers only, I can write a code in a future and update the current method, which will accept numbers as strings, cause initially it was and integer division by long division method 

Patch:
diff --git a/DIRECTORY.md b/DIRECTORY.md
index 0345ff4e..e557e92b 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -276,6 +276,7 @@
             * [LeonardoNumber](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/LeonardoNumber.java)
             * [LinearDiophantineEquationsSolver](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/LinearDiophantineEquationsSolver.java)
             * [LiouvilleLambdaFunction](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/LiouvilleLambdaFunction.java)
+            * [LongDivision](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/LongDivision.java)
             * [LucasSeries](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/LucasSeries.java)
             * [MagicSquare](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/MagicSquare.java)
             * [MatrixUtil](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/MatrixUtil.java)
@@ -567,11 +568,13 @@
             * [FindMinTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/FindMinTest.java)
             * [GaussianTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/GaussianTest.java)
             * [GCDTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/GCDTest.java)
+            * [HarshadNumberTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/HarshadNumberTest.java)
             * [HeronsFormulaTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/HeronsFormulaTest.java)
             * [JosephusProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/JosephusProblemTest.java)
             * [KaprekarNumbersTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/KaprekarNumbersTest.java)
             * [LeastCommonMultipleTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/LeastCommonMultipleTest.java)
             * [LiouvilleLambdaFunctionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/LiouvilleLambdaFunctionTest.java)
+            * [LongDivisionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/LongDivisionTest.java)
             * [MobiusFunctionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/MobiusFunctionTest.java)
             * [PascalTriangleTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/PascalTriangleTest.java)
             * [PerfectCubeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/PerfectCubeTest.java)
diff --git a/src/main/java/com/thealgorithms/maths/LongDivision.java b/src/main/java/com/thealgorithms/maths/LongDivision.java
new file mode 100644
index 00000000..88a0a261
--- /dev/null
+++ b/src/main/java/com/thealgorithms/maths/LongDivision.java
@@ -0,0 +1,78 @@
+//        Given two integers dividend and divisor, divide two integers without using multiplication, division, and mod operator.
+//
+//        The integer division should truncate toward zero, which means losing its fractional part. For example, 8.345 would be truncated to 8, 
+//        and -2.7335 would be truncated to -2.
+//        My method used Long Division, here is the source "https://en.wikipedia.org/wiki/Long_division"
+
+package com.thealgorithms.maths;
+
+public class LongDivision {
+public static int divide(int dividend, int divisor) {
+        long new_dividend_1 = dividend;
+        long new_divisor_1 = divisor;
+
+        if (dividend < 0) {
+            new_dividend_1 = new_dividend_1 * -1;
+        }
+        if (divisor < 0) {
+            new_divisor_1 = new_divisor_1 * -1;
+        }
+
+        if (dividend == 0 || new_dividend_1 < new_divisor_1) {
+            return 0;
+        }
+
+        StringBuilder answer = new StringBuilder();
+
+        String dividend_string = "" + new_dividend_1;
+        int last_index = 0;
+
+        String remainder = "";
+
+
+        for (int i = 0; i < dividend_string.length(); i++) {
+            String part_v1 = remainder + "" + dividend_string.substring(last_index, i + 1);
+            long part_1 = Long.parseLong(part_v1);
+            if (part_1 > new_divisor_1) {
+                int quotient = 0;
+                while (part_1 >= new_divisor_1) {
+                    part_1 = part_1 - new_divisor_1;
+                    quotient++;
+                }
+                answer.append(quotient);
+            } else if (part_1 == new_divisor_1) {
+                int quotient = 0;
+                while (part_1 >= new_divisor_1) {
+                    part_1 = part_1 - new_divisor_1;
+                    quotient++;
+                }
+                answer.append(quotient);
+            } else if (part_1 == 0) {
+                answer.append(0);
+            } else if (part_1 < new_divisor_1) {
+                answer.append(0);
+            }
+            if (!(part_1 == 0)) {
+                remainder = String.valueOf(part_1);
+            }else{
+                remainder = "";
+            }
+
+            last_index++;
+        }
+
+        if ((dividend < 0 && divisor > 0) || (dividend > 0 && divisor < 0)) {
+            try {
+                return Integer.parseInt(answer.toString()) * (-1);
+            } catch (NumberFormatException e) {
+                return -2147483648;
+            }
+        }
+        try {
+            return Integer.parseInt(answer.toString());
+        } catch (NumberFormatException e) {
+            return 2147483647;
+        }
+
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/maths/LongDivisionTest.java b/src/test/java/com/thealgorithms/maths/LongDivisionTest.java
new file mode 100644
index 00000000..c35b606a
--- /dev/null
+++ b/src/test/java/com/thealgorithms/maths/LongDivisionTest.java
@@ -0,0 +1,25 @@
+package com.thealgorithms.maths;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+public class LongDivisionTest {
+	
+    @Test
+    void testOne() {
+        assertEquals(3, LongDivision.divide(10,3));
+    }
+  
+      @Test
+    void testTwo() {
+        assertEquals(-2, LongDivision.divide(7,-3));
+    }
+  
+  
+      @Test
+    void testThree() {
+        assertEquals(10, LongDivision.divide(105,10));
+    }
+	
+}