diff --git a/algorithm-exercises-java/src/main/java/ae/hackerrank/interview_preparation_kit/miscellaneous/TimeComplexityPrimality.java b/algorithm-exercises-java/src/main/java/ae/hackerrank/interview_preparation_kit/miscellaneous/TimeComplexityPrimality.java new file mode 100644 index 00000000..7415756d --- /dev/null +++ b/algorithm-exercises-java/src/main/java/ae/hackerrank/interview_preparation_kit/miscellaneous/TimeComplexityPrimality.java @@ -0,0 +1,50 @@ + +package ae.hackerrank.interview_preparation_kit.miscellaneous; + +/** + * TimeComplexityPrimality. + * + * @link Problem definition + * [[docs/hackerrank/interview_preparation_kit/miscellaneous/ctci-big-o.md]] + */ +public class TimeComplexityPrimality { + + private static final String PRIME = "Prime"; + private static final String NOT_PRIME = "Not prime"; + + private TimeComplexityPrimality() {} + + private static Integer primeFactor(int n) { + if (n < 2) { + return null; + } + + int divisor = n; + Integer maxPrimeFactor = null; + int i = 2; + while (i <= Math.sqrt(divisor)) { + if (0 == divisor % i) { + divisor = divisor / i; + maxPrimeFactor = i; + } else { + i += 1; + } + } + + if (maxPrimeFactor == null) { + return n; + } + + return maxPrimeFactor; + } + + + /** + * primality. + */ + public static String primality(int n) { + Integer pf = primeFactor(n); + + return (pf == null || pf != n) ? NOT_PRIME : PRIME; + } +} diff --git a/algorithm-exercises-java/src/test/java/ae/hackerrank/interview_preparation_kit/miscellaneous/TimeComplexityPrimalityTest.java b/algorithm-exercises-java/src/test/java/ae/hackerrank/interview_preparation_kit/miscellaneous/TimeComplexityPrimalityTest.java new file mode 100644 index 00000000..c2adc98a --- /dev/null +++ b/algorithm-exercises-java/src/test/java/ae/hackerrank/interview_preparation_kit/miscellaneous/TimeComplexityPrimalityTest.java @@ -0,0 +1,61 @@ +package ae.hackerrank.interview_preparation_kit.miscellaneous; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.IOException; +import java.util.List; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.junit.jupiter.api.TestInstance.Lifecycle; +import util.JsonLoader; + +/** + * TimeComplexityPrimalityTest. + */ +@TestInstance(Lifecycle.PER_CLASS) +class TimeComplexityPrimalityTest { + /** + * TimeComplexityPrimalityTestCaseTest. + */ + public static class TimeComplexityPrimalityTestCaseTest { + public int input; + public String answer; + } + + /** + * TimeComplexityPrimalityTestCase. + */ + public static class TimeComplexityPrimalityTestCase { + public String title; + public List tests; + } + + private List testCases; + + @BeforeAll + void setup() throws IOException { + String path = String.join("/", + "hackerrank", + "interview_preparation_kit", + "miscellaneous", + "ctci_big_o.testcases.json"); + + this.testCases = JsonLoader.loadJson(path, TimeComplexityPrimalityTestCase.class); + } + + @Test + void testTimeComplexityPrimality() { + for (TimeComplexityPrimalityTestCase tests : testCases) { + for (TimeComplexityPrimalityTestCaseTest test : tests.tests) { + String result = TimeComplexityPrimality.primality(test.input); + + assertEquals(test.answer, result, + "%s(%s) => must be: %s".formatted( + "TimeComplexityPrimality.primality", + test.input, + test.answer)); + } + } + } +} diff --git a/algorithm-exercises-java/src/test/resources/hackerrank/interview_preparation_kit/miscellaneous/ctci_big_o.testcases.json b/algorithm-exercises-java/src/test/resources/hackerrank/interview_preparation_kit/miscellaneous/ctci_big_o.testcases.json new file mode 100644 index 00000000..04ec605b --- /dev/null +++ b/algorithm-exercises-java/src/test/resources/hackerrank/interview_preparation_kit/miscellaneous/ctci_big_o.testcases.json @@ -0,0 +1,66 @@ +[ + { + "title": "Sample Test case 0", + "tests": [ + { + "input": 12, + "answer": "Not prime" + }, + { + "input": 5, + "answer": "Prime" + }, + { + "input": 7, + "answer": "Prime" + } + ] + }, + { + "title": "Sample Test case 1", + "tests": [ + { + "input": 31, + "answer": "Prime" + }, + { + "input": 33, + "answer": "Not prime" + } + ] + }, + { + "title": "Sample Test case 2", + "tests": [ + { + "input": 2, + "answer": "Prime" + }, + { + "input": 7, + "answer": "Prime" + }, + { + "input": 1982, + "answer": "Not prime" + }, + { + "input": 14582734, + "answer": "Not prime" + }, + { + "input": 9891, + "answer": "Not prime" + } + ] + }, + { + "title": "Sample Test case 0", + "tests": [ + { + "input": 1, + "answer": "Not prime" + } + ] + } +] diff --git a/docs/hackerrank/interview_preparation_kit/miscellaneous/ctci-big-o.md b/docs/hackerrank/interview_preparation_kit/miscellaneous/ctci-big-o.md new file mode 100644 index 00000000..6127c885 --- /dev/null +++ b/docs/hackerrank/interview_preparation_kit/miscellaneous/ctci-big-o.md @@ -0,0 +1,65 @@ +# [Time Complexity: Primality](https://www.hackerrank.com/challenges/ctci-big-o) + +- Difficulty: `#medium` +- Category: `#ProblemSolvingBasic` + +## Using bitwise operations + +A prime is a natural number greater than `1` that has no positive divisors other +than `1` and itself. +Given `p` integers, determine the primality of each integer and return `Prime` +or `Not prime` on a new line. + +**Note**: If possible, try to come up with an $ \mathcal{O}(\sqrt{n}) $ +primality algorithm, or see what sort of optimizations you can come up with for +san $ \mathcal{O}(\sqrt{n}) $ algorithm. Be sure to check out the Editorial +after submitting your code. + +## Function Description + +Complete the primality function in the editor below. +primality has the following parameter(s): + +- `int` n: an integer to test for primality + +## Returns + +- string: Prime if is prime, or Not prime + +## Input Format + +The first line contains an integer, , the number of integers to check for primality. +Each of the subsequent lines contains an integer, , the number to test. + +## Constraints + +- $ 1 \leq p \leq 30 $ +- $ 1 \leq n \leq 2 × 10^9 $ + +## Sample Input + +```text +STDIN Function +----- -------- +3 p = 3 (number of values to follow) +12 n = 12 (first number to check) +5 n = 5 +7 n = 7 +``` + +## Sample Output + +```text +Not prime +Prime +Prime +``` + +## Explanation + +We check the following $ p = 3 $ integers for primality: + +1. $ n = 12 $ is divisible by numbers other than $ 1 $ and itself + (i.e.: $ 2 $, $ 3 $, $ 4 $, $ 6 $). +1. $ n = 5 $ is only divisible and itself. +1. $ n = 7 $ is only divisible and itself.