Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3dc6df7

Browse files
author
Gonzalo Diaz
committed
[Hacker Rank] Interview Preparation Kit: Miscellaneous: Time Complexity: Primality. Solved ✓.
1 parent c68c3df commit 3dc6df7

File tree

4 files changed

+240
-0
lines changed

4 files changed

+240
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# [Time Complexity: Primality](https://www.hackerrank.com/challenges/ctci-big-o)
2+
3+
- Difficulty: `#medium`
4+
- Category: `#ProblemSolvingBasic`
5+
6+
## Using bitwise operations
7+
8+
A prime is a natural number greater than `1` that has no positive divisors other
9+
than `1` and itself.
10+
Given `p` integers, determine the primality of each integer and return `Prime`
11+
or `Not prime` on a new line.
12+
13+
**Note**: If possible, try to come up with an $ \mathcal{O}(\sqrt{n}) $
14+
primality algorithm, or see what sort of optimizations you can come up with for
15+
san $ \mathcal{O}(\sqrt{n}) $ algorithm. Be sure to check out the Editorial
16+
after submitting your code.
17+
18+
## Function Description
19+
20+
Complete the primality function in the editor below.
21+
primality has the following parameter(s):
22+
23+
- `int` n: an integer to test for primality
24+
25+
## Returns
26+
27+
- string: Prime if is prime, or Not prime
28+
29+
## Input Format
30+
31+
The first line contains an integer, , the number of integers to check for primality.
32+
Each of the subsequent lines contains an integer, , the number to test.
33+
34+
## Constraints
35+
36+
- $ 1 \leq p \leq 30 $
37+
- $ 1 \leq n \leq 2 ×ばつ 10^9 $
38+
39+
## Sample Input
40+
41+
```text
42+
STDIN Function
43+
----- --------
44+
3 p = 3 (number of values to follow)
45+
12 n = 12 (first number to check)
46+
5 n = 5
47+
7 n = 7
48+
```
49+
50+
## Sample Output
51+
52+
```text
53+
Not prime
54+
Prime
55+
Prime
56+
```
57+
58+
## Explanation
59+
60+
We check the following $ p = 3 $ integers for primality:
61+
62+
1. $ n = 12 $ is divisible by numbers other than $ 1 $ and itself
63+
(i.e.: $ 2 ,ドル $ 3 ,ドル $ 4 ,ドル $ 6 $).
64+
1. $ n = 5 $ is only divisible and itself.
65+
1. $ n = 7 $ is only divisible and itself.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// @link Problem definition [[docs/hackerrank/interview_preparation_kit/miscellaneous/ctci-big-o.md]]
2+
3+
namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit.miscellaneous;
4+
5+
using System.Diagnostics.CodeAnalysis;
6+
using System.Text;
7+
8+
9+
public static class TimeComplexityPrimality
10+
{
11+
private const string PRIME = "Prime";
12+
private const string NOT_PRIME = "Not prime";
13+
14+
15+
private static Int32? primeFactor(int n)
16+
{
17+
if (n < 2)
18+
{
19+
return null;
20+
}
21+
22+
int divisor = n;
23+
Int32? maxPrimeFactor = null;
24+
int i = 2;
25+
while (i <= Math.Sqrt(divisor))
26+
{
27+
if (divisor % i == 0)
28+
{
29+
divisor = divisor / i;
30+
maxPrimeFactor = i;
31+
}
32+
else
33+
{
34+
i += 1;
35+
}
36+
}
37+
38+
if (maxPrimeFactor == null)
39+
{
40+
return n;
41+
}
42+
43+
return maxPrimeFactor;
44+
}
45+
46+
47+
/**
48+
* primality.
49+
*/
50+
public static string primality(int n)
51+
{
52+
Int32? pf = primeFactor(n);
53+
54+
return (pf == null || pf != n) ? NOT_PRIME : PRIME;
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
[
2+
{
3+
"title": "Sample Test case 0",
4+
"tests": [
5+
{
6+
"input": 12,
7+
"answer": "Not prime"
8+
},
9+
{
10+
"input": 5,
11+
"answer": "Prime"
12+
},
13+
{
14+
"input": 7,
15+
"answer": "Prime"
16+
}
17+
]
18+
},
19+
{
20+
"title": "Sample Test case 1",
21+
"tests": [
22+
{
23+
"input": 31,
24+
"answer": "Prime"
25+
},
26+
{
27+
"input": 33,
28+
"answer": "Not prime"
29+
}
30+
]
31+
},
32+
{
33+
"title": "Sample Test case 2",
34+
"tests": [
35+
{
36+
"input": 2,
37+
"answer": "Prime"
38+
},
39+
{
40+
"input": 7,
41+
"answer": "Prime"
42+
},
43+
{
44+
"input": 1982,
45+
"answer": "Not prime"
46+
},
47+
{
48+
"input": 14582734,
49+
"answer": "Not prime"
50+
},
51+
{
52+
"input": 9891,
53+
"answer": "Not prime"
54+
}
55+
]
56+
},
57+
{
58+
"title": "Sample Test case 0",
59+
"tests": [
60+
{
61+
"input": 1,
62+
"answer": "Not prime"
63+
}
64+
]
65+
}
66+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
namespace algorithm_exercises_csharp_test.hackerrank.interview_preparation_kit.miscellaneous;
2+
3+
using algorithm_exercises_csharp_test.common;
4+
using algorithm_exercises_csharp.hackerrank.interview_preparation_kit.miscellaneous;
5+
6+
[TestClass]
7+
public class TimeComplexityPrimalityTest
8+
{
9+
public class TimeComplexityPrimalityTestCaseTest(int input, String answer)
10+
{
11+
public int Input { get; } = input;
12+
public String Answer { get; } = answer;
13+
}
14+
15+
public class TimeComplexityPrimalityTestCase(string title, List<TimeComplexityPrimalityTestCaseTest> tests)
16+
{
17+
public string Title { get; } = title;
18+
public List<TimeComplexityPrimalityTestCaseTest> Tests { get; } = tests;
19+
}
20+
21+
private List<TimeComplexityPrimalityTestCase> testCases { get; set; } = default!;
22+
23+
[TestInitialize]
24+
public void testInitialize()
25+
{
26+
testCases = JsonLoader.resourceLoad<List<TimeComplexityPrimalityTestCase>>(
27+
"hackerrank/interview_preparation_kit/miscellaneous/ctci_big_o.testcases.json"
28+
) ?? [];
29+
}
30+
31+
[TestMethod]
32+
public void testTimeComplexityPrimality()
33+
{
34+
foreach (TimeComplexityPrimalityTestCase tests in testCases)
35+
{
36+
foreach (TimeComplexityPrimalityTestCaseTest test in tests.Tests)
37+
{
38+
String result = TimeComplexityPrimality.primality(test.Input);
39+
40+
Assert.AreEqual(
41+
test.Answer,
42+
result,
43+
string.Format(
44+
System.Globalization.CultureInfo.InvariantCulture,
45+
"TimeComplexityPrimality.primality({0}) => must be: {1}",
46+
test.Input,
47+
test.Answer
48+
)
49+
);
50+
}
51+
}
52+
}
53+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /