-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperfect_square_mod8.py
More file actions
354 lines (284 loc) · 13.9 KB
/
perfect_square_mod8.py
File metadata and controls
354 lines (284 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# Manim animation: "Perfect squares modulo 8 are 0, 1, or 4"
# Dependencies:
# pip install manim
# pip install "manim-voiceover[gtts]"
# Run with:
# manim -pqh perfect_square_mod8.py PerfectSquareModuloEight
from manim import *
from manim_voiceover import VoiceoverScene
from manim_voiceover.services.gtts import GTTSService
from pathlib import Path
LATEX_TEMPLATE = TexTemplate()
LATEX_TEMPLATE.add_to_preamble(r"\spaceskip=0.45em plus 0.15em minus 0.1em")
# -- Palette -----------------------------------------------------------------
BG = WHITE
FG = BLACK
ACCENT = ManimColor("#1565C0")
GOOD = ManimColor("#2E7D32")
BAD = ManimColor("#C62828")
MUTED = ManimColor("#546E7A")
# -- Global font sizes --------------------------------------------------------
TITLE_FS = 40
HEAD_FS = 36
BODY_FS = 32
SMALL_FS = 30
TINY_FS = 22
SAFE_FRAME_RATIO = 0.95
WATERMARK_WIDTH_RATIO = 0.04
WATERMARK_OPACITY = 0.85
WATERMARK_BUFF = 0.22
def styled_title(text: str, font_size: int = TITLE_FS, color=ACCENT) -> Text:
return Text(text, font_size=font_size, color=color, weight=BOLD)
def h_rule(width: float = 5.5, color=ACCENT) -> Line:
return Line(LEFT * width / 2, RIGHT * width / 2, color=color, stroke_width=2)
def stage_header(number: str, title: str) -> VGroup:
badge = RoundedRectangle(
width=0.55, height=0.55, corner_radius=0.12,
fill_color=ACCENT, fill_opacity=1, stroke_width=0
)
badge_num = Text(number, font_size=TINY_FS, color=WHITE, weight=BOLD).move_to(badge)
label = Text(title, font_size=HEAD_FS, color=ACCENT, weight=BOLD)
return VGroup(badge, badge_num, label).arrange(RIGHT, buff=0.18)
class PerfectSquareModuloEight(VoiceoverScene):
def setup(self):
super().setup()
self.camera.background_color = BG
self.set_speech_service(GTTSService(lang="en", tld="com"))
Text.set_default(font="Latin Modern Roman", line_spacing=1.15)
Tex.set_default(tex_template=LATEX_TEMPLATE)
MathTex.set_default(tex_template=LATEX_TEMPLATE)
CONFIG = {"pixel_width": 1080, "pixel_height": 1920}
def _fit_to_safe_area(self, mobj: Mobject, ratio: float = SAFE_FRAME_RATIO) -> Mobject:
max_width = config.frame_width * ratio
max_height = config.frame_height * ratio
if mobj.width == 0 or mobj.height == 0:
return mobj
scale_factor = min(max_width / mobj.width, max_height / mobj.height)
if abs(scale_factor - 1) > 0.01:
mobj.scale(scale_factor)
left_limit = -max_width / 2
right_limit = max_width / 2
bottom_limit = -max_height / 2
top_limit = max_height / 2
shift_x = 0.0
if mobj.get_left()[0] < left_limit:
shift_x = left_limit - mobj.get_left()[0]
elif mobj.get_right()[0] > right_limit:
shift_x = right_limit - mobj.get_right()[0]
shift_y = 0.0
if mobj.get_bottom()[1] < bottom_limit:
shift_y = bottom_limit - mobj.get_bottom()[1]
elif mobj.get_top()[1] > top_limit:
shift_y = top_limit - mobj.get_top()[1]
if shift_x or shift_y:
mobj.shift(RIGHT * shift_x + UP * shift_y)
return mobj
def construct(self):
self._add_watermark()
self._slide_title_card()
self._slide_step1_parity_split()
self._slide_step2_even_case()
self._slide_step3_odd_case()
self._slide_conclusion()
self._slide_end_card()
def _add_watermark(self):
use_watermark = True
watermark_text = "© hameefy"
if use_watermark:
wm = Text(watermark_text, color=GRAY, font_size=24).to_edge(DR, buff=0.25)
wm.set_opacity(0.35)
self.add(wm)
def _slide_title_card(self):
topic = Text("Number Theory", font_size=SMALL_FS, color=MUTED, weight=BOLD)
rule = h_rule(4.5)
title = Text(
"Perfect Squares Modulo 8",
font_size=TITLE_FS + 2, color=ACCENT, weight=BOLD,
line_spacing=1.25
)
title.scale_to_fit_width(config.frame_width * 0.86)
subtitle = Text(
"Show that for any integer n",
font_size=HEAD_FS, color=FG
)
sub2 = MathTex(
"n^2 \\equiv 0, 1, \\text{ or } 4 \\pmod{8}",
font_size=HEAD_FS, color=MUTED
)
stack = VGroup(topic, rule, title, subtitle, sub2).arrange(DOWN, buff=0.34).move_to(ORIGIN)
box = SurroundingRectangle(stack, color=ACCENT, buff=0.5, corner_radius=0.25, stroke_width=3)
slide_group = VGroup(box, topic, rule, title, subtitle, sub2)
self._fit_to_safe_area(slide_group)
with self.voiceover(
"Show that every perfect square leaves remainder zero, one, or four when divided by eight."
) as trk:
self.play(FadeIn(box), Write(topic), run_time=min(trk.duration * 0.23, 1.2))
self.play(GrowFromCenter(rule), run_time=min(trk.duration * 0.12, 0.8))
self.play(Write(title), run_time=min(trk.duration * 0.30, 1.8))
self.play(FadeIn(subtitle, shift=UP * 0.2), FadeIn(sub2), run_time=min(trk.duration * 0.25, 1.5))
self.wait(0.2)
self.play(FadeOut(slide_group))
def _slide_step1_parity_split(self):
header = stage_header("1", "Split by Parity").to_edge(UP, buff=0.55)
rule = h_rule().next_to(header, DOWN, buff=0.18)
prompt = Text(
"Every integer n is even or odd:",
font_size=SMALL_FS, color=FG
).next_to(rule, DOWN, buff=0.4).align_to(rule, LEFT)
system = MathTex(
r"n = 2k\quad \text{or}\quad n = 2k+1,\; k\in\mathbb{Z}",
font_size=BODY_FS, color=FG
).next_to(prompt, DOWN, buff=0.34)
meaning = MathTex(
r"\Rightarrow\; \text{analyze } n^2 \pmod{8} \text{ in two cases}",
font_size=SMALL_FS, color=ACCENT
).next_to(system, DOWN, buff=0.38)
box = SurroundingRectangle(meaning, color=ACCENT, buff=0.16, corner_radius=0.12, stroke_width=2.5)
note = Text(
"Case 1: n even. Case 2: n odd.",
font_size=TINY_FS, color=MUTED
).next_to(box, DOWN, buff=0.25)
slide_group = VGroup(header, rule, prompt, system, meaning, box, note)
self._fit_to_safe_area(slide_group)
with self.voiceover(
"Let n be any integer. We split into two cases based on parity: even or odd."
) as trk:
self.play(FadeIn(header), GrowFromCenter(rule), run_time=0.6)
self.play(Write(prompt), run_time=0.6)
self.play(Write(system), run_time=min(trk.duration * 0.45, 2.2))
self.play(Write(meaning), Create(box), run_time=0.9)
self.play(FadeIn(note), run_time=0.6)
self.wait(0.2)
self.play(FadeOut(slide_group))
def _slide_step2_even_case(self):
header = stage_header("2", "Case 1: n Even").to_edge(UP, buff=0.55)
rule = h_rule().next_to(header, DOWN, buff=0.18)
l1 = MathTex(r"n = 2k \Rightarrow n^2 = (2k)^2 = 4k^2", font_size=SMALL_FS, color=FG).next_to(rule, DOWN, buff=0.35)
b1 = MathTex(
r"k=2m \Rightarrow n^2 = 4(2m)^2 = 16m^2 = 8(2m^2) \equiv 0 \pmod{8}",
font_size=TINY_FS + 1, color=GOOD
).next_to(l1, DOWN, buff=0.28)
b2 = MathTex(
r"k=2m+1 \Rightarrow n^2 = 4(2m+1)^2 = 16m^2+16m+4",
font_size=TINY_FS + 1, color=FG
).next_to(b1, DOWN, buff=0.20)
b3 = MathTex(
r"= 8(2m^2+2m)+4 \equiv 4 \pmod{8}",
font_size=TINY_FS + 1, color=GOOD
).next_to(b2, DOWN, buff=0.15)
concl = MathTex(
r"\therefore\; n \text{ even } \Rightarrow n^2 \equiv 0 \text{ or } 4 \pmod{8}",
font_size=SMALL_FS, color=ACCENT
).next_to(b3, DOWN, buff=0.32)
gbox = SurroundingRectangle(concl, color=ACCENT, buff=0.14, corner_radius=0.12, stroke_width=2.5)
slide_group = VGroup(header, rule, l1, b1, b2, b3, concl, gbox)
self._fit_to_safe_area(slide_group)
with self.voiceover(
"If n is even, write n equals two k. Then n squared is four k squared."
"If k is even, we get remainder zero mod eight. If k is odd, we get remainder four mod eight."
) as trk:
self.play(FadeIn(header), GrowFromCenter(rule), run_time=0.6)
self.play(Write(l1), run_time=0.8)
self.play(Write(b1), run_time=0.9)
self.play(Write(b2), run_time=0.9)
self.play(Write(b3), run_time=0.7)
self.play(Write(concl), Create(gbox), run_time=min(trk.duration * 0.25, 1.3))
self.wait(0.2)
self.play(FadeOut(slide_group))
def _slide_step3_odd_case(self):
header = stage_header("3", "Case 2: n Odd").to_edge(UP, buff=0.55)
rule = h_rule().next_to(header, DOWN, buff=0.18)
cond = MathTex(
r"n = 2k+1 \Rightarrow n^2 = (2k+1)^2 = 4k^2+4k+1 = 4k(k+1)+1",
font_size=TINY_FS + 1, color=FG
).next_to(rule, DOWN, buff=0.38)
kres = MathTex(
r"k(k+1) \text{ is even } \Rightarrow k(k+1)=2m \text{ for some } m\in\mathbb{Z}",
font_size=TINY_FS + 1, color=ACCENT
).next_to(cond, DOWN, buff=0.30)
rows = VGroup(
MathTex(r"n^2 = 4(2m) + 1", font_size=SMALL_FS, color=FG),
MathTex(r"= 8m + 1", font_size=SMALL_FS, color=FG),
MathTex(r"\equiv 1 \pmod{8}", font_size=SMALL_FS, color=GOOD),
).arrange(DOWN, buff=0.16, aligned_edge=LEFT).next_to(kres, DOWN, buff=0.32)
#cutoff = Text(
# "So odd n always gives remainder 1 modulo 8.",
# font_size=TINY_FS, color=MUTED, weight=BOLD
#).next_to(rows, DOWN, buff=0.30)
slide_group = VGroup(header, rule, cond, kres, rows)
self._fit_to_safe_area(slide_group)
with self.voiceover(
"If n is odd, n squared is four k times k plus one, plus one."
"Since consecutive integers have one even factor, k times k plus one is even."
"So n squared is congruent to one mod eight."
) as trk:
self.play(FadeIn(header), GrowFromCenter(rule), run_time=0.6)
self.play(Write(cond), run_time=1.0)
self.play(Write(kres), run_time=0.9)
self.play(LaggedStart(*[FadeIn(r, shift=RIGHT * 0.2) for r in rows], lag_ratio=0.2),
run_time=min(trk.duration * 0.45, 2.2))
#self.play(FadeIn(cutoff), run_time=0.7)
self.wait(0.2)
self.play(FadeOut(slide_group))
def _slide_conclusion(self):
header = styled_title("Conclusion", font_size=TITLE_FS).to_edge(UP, buff=0.55)
rule = h_rule().next_to(header, DOWN, buff=0.22)
answer = MathTex(
r"\boxed{n^2 \equiv 0,\;1,\;\text{or }4 \pmod{8}}",
font_size=BODY_FS + 2, color=GOOD
).next_to(rule, DOWN, buff=0.55)
verify = Text(
"Even n gives 0 or 4; odd n gives 1 modulo 8.",
font_size=SMALL_FS, color=FG
).next_to(answer, DOWN, buff=0.42)
final_note = Text(
"Examples: 16 ≡ 0, 25 ≡ 1, 36 ≡ 4 (mod 8)",
font_size=SMALL_FS, color=ACCENT, weight=BOLD
).next_to(verify, DOWN, buff=0.28)
box = SurroundingRectangle(answer, color=GOOD, buff=0.25, corner_radius=0.15, stroke_width=3)
slide_group = VGroup(header, rule, answer, verify, final_note, box)
self._fit_to_safe_area(slide_group)
with self.voiceover(
"Therefore the only possible remainders for a perfect square modulo eight are zero, one, and four."
) as trk:
self.play(FadeIn(header), GrowFromCenter(rule), run_time=0.7)
self.play(Write(answer), Create(box), run_time=min(trk.duration * 0.45, 1.9))
self.play(FadeIn(verify), run_time=0.8)
self.play(FadeIn(final_note), run_time=0.7)
self.wait(0.3)
self.play(FadeOut(slide_group))
def _slide_end_card(self):
logo_path = Path(__file__).with_name("hameefy_logo.png")
ref_star = Star(n=5, outer_radius=0.9, inner_radius=0.42,
fill_color=ACCENT, fill_opacity=0.9, stroke_width=0)
ref_star.scale(0.7)
if logo_path.exists():
emblem = ImageMobject(str(logo_path))
emblem.scale_to_fit_height(ref_star.height)
else:
emblem = ref_star
emblem.move_to(UP * 2.5)
main_msg = Text(
"Hameefy's Visual Maths Series",
font_size=HEAD_FS + 12, color=ACCENT, weight=BOLD
).next_to(emblem, DOWN, buff=0.35)
sub_msg = Text(
"Perfect squares modulo 8: 0, 1, or 4",
font_size=SMALL_FS, color=FG, line_spacing=1.35
).next_to(main_msg, DOWN, buff=0.4)
rule = h_rule(4.0).next_to(sub_msg, DOWN, buff=0.45)
tag = Text(
"Parity split + modular arithmetic",
font_size=TINY_FS, color=MUTED
).next_to(rule, DOWN, buff=0.25)
slide_group = Group(emblem, main_msg, sub_msg, rule, tag)
self._fit_to_safe_area(slide_group)
with self.voiceover(
"A parity split makes the proof immediate. Thanks for watching."
"Like, share, and follow! Use comment section to ask questions. See you in the next one!"
) as trk:
self.play(FadeIn(emblem), run_time=min(trk.duration * 0.25, 1.2))
self.play(Write(main_msg), run_time=min(trk.duration * 0.30, 1.5))
self.play(FadeIn(sub_msg, shift=UP * 0.15), run_time=min(trk.duration * 0.25, 1.2))
self.play(GrowFromCenter(rule), FadeIn(tag), run_time=min(trk.duration * 0.20, 1.0))
self.wait(0.6)