Repository: TheAlgorithms/Java
Pull request #3278 (2022-09-19 12:37:31)

fix: Prime factorization algorithm
### **Describe your change:**

- [ ] Add an algorithm?
- [x] Fix a bug or typo in an existing algorithm?
- [ ] Documentation change?

#### References
[#3276](https://github.com/TheAlgorithms/Java/issues/3276)

<!-- Add any reference to previous pull-request or issue -->

### **Checklist:**

<!-- Remove items that do not apply. 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.
- [x] I know that pull requests will not be merged if they fail the automated tests.
- [x] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
- [x] All new Java files are placed inside an existing directory.
- [x] All filenames are in all uppercase characters with no spaces or dashes.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
- [x] If this pull request resolves one or more open issues then the commit message contains `Fixes: #{$ISSUE_NO}`.



Patch:
diff --git a/src/main/java/com/thealgorithms/maths/PrimeFactorization.java b/src/main/java/com/thealgorithms/maths/PrimeFactorization.java
index ca6f1263..44c82afb 100644
--- a/src/main/java/com/thealgorithms/maths/PrimeFactorization.java
+++ b/src/main/java/com/thealgorithms/maths/PrimeFactorization.java
@@ -1,35 +1,39 @@
 package com.thealgorithms.maths;
 
-import java.util.Scanner;
+/*
+ * Authors: 
+ * (1) Aitor Fidalgo Sánchez (https://github.com/aitorfi) 
+ * (2) Akshay Dubey (https://github.com/itsAkshayDubey)
+*/
+
+import java.util.ArrayList;
+import java.util.List;
 
 public class PrimeFactorization {
 
-    public static void main(String[] args) {
-        System.out.println("## all prime factors ##");
-        Scanner scanner = new Scanner(System.in);
-        System.out.print("Enter a number: ");
-        int n = scanner.nextInt();
-        System.out.print(("printing factors of " + n + " : "));
-        pfactors(n);
-        scanner.close();
-    }
-
-    public static void pfactors(int n) {
-
-        while (n % 2 == 0) {
-            System.out.print(2 + " ");
-            n /= 2;
-        }
-
-        for (int i = 3; i <= Math.sqrt(n); i += 2) {
-            while (n % i == 0) {
-                System.out.print(i + " ");
-                n /= i;
-            }
-        }
-
-        if (n > 2) {
-            System.out.print(n);
-        }
-    }
+	public static List<Integer> pfactors(int n) {
+		
+		List<Integer> primeFactors = new ArrayList<>();
+
+		if (n == 0) {
+			return primeFactors;
+		}
+
+		while (n % 2 == 0) {
+			primeFactors.add(2);
+			n /= 2;
+		}
+
+		for (int i = 3; i <= Math.sqrt(n); i += 2) {
+			while (n % i == 0) {
+				primeFactors.add(i);
+				n /= i;
+			}
+		}
+
+		if (n > 2) {
+			primeFactors.add(n);
+		}
+		return primeFactors;
+	}
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/maths/PrimeFactorizationTest.java b/src/test/java/com/thealgorithms/maths/PrimeFactorizationTest.java
new file mode 100644
index 00000000..3f21a515
--- /dev/null
+++ b/src/test/java/com/thealgorithms/maths/PrimeFactorizationTest.java
@@ -0,0 +1,36 @@
+package com.thealgorithms.maths;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+import java.util.List;
+
+import org.junit.jupiter.api.Test;
+
+class PrimeFactorizationTest {
+
+	@Test
+	void testpFactorsMustReturnEmptyList() {
+		//given
+		int n = 0; 
+		
+		//then
+		assertTrue(PrimeFactorization.pfactors(n).isEmpty());
+	}
+	
+	@Test
+	void testpFactorsMustReturnNonEmptyList() {
+		//given
+		int n = 198;
+		int expectedListSize = 4;
+		
+		//when
+		List<Integer> actualResultList = PrimeFactorization.pfactors(n);
+		
+		//then
+		assertEquals(expectedListSize, actualResultList.size());
+		assertEquals(2, actualResultList.get(0));
+		assertEquals(3, actualResultList.get(1));
+		assertEquals(3, actualResultList.get(2));
+		assertEquals(11, actualResultList.get(3));
+	}
+}