Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/miscellaneous/ctci-big-o.md]] # noqa
*/

package hackerrank

import "math"

const NOT_PRIME = "Not prime"
const PRIME = "Prime"

func primeFactor(n int32) *int32 {
if n < 2 {
return nil
}

divisor := n
var maxPrimeFactor *int32 = nil

var i int32 = 2
for i <= int32(math.Sqrt(float64(divisor))) {
if divisor%i == 0 {
divisor = int32(divisor / i)
maxPrimeFactor = &divisor
} else {
i += 1
}
}
if maxPrimeFactor == nil {
val := int32(n)
return &val
}

return maxPrimeFactor
}

func primality(n int32) string {
pf := primeFactor(n)
if pf == nil || *pf != n {
return NOT_PRIME
}

return PRIME
}

func Primality(n int32) string {
return primality(n)
}
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package hackerrank

import (
"fmt"
"os"
"testing"

"github.com/stretchr/testify/assert"
"gon.cl/algorithms/utils"
)

type TimeComplexityPrimalityTest struct {
Input int32 `json:"input"`
Expected string `json:"answer"`
}

type TimeComplexityPrimalityTests struct {
Title string `json:"title"`
Tests []TimeComplexityPrimalityTest `json:"tests"`
}

var TimeComplexityPrimalityTestCases []TimeComplexityPrimalityTests

// You can use testing.T, if you want to test the code without benchmarking
func TimeComplexityPrimalitySetupSuite(t testing.TB) {
wd, _ := os.Getwd()
filepath := wd + "/ctci_big_o.testcases.json"
t.Log("Setup test cases from JSON: ", filepath)

var _, err = utils.LoadJSON(filepath, &TimeComplexityPrimalityTestCases)
if err != nil {
t.Log(err)
}
}

func TestTimeComplexityPrimality(t *testing.T) {

TimeComplexityPrimalitySetupSuite(t)

for _, tt := range TimeComplexityPrimalityTestCases {
for _, testCase := range tt.Tests {
testname := fmt.Sprintf("Primality(%d) => %s \n", testCase.Input, testCase.Expected)

t.Run(testname, func(t *testing.T) {
ans := Primality(testCase.Input)
assert.Equal(t, testCase.Expected, ans)
})
}
}
}