Skip to content

Commit 82af88b

Browse files
committed
Updated tutorial
1 parent 5e42d44 commit 82af88b

File tree

4 files changed

+770
-0
lines changed

4 files changed

+770
-0
lines changed
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"This is where to show how long it takes to run each method. "
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"## Method 1"
15+
]
16+
},
17+
{
18+
"cell_type": "code",
19+
"execution_count": 24,
20+
"metadata": {},
21+
"outputs": [],
22+
"source": [
23+
"primes = []\n",
24+
"for num in range(2, 20 + 1):\n",
25+
" isPrime = True\n",
26+
" for y in range(2, num):\n",
27+
" if num % y == 0:\n",
28+
" isPrime = False\n",
29+
" \n",
30+
" if isPrime:\n",
31+
" primes.append(num)"
32+
]
33+
},
34+
{
35+
"cell_type": "code",
36+
"execution_count": 25,
37+
"metadata": {},
38+
"outputs": [
39+
{
40+
"data": {
41+
"text/plain": [
42+
"[2, 3, 5, 7, 11, 13, 17, 19]"
43+
]
44+
},
45+
"execution_count": 25,
46+
"metadata": {},
47+
"output_type": "execute_result"
48+
}
49+
],
50+
"source": [
51+
"primes"
52+
]
53+
},
54+
{
55+
"cell_type": "markdown",
56+
"metadata": {},
57+
"source": [
58+
"## Method 2"
59+
]
60+
},
61+
{
62+
"cell_type": "markdown",
63+
"metadata": {},
64+
"source": [
65+
"Only difference is a break statement. Basically once you know that a number isn't prime why should you keep on testing it in the inner loop"
66+
]
67+
},
68+
{
69+
"cell_type": "code",
70+
"execution_count": 26,
71+
"metadata": {},
72+
"outputs": [],
73+
"source": [
74+
"primes = []\n",
75+
"for num in range(2, 20 + 1):\n",
76+
" isPrime = True\n",
77+
" for y in range(2, num):\n",
78+
" if num % y == 0:\n",
79+
" isPrime = False\n",
80+
" break\n",
81+
" \n",
82+
" if isPrime:\n",
83+
" primes.append(num)"
84+
]
85+
},
86+
{
87+
"cell_type": "code",
88+
"execution_count": 27,
89+
"metadata": {},
90+
"outputs": [
91+
{
92+
"data": {
93+
"text/plain": [
94+
"[2, 3, 5, 7, 11, 13, 17, 19]"
95+
]
96+
},
97+
"execution_count": 27,
98+
"metadata": {},
99+
"output_type": "execute_result"
100+
}
101+
],
102+
"source": [
103+
"primes"
104+
]
105+
},
106+
{
107+
"cell_type": "markdown",
108+
"metadata": {},
109+
"source": [
110+
"## Method 3"
111+
]
112+
},
113+
{
114+
"cell_type": "markdown",
115+
"metadata": {},
116+
"source": [
117+
"When you are testing whether a nuber is prime, all you are doing is testing whether a number times another number is prime. Eventually you will get to a situation where you will multiply the same prime numbers against each other. \n",
118+
"\n",
119+
"Commutative property: When two numbers are multiplied together, the product is the same regardless of the order of the multiplicands. For example 4 * 2 = 2 * 4"
120+
]
121+
},
122+
{
123+
"cell_type": "markdown",
124+
"metadata": {},
125+
"source": [
126+
"6 x8 is the same as 8 x 6 (can make a visual on this). The image above is all the factors for 48 (notice it gets repetitive). up a hill and down. communtative property of multiplication). Tipping point happens at the square root of a number."
127+
]
128+
},
129+
{
130+
"cell_type": "code",
131+
"execution_count": 30,
132+
"metadata": {},
133+
"outputs": [],
134+
"source": [
135+
"primes = []\n",
136+
"for x in range(2, 20 + 1):\n",
137+
" isPrime = True\n",
138+
" for y in range(2, int(x ** 0.5) + 1):\n",
139+
" if x % y == 0:\n",
140+
" isPrime = False\n",
141+
" break\n",
142+
" \n",
143+
" if isPrime:\n",
144+
" primes.append(x)"
145+
]
146+
},
147+
{
148+
"cell_type": "code",
149+
"execution_count": 31,
150+
"metadata": {},
151+
"outputs": [
152+
{
153+
"data": {
154+
"text/plain": [
155+
"[2, 3, 5, 7, 11, 13, 17, 19]"
156+
]
157+
},
158+
"execution_count": 31,
159+
"metadata": {},
160+
"output_type": "execute_result"
161+
}
162+
],
163+
"source": [
164+
"primes"
165+
]
166+
},
167+
{
168+
"cell_type": "code",
169+
"execution_count": null,
170+
"metadata": {},
171+
"outputs": [],
172+
"source": []
173+
}
174+
],
175+
"metadata": {
176+
"kernelspec": {
177+
"display_name": "Python [conda root]",
178+
"language": "python",
179+
"name": "conda-root-py"
180+
},
181+
"language_info": {
182+
"codemirror_mode": {
183+
"name": "ipython",
184+
"version": 3
185+
},
186+
"file_extension": ".py",
187+
"mimetype": "text/x-python",
188+
"name": "python",
189+
"nbconvert_exporter": "python",
190+
"pygments_lexer": "ipython3",
191+
"version": "3.6.4"
192+
}
193+
},
194+
"nbformat": 4,
195+
"nbformat_minor": 2
196+
}
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"## Method 1"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": 24,
13+
"metadata": {},
14+
"outputs": [],
15+
"source": [
16+
"primes = []\n",
17+
"for num in range(2, 20 + 1):\n",
18+
" isPrime = True\n",
19+
" for y in range(2, num):\n",
20+
" if num % y == 0:\n",
21+
" isPrime = False\n",
22+
" \n",
23+
" if isPrime:\n",
24+
" primes.append(num)"
25+
]
26+
},
27+
{
28+
"cell_type": "code",
29+
"execution_count": 25,
30+
"metadata": {},
31+
"outputs": [
32+
{
33+
"data": {
34+
"text/plain": [
35+
"[2, 3, 5, 7, 11, 13, 17, 19]"
36+
]
37+
},
38+
"execution_count": 25,
39+
"metadata": {},
40+
"output_type": "execute_result"
41+
}
42+
],
43+
"source": [
44+
"primes"
45+
]
46+
},
47+
{
48+
"cell_type": "markdown",
49+
"metadata": {},
50+
"source": [
51+
"## Method 2"
52+
]
53+
},
54+
{
55+
"cell_type": "markdown",
56+
"metadata": {},
57+
"source": [
58+
"Only difference is a break statement. Basically once you know that a number isn't prime why should you keep on testing it in the inner loop"
59+
]
60+
},
61+
{
62+
"cell_type": "code",
63+
"execution_count": 26,
64+
"metadata": {},
65+
"outputs": [],
66+
"source": [
67+
"primes = []\n",
68+
"for num in range(2, 20 + 1):\n",
69+
" isPrime = True\n",
70+
" for y in range(2, num):\n",
71+
" if num % y == 0:\n",
72+
" isPrime = False\n",
73+
" break\n",
74+
" \n",
75+
" if isPrime:\n",
76+
" primes.append(num)"
77+
]
78+
},
79+
{
80+
"cell_type": "code",
81+
"execution_count": 27,
82+
"metadata": {},
83+
"outputs": [
84+
{
85+
"data": {
86+
"text/plain": [
87+
"[2, 3, 5, 7, 11, 13, 17, 19]"
88+
]
89+
},
90+
"execution_count": 27,
91+
"metadata": {},
92+
"output_type": "execute_result"
93+
}
94+
],
95+
"source": [
96+
"primes"
97+
]
98+
},
99+
{
100+
"cell_type": "markdown",
101+
"metadata": {},
102+
"source": [
103+
"## Method 3"
104+
]
105+
},
106+
{
107+
"cell_type": "markdown",
108+
"metadata": {},
109+
"source": [
110+
"When you are testing whether a nuber is prime, all you are doing is testing whether a number times another number is prime. Eventually you will get to a situation where you will multiply the same prime numbers against each other. \n",
111+
"\n",
112+
"Commutative property: When two numbers are multiplied together, the product is the same regardless of the order of the multiplicands. For example 4 * 2 = 2 * 4"
113+
]
114+
},
115+
{
116+
"cell_type": "markdown",
117+
"metadata": {},
118+
"source": [
119+
"6 x8 is the same as 8 x 6 (can make a visual on this). The image above is all the factors for 48 (notice it gets repetitive). up a hill and down. communtative property of multiplication). Tipping point happens at the square root of a number."
120+
]
121+
},
122+
{
123+
"cell_type": "code",
124+
"execution_count": 30,
125+
"metadata": {},
126+
"outputs": [],
127+
"source": [
128+
"primes = []\n",
129+
"for x in range(2, 20 + 1):\n",
130+
" isPrime = True\n",
131+
" for y in range(2, int(x ** 0.5) + 1):\n",
132+
" if x % y == 0:\n",
133+
" isPrime = False\n",
134+
" break\n",
135+
" \n",
136+
" if isPrime:\n",
137+
" primes.append(x)"
138+
]
139+
},
140+
{
141+
"cell_type": "code",
142+
"execution_count": 31,
143+
"metadata": {},
144+
"outputs": [
145+
{
146+
"data": {
147+
"text/plain": [
148+
"[2, 3, 5, 7, 11, 13, 17, 19]"
149+
]
150+
},
151+
"execution_count": 31,
152+
"metadata": {},
153+
"output_type": "execute_result"
154+
}
155+
],
156+
"source": [
157+
"primes"
158+
]
159+
},
160+
{
161+
"cell_type": "code",
162+
"execution_count": null,
163+
"metadata": {},
164+
"outputs": [],
165+
"source": []
166+
}
167+
],
168+
"metadata": {
169+
"kernelspec": {
170+
"display_name": "Python [conda root]",
171+
"language": "python",
172+
"name": "conda-root-py"
173+
},
174+
"language_info": {
175+
"codemirror_mode": {
176+
"name": "ipython",
177+
"version": 3
178+
},
179+
"file_extension": ".py",
180+
"mimetype": "text/x-python",
181+
"name": "python",
182+
"nbconvert_exporter": "python",
183+
"pygments_lexer": "ipython3",
184+
"version": "3.6.4"
185+
}
186+
},
187+
"nbformat": 4,
188+
"nbformat_minor": 2
189+
}

0 commit comments

Comments
 (0)