...work in progress, the next iteration currently crashes
Last active
January 20, 2026 23:20
-
-
Save uwezi/0f8098b5f1fe5d7daa4dc6b69cb950c9 to your computer and use it in GitHub Desktop.
[long division] Long division in Manim. #manim #math #animation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class longdiv(Scene): | |
| def construct(self): | |
| # https://www.calculatorsoup.com/calculators/math/longdivision.php | |
| divisor = 123 | |
| dividend = 764556 | |
| divisor_mobj = VGroup( | |
| *Tex(f"{divisor}")[0] | |
| ) | |
| remainder = 0 | |
| divisor_mobj.arrange_in_grid( | |
| rows=1, | |
| cols=len(divisor_mobj), | |
| col_widths=[0.5]*len(divisor_mobj), | |
| col_alignments="c"*len(divisor_mobj) | |
| ).to_corner(UL).shift(DOWN) | |
| dividend_mobj = VGroup( | |
| *Tex(f"{dividend}")[0] | |
| ) | |
| dividend_mobj.arrange_in_grid( | |
| rows=1, | |
| cols=len(dividend_mobj), | |
| col_widths=[0.5]*len(dividend_mobj), | |
| col_alignments="c"*len(dividend_mobj) | |
| ).next_to(divisor_mobj,RIGHT,aligned_edge=DOWN,buff=0.5) | |
| angle = VMobject().set_points_as_corners( | |
| [ | |
| (divisor_mobj[-1].get_bottom()+dividend_mobj[0].get_bottom())/2+0.1*DOWN, | |
| (divisor_mobj[-1].get_top()+dividend_mobj[0].get_top())/2+0.1*UP, | |
| dividend_mobj[-1].get_top()+RIGHT+0.1*UP | |
| ] | |
| ) | |
| self.add(divisor_mobj,dividend_mobj,angle) | |
| acc = dividend | |
| i = int(np.log10(dividend))-int(np.log10(divisor)) | |
| acc = dividend / 10**(i) | |
| j = 0 # iteration number | |
| self.wait() | |
| while (j<3): | |
| temp = acc | |
| res = int(temp / divisor) | |
| sum = res*divisor | |
| acc = (temp - sum)*10 | |
| rem = int(acc) | |
| print(temp,res,sum,acc,rem) | |
| sum_mobj = VGroup( | |
| *Tex(f"{sum}")[0] | |
| ) | |
| sum_mobj.arrange_in_grid( | |
| rows=1, | |
| cols=len(sum_mobj), | |
| col_widths=[0.5]*len(sum_mobj), | |
| col_alignments="c"*len(sum_mobj) | |
| ) | |
| sum_mobj.shift(dividend_mobj[i-1+j].get_bottom()-sum_mobj[-1].get_bottom()+(j+0.5)*DOWN) | |
| rem_mobj = VGroup( | |
| *Tex(f"{rem}")[0] | |
| ) | |
| rem_mobj.arrange_in_grid( | |
| rows=1, | |
| cols=len(rem_mobj), | |
| col_widths=[0.5]*len(rem_mobj), | |
| col_alignments="c"*len(rem_mobj) | |
| ) | |
| rem_mobj.shift(dividend_mobj[i+j].get_bottom()-rem_mobj[-1].get_bottom()+(j+1)*DOWN) | |
| res_mobj = Tex(f"{res}").next_to(dividend_mobj[i-1+j],UP) | |
| self.play(Write(sum_mobj)) | |
| self.wait() | |
| self.play(Write(res_mobj)) | |
| self.wait() | |
| self.play(Write(rem_mobj[0:-1])) | |
| self.wait() | |
| self.play(FadeIn(rem_mobj[-1],shift=DOWN)) | |
| self.wait() | |
| j = j+1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
