Repository: TheAlgorithms/Java
Pull request #3290 (2022-09-24 16:13:09)

algorithm: Collatz Conjecture
### **Describe your change:**

- [x] Add an algorithm?
- [ ] Fix a bug or typo in an existing algorithm?
- [ ] Documentation change?

#### References
Issue #3283

### **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}`.


amarildoaliaj: I closed the PR by mistake

Patch:
diff --git a/src/main/java/com/thealgorithms/maths/CollatzConjecture.java b/src/main/java/com/thealgorithms/maths/CollatzConjecture.java
new file mode 100644
index 00000000..980dc89b
--- /dev/null
+++ b/src/main/java/com/thealgorithms/maths/CollatzConjecture.java
@@ -0,0 +1,42 @@
+package com.thealgorithms.maths;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * <a href="https://en.wikipedia.org/wiki/Collatz_conjecture">...</a>
+ */
+public class CollatzConjecture {
+
+    /**
+     * Calculate the next number of the sequence.
+     *
+     * @param n current number of the sequence
+     * @return next number of the sequence
+     */
+    public int nextNumber(final int n) {
+        if (n % 2 == 0) {
+            return n / 2;
+        }
+        return 3 * n + 1;
+    }
+
+    /**
+     * Calculate the Collatz sequence of any natural number.
+     *
+     * @param firstNumber starting number of the sequence
+     * @return sequence of the Collatz Conjecture
+     */
+    public List<Integer> collatzConjecture(int firstNumber) {
+        if (firstNumber < 1) {
+            throw new IllegalArgumentException("Must be a natural number");
+        }
+        ArrayList<Integer> result = new ArrayList<>();
+        result.add(firstNumber);
+        while (firstNumber != 1) {
+            result.add(nextNumber(firstNumber));
+            firstNumber = nextNumber(firstNumber);
+        }
+        return result;
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/maths/CollatzConjectureTest.java b/src/test/java/com/thealgorithms/maths/CollatzConjectureTest.java
new file mode 100644
index 00000000..e63a956d
--- /dev/null
+++ b/src/test/java/com/thealgorithms/maths/CollatzConjectureTest.java
@@ -0,0 +1,40 @@
+package com.thealgorithms.maths;
+
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+class CollatzConjectureTest {
+
+    static CollatzConjecture cConjecture;
+
+    @BeforeAll
+    static void setUp() {
+        cConjecture = new CollatzConjecture();
+    }
+
+    @Test
+    void nextNumberFromEvenNumber() {
+        assertEquals(25, cConjecture.nextNumber(50));
+    }
+
+    @Test
+    void nextNumberFromOddNumber() {
+        assertEquals(154, cConjecture.nextNumber(51));
+    }
+
+    @Test
+    void collatzConjecture() {
+        final List<Integer> expected = List.of(35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1);
+        assertIterableEquals(expected, cConjecture.collatzConjecture(35));
+    }
+
+    @Test
+    void sequenceOfNotNaturalFirstNumber() {
+        assertThrows(IllegalArgumentException.class, () -> cConjecture.collatzConjecture(0));
+        assertThrows(IllegalArgumentException.class, () -> cConjecture.collatzConjecture(-1));
+    }
+}
\ No newline at end of file