from manim import *
class S1(Scene):
    def construct(self):
        t3 = Tex("$1000^{th}$ term of :-  \\\  1,2,2,3,3,3,4,4,4,4 $\dotsb$").to_edge(UP).scale(1.5)
        br = BackgroundRectangle(t3,buff=0.5,color=YELLOW_A)
        self.play(Write(t3),FadeIn(br))
        self.wait()
        t1 = Text("Approach",font="Comic Sans").next_to(t3,DOWN,buff=1).scale(2).set_color_by_gradient(RED,BLUE,GREEN)
        self.play(Write(t1),run_time = 1.5)
        self.wait()
        t2 = Tex("We will try to find the $10^{th}$\\\or $100^{th}$ in order to \\\ observe any kind of pattern",line_spacing = 2).next_to(t1,DOWN,buff=1).scale(1.5)
        self.play(Write(t2),Indicate(t1),run_time = 2.5)
        self.wait(4)
        self.play(*map(FadeOut, self.mobjects),run_time=1)
class S2(Scene):
    def construct(self):
        t1 =Text("Re-Writing the pattern").to_edge(UP).scale(1.5).set_color_by_gradient(RED,BLUE,GREEN)
        self.play(Create(t1))
        self.wait()
        q = MathTex(
            """
            1 \\\\
            2  2 \\\\
            3  3  3 \\\\
            4  4  4  4 \\\\
            \\vdots
            """
        ,tex_environment="flushleft").scale(2).shift(LEFT*3.5+DOWN*0.5).set_color_by_gradient(YELLOW_A,YELLOW_B,YELLOW_C,YELLOW_D)
        self.play(Write(q),FadeOut(t1),run_time = 2.5)
        self.wait(2.5)
        q2 = MathTex("1 ","\\fbox{ 1 }","\\\\2  2","\\fbox{ 3 } ","\\\\3  3  3","\\fbox{ 6 }"," \\\\4  4  4  4","\\fbox{ 10 }","\\\\ \\vdots",tex_environment="flushleft").scale(1.5).shift(LEFT*3.5+DOWN*0.5).set_color_by_gradient(YELLOW_A,YELLOW_B,YELLOW_C,YELLOW_D)
        self.play(ReplacementTransform(q,q2),run_time = 2.5)
        
        t2 = Tex("$\\rightarrow$till  $1^{ st }$ term\\\\","$\\rightarrow$till  $3^{ rd }$ term\\\\","$\\rightarrow$till  $6^{ th }$ term\\\\","$\\rightarrow$till  $10^{ th }$ term\\\\",line_spacing=2).set_color_by_gradient(BLUE_A,BLUE_B,BLUE_C,BLUE_D).to_edge(RIGHT,buff=1).scale(1.5)
        self.play(q[1::2].animate.set_color(WHITE),FadeIn(t2),Indicate(q2[1::2]),run_time = 4.5)
        self.wait(3.5)
        t3 = MathTex("1").set_color_by_gradient(GREEN_A,GREEN_B,GREEN_C,GREEN_D).to_corner(UL).shift(RIGHT*2.5).scale(1.5)
        self.play(Write(t3))
        self.wait()
        t4 = MathTex("1","+ 2").set_color_by_gradient(GREEN_A,GREEN_B,GREEN_C,GREEN_D).to_corner(UL).shift(RIGHT*2.5).scale(1.5)
        a1 = Arrow(t4[1],q2[3])
        b1 = Brace(t4,direction=DOWN)
        self.play(TransformMatchingTex(t3,t4),Create(a1),FadeIn(b1),run_time = 2.5)
        self.wait(2.5)
        t5 = MathTex("1","+ 2","+3").set_color_by_gradient(GREEN_A,GREEN_B,GREEN_C,GREEN_D).to_corner(UL).shift(RIGHT*2.5).scale(1.5)
        a2 = Arrow(t5[2],q2[5])
        b2 = Brace(t5,direction=DOWN)
        self.play(TransformMatchingTex(t4,t5),Create(a2),FadeOut(a1),FadeOut(b1),FadeIn(b2),run_time = 2.5)
        self.wait(2.5)
        t6 = MathTex("1","+ 2","+3","+4").set_color_by_gradient(GREEN_A,GREEN_B,GREEN_C,GREEN_D).to_corner(UL).shift(RIGHT*2.5).scale(1.5)
        a3 = Arrow(t6[3],q2[7])
        b3 = Brace(t6,direction=DOWN)
        self.play(TransformMatchingTex(t5,t6),Create(a3),FadeOut(a2),FadeOut(b2),FadeIn(b3),run_time = 3)
        self.wait(3)
        t7 = MathTex("1","+ 2","+3","+4\dotsb","= n \cdot {n+1 \over 2}").set_color_by_gradient(RED_A,RED_B,RED_C,RED_D).to_corner(UL).shift(RIGHT*2.5).scale(1.5)
        self.play(TransformMatchingTex(t6,t7),FadeOut(a3),FadeOut(b3),FadeOut(t2),run_time = 3.5)
        self.wait(3.5)
        t8 = Text("So we can observe that\nsum of n natural numbers\ngives the last term,\nwhere n will be placed\nin the series").to_edge(RIGHT,buff=1).shift(DOWN*1.5).set_color_by_gradient(TEAL_E,TEAL_D,TEAL_C,TEAL_B,TEAL).scale(0.75)
        self.play(AddTextWordByWord(t8),run_time = 5)
        self.play(FocusOn(t8),Create(SurroundingRectangle(t8)),run_time=2)
        self.wait(3.5)
        b4 = Brace(t7,direction=RIGHT)
        b4_tex = b4.get_text("this gives the\\\\","position of n\\\\","(last one) in\\\\the series").set_color(PURPLE).shift(DOWN*0.25)
        self.play(FadeIn(b4),GrowFromCenter(b4_tex),run_time = 3.5)
        self.wait(2.5)
        self.play(*map(FadeOut, self.mobjects),run_time=1)
