Skip to content

Commit e348250

Browse files
Interview Preparation Kit --> Miscellaneous --> Time_Complexity-Primality
1 parent a483823 commit e348250

File tree

3 files changed

+96
-9
lines changed

3 files changed

+96
-9
lines changed

HackerRank/Interview Preparation Kit/Miscellaneous/Miscellaneous.ipynb

Lines changed: 77 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,6 @@
88
"![Flipping bits](Flipping_bits.png)"
99
]
1010
},
11-
{
12-
"cell_type": "code",
13-
"execution_count": null,
14-
"metadata": {},
15-
"outputs": [],
16-
"source": [
17-
"n=b"
18-
]
19-
},
2011
{
2112
"cell_type": "code",
2213
"execution_count": 17,
@@ -67,6 +58,83 @@
6758
" print(int('1'*(32-len(x))+s,2))"
6859
]
6960
},
61+
{
62+
"cell_type": "markdown",
63+
"metadata": {},
64+
"source": [
65+
"# Time Complexity: Primality\n",
66+
" ***Every prime number can represented in form of 6n+1 or 6n-1 except 2 and 3, where n is natural number***\n",
67+
" \n",
68+
" \n",
69+
"![Time_Complexity-Primality](Time_Complexity-Primality.png)"
70+
]
71+
},
72+
{
73+
"cell_type": "code",
74+
"execution_count": null,
75+
"metadata": {},
76+
"outputs": [],
77+
"source": [
78+
"n=b"
79+
]
80+
},
81+
{
82+
"cell_type": "code",
83+
"execution_count": 1,
84+
"metadata": {},
85+
"outputs": [
86+
{
87+
"name": "stdin",
88+
"output_type": "stream",
89+
"text": [
90+
" 2\n",
91+
" 23\n"
92+
]
93+
},
94+
{
95+
"name": "stdout",
96+
"output_type": "stream",
97+
"text": [
98+
"Prime\n"
99+
]
100+
},
101+
{
102+
"name": "stdin",
103+
"output_type": "stream",
104+
"text": [
105+
" 67\n"
106+
]
107+
},
108+
{
109+
"name": "stdout",
110+
"output_type": "stream",
111+
"text": [
112+
"Prime\n"
113+
]
114+
}
115+
],
116+
"source": [
117+
"def isPrime(n):\n",
118+
" if n<=1:\n",
119+
" return False\n",
120+
" if n<=3:\n",
121+
" return True\n",
122+
" if n%2==0 or n%3==0:\n",
123+
" return False\n",
124+
" i=5\n",
125+
" while i*i<=n:\n",
126+
" if n%i==0 or n%(i+2)==0:\n",
127+
" return False\n",
128+
" i+=6\n",
129+
" return True\n",
130+
"for _ in range(int(input())):\n",
131+
" n=int(input())\n",
132+
" if isPrime(n):\n",
133+
" print('Prime')\n",
134+
" else:\n",
135+
" print('Not Prime')"
136+
]
137+
},
70138
{
71139
"cell_type": "code",
72140
"execution_count": null,
131 KB
Loading
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
def isPrime(n):
2+
if n<=1:
3+
return False
4+
if n<=3:
5+
return True
6+
if n%2==0 or n%3==0:
7+
return False
8+
i=5
9+
while i*i<=n:
10+
if n%i==0 or n%(i+2)==0:
11+
return False
12+
i+=6
13+
return True
14+
for _ in range(int(input())):
15+
n=int(input())
16+
if isPrime(n):
17+
print('Prime')
18+
else:
19+
print('Not Prime')

0 commit comments

Comments
 (0)