Skip to content

Commit cf7e99c

Browse files
Add files via upload
1 parent 5c0fc41 commit cf7e99c

File tree

1 file changed

+176
-0
lines changed

1 file changed

+176
-0
lines changed
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# **Question 3 (Detect Prime Numbers)**"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"**Implement a function to check if a given number is prime or not. A prime number is a whole number greater than 1 that is only divisible by 1 and itself.**"
15+
]
16+
},
17+
{
18+
"cell_type": "markdown",
19+
"metadata": {},
20+
"source": [
21+
"### `Solution 1`"
22+
]
23+
},
24+
{
25+
"cell_type": "code",
26+
"execution_count": 1,
27+
"metadata": {},
28+
"outputs": [],
29+
"source": [
30+
"def is_prime(number):\n",
31+
" if number <= 1:\n",
32+
" return False\n",
33+
" elif number == 2:\n",
34+
" return True\n",
35+
" elif number % 2 == 0:\n",
36+
" return False\n",
37+
" else:\n",
38+
" # Check divisibility from 3 to the square root of the number\n",
39+
" for i in range(3, int(number ** 0.5) + 1, 2):\n",
40+
" if number % i == 0:\n",
41+
" return False\n",
42+
" return True"
43+
]
44+
},
45+
{
46+
"cell_type": "markdown",
47+
"metadata": {},
48+
"source": [
49+
"In this program, the is_prime function takes a number number as input and returns True if the number is prime, and False otherwise.\n",
50+
"\n",
51+
"The function first handles the special cases where number is less than or equal to 1 and 2, returning False for 0, 1, and returning True for 2, as 2 is the only even prime number.\n",
52+
"\n",
53+
"For other numbers, the function checks if the number is divisible by 2, returning False if it is, as even numbers greater than 2 cannot be prime.\n",
54+
"\n",
55+
"For odd numbers greater than 2, the function iterates from 3 up to the square root of the number, checking divisibility by odd numbers only. If the number is divisible by any of these odd factors, it is not prime and False is returned. Otherwise, if no factor is found, the number is prime and True is returned."
56+
]
57+
},
58+
{
59+
"cell_type": "code",
60+
"execution_count": 2,
61+
"metadata": {},
62+
"outputs": [
63+
{
64+
"name": "stdout",
65+
"output_type": "stream",
66+
"text": [
67+
"17 is a prime number.\n"
68+
]
69+
}
70+
],
71+
"source": [
72+
"num = 17\n",
73+
"if is_prime(num):\n",
74+
" print(f'{num} is a prime number.')\n",
75+
"else:\n",
76+
" print(f'{num} is not a prime number.')"
77+
]
78+
},
79+
{
80+
"cell_type": "markdown",
81+
"metadata": {},
82+
"source": [
83+
"### `Solution 2`"
84+
]
85+
},
86+
{
87+
"cell_type": "code",
88+
"execution_count": 4,
89+
"metadata": {},
90+
"outputs": [],
91+
"source": [
92+
"def is_prime(number):\n",
93+
" if number <= 1:\n",
94+
" return False\n",
95+
"\n",
96+
" for divisor in range(2, int(number**0.5) + 1):\n",
97+
" if number % divisor == 0:\n",
98+
" return False\n",
99+
"\n",
100+
" return True"
101+
]
102+
},
103+
{
104+
"cell_type": "markdown",
105+
"metadata": {},
106+
"source": [
107+
"In this version, the is_prime function follows a similar logic as before but uses a simplified loop structure.\n",
108+
"\n",
109+
"The function first handles the case where number is less than or equal to 1, returning False as these numbers are not prime.\n",
110+
"\n",
111+
"The function then iterates from 2 up to the square root of the number (inclusive) using the range function. It checks if the number is divisible by any of these divisors. If a divisor is found, it means the number is not prime, and False is returned. Otherwise, if no divisor is found, the number is prime, and True is returned."
112+
]
113+
},
114+
{
115+
"cell_type": "code",
116+
"execution_count": 9,
117+
"metadata": {},
118+
"outputs": [
119+
{
120+
"data": {
121+
"text/plain": [
122+
"True"
123+
]
124+
},
125+
"execution_count": 9,
126+
"metadata": {},
127+
"output_type": "execute_result"
128+
}
129+
],
130+
"source": [
131+
"is_prime(1789)"
132+
]
133+
},
134+
{
135+
"cell_type": "code",
136+
"execution_count": 5,
137+
"metadata": {},
138+
"outputs": [
139+
{
140+
"data": {
141+
"text/plain": [
142+
"True"
143+
]
144+
},
145+
"execution_count": 5,
146+
"metadata": {},
147+
"output_type": "execute_result"
148+
}
149+
],
150+
"source": [
151+
"is_prime(2)"
152+
]
153+
}
154+
],
155+
"metadata": {
156+
"kernelspec": {
157+
"display_name": "Python 3",
158+
"language": "python",
159+
"name": "python3"
160+
},
161+
"language_info": {
162+
"codemirror_mode": {
163+
"name": "ipython",
164+
"version": 3
165+
},
166+
"file_extension": ".py",
167+
"mimetype": "text/x-python",
168+
"name": "python",
169+
"nbconvert_exporter": "python",
170+
"pygments_lexer": "ipython3",
171+
"version": "3.8.3"
172+
}
173+
},
174+
"nbformat": 4,
175+
"nbformat_minor": 4
176+
}

0 commit comments

Comments
 (0)