Skip to content

Commit cf50b1e

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

File tree

4 files changed

+179
-0
lines changed

4 files changed

+179
-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.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# pylint: disable=line-too-long
2+
# @link Problem definition [[docs/hackerrank/interview_preparation_kit/miscellaneous/ctci-big-o.md]] # noqa
3+
# pylint: enable=line-too-long
4+
5+
import math
6+
7+
8+
def primeFactor(n: int) -> 'int | None':
9+
10+
if n < 2:
11+
return None
12+
13+
divisor = n
14+
max_prime_factor = None
15+
16+
i = 2
17+
while i <= math.sqrt(divisor):
18+
if 0 == divisor % i:
19+
divisor = int(divisor / i)
20+
max_prime_factor = divisor
21+
else:
22+
i += 1
23+
24+
if max_prime_factor is None:
25+
return n
26+
27+
return max_prime_factor
28+
29+
30+
def primality(n):
31+
return "Prime" if primeFactor(n) == n else "Not prime"
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
[
2+
{
3+
"title": "Sample Test case 0",
4+
"tests": [
5+
6+
{
7+
"input": 12,
8+
"answer": "Not prime"
9+
},
10+
{
11+
"input": 5,
12+
"answer": "Prime"
13+
},
14+
{
15+
"input": 7,
16+
"answer": "Prime"
17+
}
18+
]
19+
},
20+
{
21+
"title": "Sample Test case 1",
22+
"tests": [
23+
{
24+
"input": 31,
25+
"answer": "Prime"
26+
},
27+
{
28+
"input": 33,
29+
"answer": "Not prime"
30+
}
31+
]
32+
},
33+
{
34+
"title": "Sample Test case 2",
35+
"tests": [
36+
{
37+
"input": 2,
38+
"answer": "Prime"
39+
},
40+
{
41+
"input": 7,
42+
"answer": "Prime"
43+
},
44+
{
45+
"input": 1982,
46+
"answer": "Not prime"
47+
},
48+
{
49+
"input": 14582734,
50+
"answer": "Not prime"
51+
},
52+
{
53+
"input": 9891,
54+
"answer": "Not prime"
55+
}
56+
]
57+
}
58+
]
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import unittest
2+
from pathlib import Path
3+
4+
from ....lib.loader import loadTestCases
5+
from .ctci_big_o import primality
6+
7+
8+
FILE_PATH = str(Path(__file__).resolve().parent)
9+
10+
TEST_CASES = loadTestCases(
11+
FILE_PATH + '/ctci_big_o.testcases.json')
12+
13+
14+
class TestCtciBigO(unittest.TestCase):
15+
16+
def test_ctci_big_o(self):
17+
18+
for _, testset in enumerate(TEST_CASES):
19+
20+
for _, _tt in enumerate(testset['tests']):
21+
22+
self.assertEqual(
23+
primality(_tt['input']), _tt['answer'],
24+
f"{_} | primality({_tt['input']}) must be "
25+
f"=> {_tt['answer']}")

0 commit comments

Comments
 (0)