class S3(Scene):
    def construct(self):
        t1 = MathTex("n \cdot {(n+1) \over 2} \\rightarrow ","\\text{ Term where n is in Series }").scale(1.5).set_color(YELLOW)
        self.play(Write(t1),t1[1].animate.set_color(BLUE),run_time= 2)
        self.wait(2)
        eg = Tex("Example", "$\\rightarrow$" ,"Sum of first 4 natural numbers is 10 \\\\which means $10^{ th }$ term of the series will be 4").shift(DOWN)
        eg[0].set_color(YELLOW)
        eg[1].set_color(RED)
        self.play(t1.animate.shift(UP*1.5).scale(1),Write(eg),run_time = 5)
        self.wait(2.5)
        s = SurroundingRectangle(eg,buff=0.8)
        self.play(ShowCreationThenDestruction(s))
        self.wait(2.5)
class S4(Scene):
    def construct(self):
        t1 = Tex("So for $1000^{ th }$ term :").to_edge(UP).set_color(RED_E).scale(1.5)
        self.play(Write(t1),run_time = 1.5)
        self.wait(1.5)
        t2 = MathTex("n \cdot {(n+1) \over 2} = ","1000").next_to(t1,DOWN,buff=1).set_color(BLUE).scale(1.5)
        t2[1].set_color(GREEN)
        bg = BackgroundRectangle(t2,color = WHITE,fill_opacity=0.15,buff=0.5)
        self.play(Write(t2),FadeIn(bg),run_time = 2.5)
        self.wait(2.5)
        t3 = MathTex("\\implies \ n^2+n=2000").next_to(t2,DOWN,buff=1).scale(1.5).shift(LEFT*3.5).set_color(YELLOW)
        self.play(Write(t3),run_time = 3.5)
        self.wait(3.5)
        b1 = Brace(t3,direction=RIGHT,buff=1)
        b1_tex = b1.get_tex("\\text{So after solving this}\\\\ \\text{quadratic we will get}\\\\ \\text{ two values of } n \\\\","-45.22,","44.22").set_color(TEAL).scale(1).shift(DOWN)
        self.play(FadeIn(b1),GrowFromCenter(b1_tex),run_time = 3.5)
        self.wait(3.5)
        b2 = Brace(b1_tex[1],direction=LEFT)
        b2_tex = b2.get_text("Our term can not be negative\\\\so we can ignore -45.22").set_color(GOLD)
        self.play(FadeIn(b2),GrowFromCenter(b2_tex),run_time = 1.5)
        self.wait(1.5)
        self.play(FadeOut(b1_tex[1]))
        self.wait()
        self.play(FadeOut(b2),FadeOut(b2_tex))
        self.wait()
        t4 = Tex("So $44.22$ means $1000^{ th }$ term would be \\\\","45").set_color(PURPLE).next_to(t3,DOWN,buff=1).scale(1)
        t4[1].set_color(MAROON)
        self.play(Write(t4),run_time = 2.5)
        self.wait()
        self.play(Indicate(t4[1]),run_time = 1.5)
        self.wait(1.5)
        self.play(*map(FadeOut, self.mobjects),run_time=1)
class S5(Scene):
    def construct(self):
        t1 = Text("Clarification").to_edge(UP).scale(1.5).set_color(RED)
        self.play(Write(t1),run_time= 1.5)
        self.wait(1.5)
        t2 = MathTex("{44 \cdot (45)\over 2} = 990").next_to(t1,DOWN,buff=1).set_color(BLUE) 
        bg = BackgroundRectangle(t2,color = WHITE,fill_opacity=0.15,buff=0.5)   
        t3 = Tex("which means $990^{th}$ term would be $44$ so \\\\ $45$ must be $1000^{th}$ term which is our answer").set_color(GREEN).next_to(t2,DOWN,buff=1).scale(1)
        self.play(Write(t2),FadeIn(bg),run_time = 3.5)
        self.wait(2.5)
        self.play(Write(t3),run_time = 3.5)
        self.wait(3.5)
        self.add(Tex("$45$").next_to(t3,DOWN,buff=1).set_color_by_gradient(RED,BLUE).scale(2))
        self.wait(2)
        self.play(*map(FadeOut, self.mobjects),run_time=1)