Repository: TheAlgorithms/Java
Pull request #3312 (2022-10-01 08:34:33)

algorithm: twin prime
### **Describe your change:**

- [x] Add an algorithm?
- [ ] Fix a bug or typo in an existing algorithm?
- [ ] Documentation change?

#### References

<!-- Add any reference to previous pull-request or issue -->
#3293 

### **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/TwinPrime.java b/src/main/java/com/thealgorithms/maths/TwinPrime.java
new file mode 100644
index 00000000..ba4310ac
--- /dev/null
+++ b/src/main/java/com/thealgorithms/maths/TwinPrime.java
@@ -0,0 +1,32 @@
+package com.thealgorithms.maths;
+/*
+ * Java program to find 'twin prime' of a prime number
+ * Twin Prime: Twin prime of a number n is (n+2) 
+ * if and only if n & (n+2) are prime.
+ * Wikipedia: https://en.wikipedia.org/wiki/Twin_prime
+ * 
+ * Author: Akshay Dubey (https://github.com/itsAkshayDubey)
+ * 
+ * */
+
+public class TwinPrime {
+	
+	/**
+     * This method returns twin prime of the integer value passed as argument
+     *
+     * @param input_number Integer value of which twin prime is to be found
+     * @return (number + 2) if number and (number + 2) are prime, -1 otherwise
+     */
+	static int getTwinPrime(int inputNumber) {
+		
+		//if inputNumber and (inputNumber + 2) are both prime 
+		//then return (inputNumber + 2) as a result
+		if(PrimeCheck.isPrime(inputNumber) && PrimeCheck.isPrime(inputNumber + 2) ) {
+			return inputNumber + 2;
+		}
+		//if any one from inputNumber and (inputNumber + 2) or if both of them are not prime 
+		//then return -1 as a result
+		return -1;
+	}
+
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/maths/TwinPrimeTest.java b/src/test/java/com/thealgorithms/maths/TwinPrimeTest.java
new file mode 100644
index 00000000..d1f00954
--- /dev/null
+++ b/src/test/java/com/thealgorithms/maths/TwinPrimeTest.java
@@ -0,0 +1,60 @@
+package com.thealgorithms.maths;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import org.junit.jupiter.api.Test;
+
+class TwinPrimeTest {
+
+	@Test
+	void shouldReturn7() {
+		//given
+		int number = 5;
+		int expectedResult = 7;
+		
+		//when
+		int actualResult = TwinPrime.getTwinPrime(number);
+		
+		//then
+		assertEquals(expectedResult,actualResult);
+	}
+	
+	@Test
+	void shouldReturn5() {
+		//given
+		int number = 3;
+		int expectedResult = 5;
+		
+		//when
+		int actualResult = TwinPrime.getTwinPrime(number);
+		
+		//then
+		assertEquals(expectedResult,actualResult);
+	}
+	
+	@Test
+	void shouldReturnNegative1() {
+		//given
+		int number = 4;
+		int expectedResult = -1;
+		
+		//when
+		int actualResult = TwinPrime.getTwinPrime(number);
+		
+		//then
+		assertEquals(expectedResult,actualResult);
+	}
+	
+	@Test
+	void shouldReturn19() {
+		//given
+		int number = 17;
+		int expectedResult = 19;
+		
+		//when
+		int actualResult = TwinPrime.getTwinPrime(number);
+		
+		//then
+		assertEquals(expectedResult,actualResult);
+	}
+}