Skip to content

Commit 77a20e6

Browse files
authored
Merge pull request #370 from sir-gon/feature/ctci-big-o
[Hacker Rank] Interview Preparation Kit: Miscellaneous: Time Complexi…
2 parents fc6f43c + 7bd4ae7 commit 77a20e6

File tree

4 files changed

+229
-0
lines changed

4 files changed

+229
-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: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/miscellaneous/ctci-big-o.md]] # noqa
3+
*/
4+
5+
package hackerrank
6+
7+
import "math"
8+
9+
const NOT_PRIME = "Not prime"
10+
const PRIME = "Prime"
11+
12+
func primeFactor(n int32) *int32 {
13+
if n < 2 {
14+
return nil
15+
}
16+
17+
divisor := n
18+
var maxPrimeFactor *int32 = nil
19+
20+
var i int32 = 2
21+
for i <= int32(math.Sqrt(float64(divisor))) {
22+
if divisor%i == 0 {
23+
divisor = int32(divisor / i)
24+
maxPrimeFactor = &divisor
25+
} else {
26+
i += 1
27+
}
28+
}
29+
if maxPrimeFactor == nil {
30+
val := int32(n)
31+
return &val
32+
}
33+
34+
return maxPrimeFactor
35+
}
36+
37+
func primality(n int32) string {
38+
pf := primeFactor(n)
39+
if pf == nil || *pf != n {
40+
return NOT_PRIME
41+
}
42+
43+
return PRIME
44+
}
45+
46+
func Primality(n int32) string {
47+
return primality(n)
48+
}
Lines changed: 66 additions & 0 deletions
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+
]
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package hackerrank
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
"gon.cl/algorithms/utils"
10+
)
11+
12+
type TimeComplexityPrimalityTest struct {
13+
Input int32 `json:"input"`
14+
Expected string `json:"answer"`
15+
}
16+
17+
type TimeComplexityPrimalityTests struct {
18+
Title string `json:"title"`
19+
Tests []TimeComplexityPrimalityTest `json:"tests"`
20+
}
21+
22+
var TimeComplexityPrimalityTestCases []TimeComplexityPrimalityTests
23+
24+
// You can use testing.T, if you want to test the code without benchmarking
25+
func TimeComplexityPrimalitySetupSuite(t testing.TB) {
26+
wd, _ := os.Getwd()
27+
filepath := wd + "/ctci_big_o.testcases.json"
28+
t.Log("Setup test cases from JSON: ", filepath)
29+
30+
var _, err = utils.LoadJSON(filepath, &TimeComplexityPrimalityTestCases)
31+
if err != nil {
32+
t.Log(err)
33+
}
34+
}
35+
36+
func TestTimeComplexityPrimality(t *testing.T) {
37+
38+
TimeComplexityPrimalitySetupSuite(t)
39+
40+
for _, tt := range TimeComplexityPrimalityTestCases {
41+
for _, testCase := range tt.Tests {
42+
testname := fmt.Sprintf("Primality(%d) => %s \n", testCase.Input, testCase.Expected)
43+
44+
t.Run(testname, func(t *testing.T) {
45+
ans := Primality(testCase.Input)
46+
assert.Equal(t, testCase.Expected, ans)
47+
})
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)