From 3dc6df77b9de1bc0e4188ee3389846bfad7c4885 Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Mon, 18 Aug 2025 17:14:11 +0000 Subject: [PATCH] =?UTF-8?q?[Hacker=20Rank]=20Interview=20Preparation=20Kit?= =?UTF-8?q?:=20Miscellaneous:=20Time=20Complexity:=20Primality.=20Solved?= =?UTF-8?q?=20=E2=9C=93.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../miscellaneous/ctci-big-o.md | 65 ++++++++++++++++++ .../miscellaneous/TimeComplexityPrimality.cs | 56 ++++++++++++++++ .../miscellaneous/ctci_big_o.testcases.json | 66 +++++++++++++++++++ .../TimeComplexityPrimalityTest.cs | 53 +++++++++++++++ 4 files changed, 240 insertions(+) create mode 100644 docs/hackerrank/interview_preparation_kit/miscellaneous/ctci-big-o.md create mode 100644 src/algorithm_exercises_csharp/hackerrank/interview_preparation_kit/miscellaneous/TimeComplexityPrimality.cs create mode 100644 src/algorithm_exercises_csharp_test/Resources/hackerrank/interview_preparation_kit/miscellaneous/ctci_big_o.testcases.json create mode 100644 src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/miscellaneous/TimeComplexityPrimalityTest.cs 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 0000000..6127c88 --- /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. diff --git a/src/algorithm_exercises_csharp/hackerrank/interview_preparation_kit/miscellaneous/TimeComplexityPrimality.cs b/src/algorithm_exercises_csharp/hackerrank/interview_preparation_kit/miscellaneous/TimeComplexityPrimality.cs new file mode 100644 index 0000000..9273f8a --- /dev/null +++ b/src/algorithm_exercises_csharp/hackerrank/interview_preparation_kit/miscellaneous/TimeComplexityPrimality.cs @@ -0,0 +1,56 @@ +// @link Problem definition [[docs/hackerrank/interview_preparation_kit/miscellaneous/ctci-big-o.md]] + +namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit.miscellaneous; + +using System.Diagnostics.CodeAnalysis; +using System.Text; + + +public static class TimeComplexityPrimality +{ + private const string PRIME = "Prime"; + private const string NOT_PRIME = "Not prime"; + + + private static Int32? primeFactor(int n) + { + if (n < 2) + { + return null; + } + + int divisor = n; + Int32? maxPrimeFactor = null; + int i = 2; + while (i <= Math.Sqrt(divisor)) + { + if (divisor % i == 0) + { + divisor = divisor / i; + maxPrimeFactor = i; + } + else + { + i += 1; + } + } + + if (maxPrimeFactor == null) + { + return n; + } + + return maxPrimeFactor; + } + + + /** + * primality. + */ + public static string primality(int n) + { + Int32? pf = primeFactor(n); + + return (pf == null || pf != n) ? NOT_PRIME : PRIME; + } +} diff --git a/src/algorithm_exercises_csharp_test/Resources/hackerrank/interview_preparation_kit/miscellaneous/ctci_big_o.testcases.json b/src/algorithm_exercises_csharp_test/Resources/hackerrank/interview_preparation_kit/miscellaneous/ctci_big_o.testcases.json new file mode 100644 index 0000000..04ec605 --- /dev/null +++ b/src/algorithm_exercises_csharp_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/src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/miscellaneous/TimeComplexityPrimalityTest.cs b/src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/miscellaneous/TimeComplexityPrimalityTest.cs new file mode 100644 index 0000000..a7ce1c8 --- /dev/null +++ b/src/algorithm_exercises_csharp_test/hackerrank/interview_preparation_kit/miscellaneous/TimeComplexityPrimalityTest.cs @@ -0,0 +1,53 @@ +namespace algorithm_exercises_csharp_test.hackerrank.interview_preparation_kit.miscellaneous; + +using algorithm_exercises_csharp_test.common; +using algorithm_exercises_csharp.hackerrank.interview_preparation_kit.miscellaneous; + +[TestClass] +public class TimeComplexityPrimalityTest +{ + public class TimeComplexityPrimalityTestCaseTest(int input, String answer) + { + public int Input { get; } = input; + public String Answer { get; } = answer; + } + + public class TimeComplexityPrimalityTestCase(string title, List tests) + { + public string Title { get; } = title; + public List Tests { get; } = tests; + } + + private List testCases { get; set; } = default!; + + [TestInitialize] + public void testInitialize() + { + testCases = JsonLoader.resourceLoad>( + "hackerrank/interview_preparation_kit/miscellaneous/ctci_big_o.testcases.json" + ) ?? []; + } + + [TestMethod] + public void testTimeComplexityPrimality() + { + foreach (TimeComplexityPrimalityTestCase tests in testCases) + { + foreach (TimeComplexityPrimalityTestCaseTest test in tests.Tests) + { + String result = TimeComplexityPrimality.primality(test.Input); + + Assert.AreEqual( + test.Answer, + result, + string.Format( + System.Globalization.CultureInfo.InvariantCulture, + "TimeComplexityPrimality.primality({0}) => must be: {1}", + test.Input, + test.Answer + ) + ); + } + } + } +}