Skip to content

Commit b4a64f7

Browse files
authored
Add check prime challenge (donnemartin#226)
1 parent da5491b commit b4a64f7

File tree

4 files changed

+395
-0
lines changed

4 files changed

+395
-0
lines changed

math_probability/check_prime/__init__.py

Whitespace-only changes.
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"This notebook was prepared by [Donne Martin](https://github.com/donnemartin). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)."
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"# Challenge Notebook"
15+
]
16+
},
17+
{
18+
"cell_type": "markdown",
19+
"metadata": {},
20+
"source": [
21+
"## Problem: Check if a number is prime.\n",
22+
"\n",
23+
"* [Constraints](#Constraints)\n",
24+
"* [Test Cases](#Test-Cases)\n",
25+
"* [Algorithm](#Algorithm)\n",
26+
"* [Code](#Code)\n",
27+
"* [Unit Test](#Unit-Test)\n",
28+
"* [Solution Notebook](#Solution-Notebook)"
29+
]
30+
},
31+
{
32+
"cell_type": "markdown",
33+
"metadata": {},
34+
"source": [
35+
"## Constraints\n",
36+
"\n",
37+
"* Is it correct that 1 is not considered a prime number?\n",
38+
" * Yes\n",
39+
"* Can we assume the inputs are valid?\n",
40+
" * No\n",
41+
"* Can we assume this fits memory?\n",
42+
" * Yes"
43+
]
44+
},
45+
{
46+
"cell_type": "markdown",
47+
"metadata": {},
48+
"source": [
49+
"## Test Cases\n",
50+
"\n",
51+
"* None -> Exception\n",
52+
"* Not an int -> Exception\n",
53+
"* Less than 2 -> False\n",
54+
"* General case"
55+
]
56+
},
57+
{
58+
"cell_type": "markdown",
59+
"metadata": {},
60+
"source": [
61+
"## Algorithm\n",
62+
"\n",
63+
"Refer to the [Solution Notebook](). If you are stuck and need a hint, the solution notebook's algorithm discussion might be a good place to start."
64+
]
65+
},
66+
{
67+
"cell_type": "markdown",
68+
"metadata": {},
69+
"source": [
70+
"## Code"
71+
]
72+
},
73+
{
74+
"cell_type": "code",
75+
"execution_count": null,
76+
"metadata": {
77+
"collapsed": false
78+
},
79+
"outputs": [],
80+
"source": [
81+
"class Math(object):\n",
82+
"\n",
83+
" def check_prime(self, num):\n",
84+
" # TODO: Implement me\n",
85+
" pass"
86+
]
87+
},
88+
{
89+
"cell_type": "markdown",
90+
"metadata": {},
91+
"source": [
92+
"## Unit Test"
93+
]
94+
},
95+
{
96+
"cell_type": "markdown",
97+
"metadata": {},
98+
"source": [
99+
"**The following unit test is expected to fail until you solve the challenge.**"
100+
]
101+
},
102+
{
103+
"cell_type": "code",
104+
"execution_count": null,
105+
"metadata": {
106+
"collapsed": false
107+
},
108+
"outputs": [],
109+
"source": [
110+
"# %load test_check_prime.py\n",
111+
"from nose.tools import assert_equal, assert_raises\n",
112+
"\n",
113+
"\n",
114+
"class TestMath(object):\n",
115+
"\n",
116+
" def test_check_prime(self):\n",
117+
" math = Math()\n",
118+
" assert_raises(TypeError, math.check_prime, None)\n",
119+
" assert_raises(TypeError, math.check_prime, 98.6)\n",
120+
" assert_equal(math.check_prime(0), False)\n",
121+
" assert_equal(math.check_prime(1), False)\n",
122+
" assert_equal(math.check_prime(97), True)\n",
123+
" print('Success: test_check_prime')\n",
124+
"\n",
125+
"\n",
126+
"def main():\n",
127+
" test = TestMath()\n",
128+
" test.test_check_prime()\n",
129+
"\n",
130+
"\n",
131+
"if __name__ == '__main__':\n",
132+
" main()"
133+
]
134+
},
135+
{
136+
"cell_type": "markdown",
137+
"metadata": {},
138+
"source": [
139+
"## Solution Notebook\n",
140+
"\n",
141+
"Review the [Solution Notebook]() for a discussion on algorithms and code solutions."
142+
]
143+
}
144+
],
145+
"metadata": {
146+
"kernelspec": {
147+
"display_name": "Python 3",
148+
"language": "python",
149+
"name": "python3"
150+
},
151+
"language_info": {
152+
"codemirror_mode": {
153+
"name": "ipython",
154+
"version": 3
155+
},
156+
"file_extension": ".py",
157+
"mimetype": "text/x-python",
158+
"name": "python",
159+
"nbconvert_exporter": "python",
160+
"pygments_lexer": "ipython3",
161+
"version": "3.5.0"
162+
}
163+
},
164+
"nbformat": 4,
165+
"nbformat_minor": 0
166+
}
Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"This notebook was prepared by [Donne Martin](https://github.com/donnemartin). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges)."
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"# Solution Notebook"
15+
]
16+
},
17+
{
18+
"cell_type": "markdown",
19+
"metadata": {},
20+
"source": [
21+
"## Problem: Check if a number is prime.\n",
22+
"\n",
23+
"* [Constraints](#Constraints)\n",
24+
"* [Test Cases](#Test-Cases)\n",
25+
"* [Algorithm](#Algorithm)\n",
26+
"* [Code](#Code)\n",
27+
"* [Unit Test](#Unit-Test)"
28+
]
29+
},
30+
{
31+
"cell_type": "markdown",
32+
"metadata": {},
33+
"source": [
34+
"## Constraints\n",
35+
"\n",
36+
"* Is it correct that 1 is not considered a prime number?\n",
37+
" * Yes\n",
38+
"* Can we assume the inputs are valid?\n",
39+
" * No\n",
40+
"* Can we assume this fits memory?\n",
41+
" * Yes"
42+
]
43+
},
44+
{
45+
"cell_type": "markdown",
46+
"metadata": {},
47+
"source": [
48+
"## Test Cases\n",
49+
"\n",
50+
"* None -> Exception\n",
51+
"* Not an int -> Exception\n",
52+
"* Less than 2 -> False\n",
53+
"* General case"
54+
]
55+
},
56+
{
57+
"cell_type": "markdown",
58+
"metadata": {},
59+
"source": [
60+
"## Algorithm\n",
61+
"\n",
62+
"For a number to be prime, it must be 2 or greater and cannot be divisible by another number other than itself (and 1).\n",
63+
"\n",
64+
"We'll check by dividing all numbers from 2 to the input number to determine if the number is prime.\n",
65+
"\n",
66+
"As an optimization, we can divide from 2 to the square root of the input number. For each value that divides the input number evenly, there is a complement b where a * b = n. If a > sqrt(n) then b < sqrt(n) because sqrt(n^2) = n.\n",
67+
"\n",
68+
"Complexity:\n",
69+
"* Time: O(n) where n is the value of the input number\n",
70+
"* Space: O(1)\n",
71+
"\n",
72+
"### Sieve of Eratosthenes\n",
73+
"\n",
74+
"The Sieve of Eratosthenes provides a more efficient way of computing and generating primes. See the challenge [\"Generate a list of primes\"]() for more details."
75+
]
76+
},
77+
{
78+
"cell_type": "markdown",
79+
"metadata": {},
80+
"source": [
81+
"## Code"
82+
]
83+
},
84+
{
85+
"cell_type": "code",
86+
"execution_count": 1,
87+
"metadata": {
88+
"collapsed": false
89+
},
90+
"outputs": [],
91+
"source": [
92+
"import math\n",
93+
"\n",
94+
"\n",
95+
"class Math(object):\n",
96+
"\n",
97+
" def check_prime(self, num):\n",
98+
" if num is None:\n",
99+
" raise TypeError('num cannot be None')\n",
100+
" if num < 2:\n",
101+
" return False\n",
102+
" for i in range(2, num):\n",
103+
" if num % i == 0:\n",
104+
" return False\n",
105+
" return True\n",
106+
"\n",
107+
" def check_prime_optimized(self, num):\n",
108+
" if num is None:\n",
109+
" raise TypeError('num cannot be None')\n",
110+
" if num < 2:\n",
111+
" return False\n",
112+
" for i in range(2, int(math.sqrt(num)+1)):\n",
113+
" if num % i == 0:\n",
114+
" return False\n",
115+
" return True"
116+
]
117+
},
118+
{
119+
"cell_type": "markdown",
120+
"metadata": {},
121+
"source": [
122+
"## Unit Test"
123+
]
124+
},
125+
{
126+
"cell_type": "code",
127+
"execution_count": 2,
128+
"metadata": {
129+
"collapsed": false
130+
},
131+
"outputs": [
132+
{
133+
"name": "stdout",
134+
"output_type": "stream",
135+
"text": [
136+
"Overwriting test_check_prime.py\n"
137+
]
138+
}
139+
],
140+
"source": [
141+
"%%writefile test_check_prime.py\n",
142+
"from nose.tools import assert_equal, assert_raises\n",
143+
"\n",
144+
"\n",
145+
"class TestMath(object):\n",
146+
"\n",
147+
" def test_check_prime(self):\n",
148+
" math = Math()\n",
149+
" assert_raises(TypeError, math.check_prime, None)\n",
150+
" assert_raises(TypeError, math.check_prime, 98.6)\n",
151+
" assert_equal(math.check_prime(0), False)\n",
152+
" assert_equal(math.check_prime(1), False)\n",
153+
" assert_equal(math.check_prime(97), True)\n",
154+
" print('Success: test_check_prime')\n",
155+
"\n",
156+
"\n",
157+
"def main():\n",
158+
" test = TestMath()\n",
159+
" test.test_check_prime()\n",
160+
"\n",
161+
"\n",
162+
"if __name__ == '__main__':\n",
163+
" main()"
164+
]
165+
},
166+
{
167+
"cell_type": "code",
168+
"execution_count": 3,
169+
"metadata": {
170+
"collapsed": false
171+
},
172+
"outputs": [
173+
{
174+
"name": "stdout",
175+
"output_type": "stream",
176+
"text": [
177+
"Success: test_check_prime\n"
178+
]
179+
}
180+
],
181+
"source": [
182+
"%run -i test_check_prime.py"
183+
]
184+
}
185+
],
186+
"metadata": {
187+
"kernelspec": {
188+
"display_name": "Python 3",
189+
"language": "python",
190+
"name": "python3"
191+
},
192+
"language_info": {
193+
"codemirror_mode": {
194+
"name": "ipython",
195+
"version": 3
196+
},
197+
"file_extension": ".py",
198+
"mimetype": "text/x-python",
199+
"name": "python",
200+
"nbconvert_exporter": "python",
201+
"pygments_lexer": "ipython3",
202+
"version": "3.5.0"
203+
}
204+
},
205+
"nbformat": 4,
206+
"nbformat_minor": 0
207+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from nose.tools import assert_equal, assert_raises
2+
3+
4+
class TestMath(object):
5+
6+
def test_check_prime(self):
7+
math = Math()
8+
assert_raises(TypeError, math.check_prime, None)
9+
assert_raises(TypeError, math.check_prime, 98.6)
10+
assert_equal(math.check_prime(0), False)
11+
assert_equal(math.check_prime(1), False)
12+
assert_equal(math.check_prime(97), True)
13+
print('Success: test_check_prime')
14+
15+
16+
def main():
17+
test = TestMath()
18+
test.test_check_prime()
19+
20+
21+
if __name__ == '__main__':
22+
main()

0 commit comments

Comments
 (0)