Solving the Fibonacci Sequence with Matrix Exponentiation

แชร์
ฝัง
  • เผยแพร่เมื่อ 15 พ.ย. 2024

ความคิดเห็น • 215

  • @killuazoldyck7705
    @killuazoldyck7705 4 ปีที่แล้ว +126

    Its always that one Indian person that explains it better than your learning materials and the teacher combined

  • @ITACHIUCHIHA-dr8sz
    @ITACHIUCHIHA-dr8sz ปีที่แล้ว +12

    6 years later, and still the best video explaining this on youtube!

    • @gkcs
      @gkcs  2 หลายเดือนก่อน

      Thank you!

  • @alessandrocamillo4939
    @alessandrocamillo4939 6 ปีที่แล้ว +53

    You’ve been the first and the best to clearly explain the math behind fast Fibonacci matrix computation .

    • @gkcs
      @gkcs  6 ปีที่แล้ว +5

      Thanks!

  • @shreyaghosh2956
    @shreyaghosh2956 8 หลายเดือนก่อน +1

    Wow! Thank you for making it so simple to understand. This was the clearest explanation of matrix exponentiation.

  • @shreyasahu481
    @shreyasahu481 7 ปีที่แล้ว +91

    when the yellow tee turns blue magically! nice video though :)

    • @gkcs
      @gkcs  7 ปีที่แล้ว +5

      Haha :-P
      Thanks Shreya!

    • @abhaypatil2000
      @abhaypatil2000 4 ปีที่แล้ว +4

      also he wore spectacles

  • @manishankar1593
    @manishankar1593 6 ปีที่แล้ว +19

    I loved the Maths involved into this problem, fascinated!

  • @iprakhar22
    @iprakhar22 7 ปีที่แล้ว +46

    Appreciate your work a lot, Man!
    Something I like about you is that you choose your words very carefully before speaking just so that every kind of audience can understand you.

    • @gkcs
      @gkcs  7 ปีที่แล้ว +2

      Thanks Prakhar!

  • @vidhansharma1787
    @vidhansharma1787 6 ปีที่แล้ว +8

    Thanks bhai....
    People like you help guys ,those are not from good colleges in ind.. to improve their competitive programming skills......

    • @Speak4Yourself2
      @Speak4Yourself2 5 ปีที่แล้ว +3

      Not even the best of colleges in India teach any Competitive Programming.

    • @Speak4Yourself2
      @Speak4Yourself2 5 ปีที่แล้ว +1

      @ 'coDiNG cULtUre' starts from You. When you code, (and code well, with time and practice) your friends/juniors are inspired to follow in your footsteps. Over the years this inspiration spreads as a ripple effect batch after batch. Then the college calls itself as having good 'coDiNG cULtUre'.

  • @atulsaswat1153
    @atulsaswat1153 4 ปีที่แล้ว +6

    At 17:45, when we do the final multiplication, we should consider f(1) = 1 and f(0) = 0 giving us f(14) = 377 & f(13) = 233. That doesn't defeat the purpose of the understanding though. Your explanations have a good buildup and that is probably why it's so appealing.

    • @rishavsaha5254
      @rishavsaha5254 3 ปีที่แล้ว

      Yes, you are correct but in the beginning of this video, he assumed both f(1) and f(0) equal to 1.

  • @abhishekkarn8918
    @abhishekkarn8918 7 ปีที่แล้ว

    I can add one thing here if anyone is stuck until what power do we have to calculate the powers:
    just do: 2^(floor(ln(n)) and you will get the required number. For n=13, you will get 2^3, which is 8.
    Hope it proved to be of some help.
    And Thanks a Lot Gaurav for such a brilliant video. You are literally one of the best. :)

    • @gkcs
      @gkcs  7 ปีที่แล้ว

      Thanks Abhishek :-)

  • @akhilesh59
    @akhilesh59 2 ปีที่แล้ว +2

    Best Explanation so far with reason behind each and every step! Loved it... Thank you Sir.. ♥

  • @theIcemanYT
    @theIcemanYT 5 ปีที่แล้ว +4

    the channel I should have found a year ago. Awesome explanation.

    • @gkcs
      @gkcs  5 ปีที่แล้ว

      Thanks! 😁

  • @dugong369
    @dugong369 5 ปีที่แล้ว

    For those who aren't aware, storing the lower powers isn't needed (see wikipedia article on Exponentiation by Squaring for example). Following a sequence of squaring and multiplication determined by the binary bits of the exponent eliminates the need to store those intermediate results. It's probably better that you explained it the way you did though because that avoids a complication which isn't relevant to the main point, O(log n).

  • @andermerketegi1177
    @andermerketegi1177 2 ปีที่แล้ว +1

    It really helped me understand how to apply Matrix Exponentiation for my particular problem. Great video, thanks!

  • @ashishchourasia2830
    @ashishchourasia2830 ปีที่แล้ว

    The best explanation for this on the internet

  • @saranshdabas4223
    @saranshdabas4223 7 ปีที่แล้ว

    Learning a lot from your videos, they are unlike any other sources available for competitive programming...

    • @gkcs
      @gkcs  7 ปีที่แล้ว

      Thanks Saransh!

  • @adoq
    @adoq 2 หลายเดือนก่อน +1

    i was just looking for a way to find fibonacci numbers quickly with pen and paper, i didnt get my answer but i got valuable dynamic programming knowledge lol. works either way

    • @gkcs
      @gkcs  2 หลายเดือนก่อน

      Excellent!

  • @tanishq2766
    @tanishq2766 2 ปีที่แล้ว

    The way your eyes shined, when you said THE MATRIX !
    NEO FOREVER

  • @LuisaGrigorescu
    @LuisaGrigorescu 4 ปีที่แล้ว

    Great explanation! You made math much easier to understand. All the explanations from the internet are to mathematical and imply having prior knowledge to all these terms and formulaes.

  • @Rajat_maurya
    @Rajat_maurya ปีที่แล้ว

    6 years later also helping many of us

  • @viditmathur8437
    @viditmathur8437 6 ปีที่แล้ว +6

    your explanations are awesome , I really like the mathematical explanations . This video really helped me understand the log(n) approach to find fibonacci sequence which i couldn't understand from texts :D.

    • @gkcs
      @gkcs  6 ปีที่แล้ว

      Thanks Vidit!

  • @FeroChau
    @FeroChau 6 ปีที่แล้ว +8

    This is amazing! It's so satisfying to watch this.

  • @NiceBot724
    @NiceBot724 6 ปีที่แล้ว +2

    This video needs a lot more likes than just hundreds

    • @gkcs
      @gkcs  6 ปีที่แล้ว

      I agree 😉

  • @ZMacZ
    @ZMacZ 2 ปีที่แล้ว

    Less coffee or more meditation, enhances calm during explanation.

  • @subhodip5293
    @subhodip5293 5 ปีที่แล้ว

    Great Teaching. Your energy is very engaging to the viewer and your explanations are clear and intuitive.

  • @udaysaiphanindra3138
    @udaysaiphanindra3138 4 ปีที่แล้ว

    what an explanation sir, MIND BLOCK

  • @vijaypratap356
    @vijaypratap356 4 ปีที่แล้ว

    bro i am frustated from data structure and algorithm but there is some which does not let it go.when i will see ur videos they encourages me to learn but i dont know what exactly to learn for understanding complex problems of ds and algo.how do you solve the problems in minutes thats really amazing.

  • @Manojshankaraj
    @Manojshankaraj 7 ปีที่แล้ว +2

    Great video Gaurav! Please keep posting such wonderful videos. Thanks a lot!

  • @beonthego8725
    @beonthego8725 2 ปีที่แล้ว

    Bless you Gaurav. Thank you

  • @Chesstreamer
    @Chesstreamer 5 ปีที่แล้ว

    Just scrolling through your videos and bazzinga, found this fibonacci video,i have once used the code without understanding it somewhere on codechef but now i have understand it thoroughly,very simple and lucid exlplanation of yours helped me a lot.can u do some more problems on dp like state reduction and those involving trees tha would be of great help

  • @sohanursohan2426
    @sohanursohan2426 4 ปีที่แล้ว

    It's helpful.Good work brother.
    From Bangladesh ❤

  • @themusicalmonk2732
    @themusicalmonk2732 3 ปีที่แล้ว

    You are Simply Genius.. man..

  • @abhimanyubahree1455
    @abhimanyubahree1455 5 ปีที่แล้ว +1

    Wow solid explaination!!! Very easy to understand

  • @srijanpaul
    @srijanpaul 3 ปีที่แล้ว

    I remember this from the book SICP! It was an exercise in the first chapter iirc. I saw the solution on community scheme wiki and was very impressed by this way of thinking about the problem.

    • @omshree901
      @omshree901 2 ปีที่แล้ว

      What is the full form of SICP?

  • @sumit-kushwah
    @sumit-kushwah 9 หลายเดือนก่อน

    Thank you Gaurav 💌

  • @fenil861
    @fenil861 2 ปีที่แล้ว

    Thanks for this video. It helped me in gaining good insights for the approach to this problem.

  • @khudkikhoz
    @khudkikhoz 3 ปีที่แล้ว

    Exactly this depth all we need... tx brother

  • @amoghdesai9138
    @amoghdesai9138 5 ปีที่แล้ว

    Should have this channel earlier. Very concise and useful explanation :)

  • @aayush_dutt
    @aayush_dutt 5 ปีที่แล้ว +3

    Elegantly explained bro!
    Thanks a lot

    • @gkcs
      @gkcs  5 ปีที่แล้ว +1

      Thanks!

  • @MegaAkash123456
    @MegaAkash123456 7 ปีที่แล้ว +1

    Great tutorial!
    matrix exponentiation is very useful in solving such linear recurrences in dp problems.

    • @gkcs
      @gkcs  7 ปีที่แล้ว

      Thanks Akash!

  • @vanshgupta24
    @vanshgupta24 ปีที่แล้ว

    the curious thing about fibonacci is that every fibonacci is golden ratio times bigger than the previous fibonacci , like 8 and 5 are in the ratio of 1.6 or 8 is golden ratio times bigger than 5. early numbers dont show it but eventually the pairs starts to converge to golden ratio, so for any large n the fibonacci is phi raised to some expnonent of n (check yourself). so you can just return pow(phi,n)*base , base here is any suitable fibonacci number that has significantly converged, and n is new shifted target .

    • @gkcs
      @gkcs  ปีที่แล้ว

      You won't get the exact number using this, but you will get an approximation.

  • @ManishKumar-kw7qe
    @ManishKumar-kw7qe 18 วันที่ผ่านมา

    Great explanation

  • @9871aa
    @9871aa 3 ปีที่แล้ว

    If you set F(0) = 0, like wikipedia says, you could have even a simpler algorithm:
    raise matrix to the n-th power and get the 1-st row, 1-st column element.

    • @gkcs
      @gkcs  3 ปีที่แล้ว

      Good idea 👍

  • @purvampujari
    @purvampujari 6 ปีที่แล้ว +3

    Thanks bro , helped me solve medium level ( broken clock ) problem of this feb18 long !

  • @shaswatdas6553
    @shaswatdas6553 4 ปีที่แล้ว +1

    17:45 "Both of them will be 1 and 1"

  • @uarangat
    @uarangat 4 ปีที่แล้ว

    Wonderful teaching brother, keep it up. Helped a lot. Many thanks.

  • @ronaldvonk
    @ronaldvonk 5 ปีที่แล้ว +1

    Very nice explanation and nice correction :-). The only thing is: even though this way of calculation may seem efficient; the total sum wil rise exponential and will be difficult to hold in memory; even for low values of N!

    • @gkcs
      @gkcs  5 ปีที่แล้ว +1

      Yes that's true, although most of the questions around this have us taking a remainder of the result. So ans % m turns out to be small enough to fit in memory :)

    • @ronaldvonk
      @ronaldvonk 5 ปีที่แล้ว

      But you will store every 2^N th step of the Fibonacci sequence in memory, right Gaurav Sen? The 2^3=8th step is only [[34,21],[21,[13]] , but the 2^5=32th step is already [[3.524.578, 2.178.309],[2.178.309, 1.346.269]] and rising exponential; resulting is a lot of digits. Which step will you take the modulo m and with what purpose?

    • @ronaldvonk
      @ronaldvonk 5 ปีที่แล้ว

      But you certainly gave me an idea for storing these exponential rising values as (mod 2^k) :-D. This may do the trick. Is that what you meant?

    • @gkcs
      @gkcs  5 ปีที่แล้ว

      @@ronaldvonk yup 😁

    • @ronaldvonk
      @ronaldvonk 5 ปีที่แล้ว

      Like: the actual stored values in the matrices too?

  • @shaziasamreen8584
    @shaziasamreen8584 4 ปีที่แล้ว

    Very good explanation

  • @jayshree9037
    @jayshree9037 4 ปีที่แล้ว +1

    Any topic become interesting when it's you 😁 also those spects give me granny vibes😄🤫
    Thanks😍

    • @gkcs
      @gkcs  4 ปีที่แล้ว

      😂😉

  • @parth7300
    @parth7300 2 หลายเดือนก่อน

    Goat explanation 🐐🐐

  • @sivasainagireddi7956
    @sivasainagireddi7956 2 ปีที่แล้ว

    such a great explination !!!! thanks broo

  • @nazrulhassan6310
    @nazrulhassan6310 3 ปีที่แล้ว

    great explanation brother

  • @kiransfitnesstips4095
    @kiransfitnesstips4095 9 หลายเดือนก่อน

    Nice explanation bro...

  • @sarcaastech
    @sarcaastech 4 ปีที่แล้ว

    Great video .. I saw some other video on the same topic but it was not engaging like this one ... ✌️🔥

  • @the_dark_kerm
    @the_dark_kerm 2 ปีที่แล้ว

    Really great explanation

    • @gkcs
      @gkcs  2 ปีที่แล้ว +1

      Thank you 😁

  • @Snehaa2296
    @Snehaa2296 3 ปีที่แล้ว

    very cool intro GKCS!

  • @xiquandong1183
    @xiquandong1183 6 ปีที่แล้ว +2

    Very good explanation. Thanks !!

    • @gkcs
      @gkcs  6 ปีที่แล้ว

      Thanks!

  • @BluEx22329
    @BluEx22329 3 ปีที่แล้ว

    Sequences of threes? Wow that's the holy trinity. My mind is blown and im agnostic

  • @beautifultime9031
    @beautifultime9031 4 ปีที่แล้ว

    Very well explained. Thanks for sharing.

  • @siddhantsharma7107
    @siddhantsharma7107 4 ปีที่แล้ว

    You've explained it wonderfully. Wow!

  • @sunnysam69
    @sunnysam69 6 ปีที่แล้ว

    Amazing bro. Very nicely explained. Have been struggling to calculate fibonacci term efficiently.

    • @gkcs
      @gkcs  6 ปีที่แล้ว +1

      Glad it helped :-)

  • @joshua_dlima
    @joshua_dlima 14 วันที่ผ่านมา

    Enjoyed it, thank you!

    • @gkcs
      @gkcs  14 วันที่ผ่านมา +1

      Cheers!

  • @rethink20s
    @rethink20s 5 ปีที่แล้ว

    Great teaching skill bro...

  • @tanvirahmed-vu2xy
    @tanvirahmed-vu2xy 4 ปีที่แล้ว

    In which order i will multiply the matrix?? If i take the reverse order is it correct??

  • @RapartiChaitanya
    @RapartiChaitanya 4 ปีที่แล้ว

    Loved this explanation!

  • @zaid6527
    @zaid6527 ปีที่แล้ว

    thanks, very well explaned

  • @ravirayal7590
    @ravirayal7590 2 ปีที่แล้ว +1

    amazing and very informative video, thanks

  • @souryasaha4001
    @souryasaha4001 7 ปีที่แล้ว

    Nice Explanation. Keep up the good work.

    • @gkcs
      @gkcs  7 ปีที่แล้ว

      Thanks Sourya!

  • @priyabratamallick7629
    @priyabratamallick7629 6 ปีที่แล้ว

    Hey Gaurav , It is a wonderful explanation, but I found a small flaw in the explanation. i.e. f(14) is 377 not 610 and f(13) is 233 not 377. We are getting it wrongly beacuse you have considered f(0)=1 which is actually 0.
    Anyway thanks of your nice work dude.

    • @gkcs
      @gkcs  6 ปีที่แล้ว

      Thanks Priyabrata!

  • @umangbehl8152
    @umangbehl8152 2 ปีที่แล้ว

    good explanation

  • @angshumanbhowmik1020
    @angshumanbhowmik1020 4 ปีที่แล้ว

    Amazing video sir👍

  • @AmanSharma-hi3fd
    @AmanSharma-hi3fd 5 ปีที่แล้ว +1

    12:04 The Matrix XD , you are the chosen one.

  • @91vasanth
    @91vasanth 4 ปีที่แล้ว

    Wonderful explanation Gaurav. That smile at 18:19 haha ! And I have one doubt from that point. How did you say that to raise to the power we just took log(n) time? and you also said something about the number of bits to represent that number. Could you please explain me this? I was so satisfied with the understanding until I arrived at this point ! :(

    • @gkcs
      @gkcs  4 ปีที่แล้ว

      Hey Vasanth, thanks for the feedback 😁
      This link will help explain the logn bits:
      qr.ae/TWyJOx

  • @rajatparab8116
    @rajatparab8116 5 ปีที่แล้ว

    Awesome explanation
    Keep up this great work
    Thanks a lot broo👍

  • @ankitmishra-dp9tx
    @ankitmishra-dp9tx 4 ปีที่แล้ว

    Thanks alot for this video. Immensely helpful and love your passion while explaining the problem. Newly created fan :D

  • @tarunpatel8168
    @tarunpatel8168 4 ปีที่แล้ว

    that an awesome explanation, thankyou for the video

  • @prathameshautade2679
    @prathameshautade2679 3 ปีที่แล้ว

    Amazing explanation dude:)

  • @KaranDoshicool
    @KaranDoshicool 4 ปีที่แล้ว

    very well explained

  • @arreankit
    @arreankit 7 ปีที่แล้ว

    Thanks, Gaurav !! Nice Explanation.

    • @gkcs
      @gkcs  7 ปีที่แล้ว +1

      Thanks Ankit!

  • @TravellerMann
    @TravellerMann 6 ปีที่แล้ว +2

    Good efforts bro..keep it up

    • @gkcs
      @gkcs  6 ปีที่แล้ว

      Thanks!

  • @Pro3512
    @Pro3512 5 ปีที่แล้ว

    Why it is showing class not found exception when i ran the code in github in online IDE for JAVA ?

    • @gkcs
      @gkcs  5 ปีที่แล้ว

      Lmgtfy.

  • @manthankhorwal1477
    @manthankhorwal1477 4 ปีที่แล้ว

    Why can't we use implementation of pow function to modify it to directly calculate for matrix same as we do it for a number ?

  • @ktxh
    @ktxh 4 ปีที่แล้ว

    hi, shouldn't matrix^0 be an identity matrix?

  • @letsghoomo
    @letsghoomo ปีที่แล้ว

    good job Sir

  • @rahulsaxena5015
    @rahulsaxena5015 5 ปีที่แล้ว

    How do we find the matrix to be "exponentiated" in different questions?

  • @manishsakariya4595
    @manishsakariya4595 4 ปีที่แล้ว

    Nice work. Your videos can not be viewed in 2x speed. you already speak so quickly 😁😜

  • @securityintech
    @securityintech 4 ปีที่แล้ว

    at 4:20 I don't understand why do we require f(n-1) in the matrix, if the conditions are satisfied, we got [1 1] and f(1) and f(0), both of them are 1x2 and 2x1 matrix, resulting will be 1x1.

  • @monideepde7413
    @monideepde7413 7 ปีที่แล้ว

    Very nicely explained. Thank you

    • @gkcs
      @gkcs  7 ปีที่แล้ว

      +Monideep De Thanks!

  • @mansinawani8970
    @mansinawani8970 5 ปีที่แล้ว

    Thanks for this amazing video...

  • @kamilkostya8600
    @kamilkostya8600 2 ปีที่แล้ว

    How to multiply those 2 matrices [1 1] x [f(n-1) f(n-2]?

  • @kautukraj
    @kautukraj 4 ปีที่แล้ว +1

    Very helpful!

  • @mandavasairaghavendradines6582
    @mandavasairaghavendradines6582 4 ปีที่แล้ว

    Hey, great explanation. Nowadays you are doing more of system design stuff. I think once a while you can also post more these kind of videos

  • @jayseagal3681
    @jayseagal3681 3 ปีที่แล้ว

    Thank you so much bro

  • @QuantumStatic
    @QuantumStatic 4 ปีที่แล้ว

    Good Video but you could try speaking a little slow next time. good job,

  • @dikshantyadav1110
    @dikshantyadav1110 5 ปีที่แล้ว

    why did you added f(n-1) there, what is the need? at 4.21 time and even if we did why not take f(n-2) why only f(n-1)

    • @gkcs
      @gkcs  5 ปีที่แล้ว

      What's the recursion? Think over it.

    • @dikshantyadav1110
      @dikshantyadav1110 5 ปีที่แล้ว

      @@gkcs if we want to multiply 2*2 only take f(n) and f(n-2) , will it affect my result

  • @marcialabrahantes3369
    @marcialabrahantes3369 ปีที่แล้ว

    You can then use Cayley's - Hamilton's theorem on exponentiation for a linear solution to any Nth Fibonacci number 😜

    • @muditkhanna8164
      @muditkhanna8164 ปีที่แล้ว

      yeah but with matrix exponentiation its logarithmic complexity.

  • @misudey3281
    @misudey3281 7 ปีที่แล้ว

    Nice Video And good explanation :) liked it a lot :)

    • @gkcs
      @gkcs  7 ปีที่แล้ว

      Thanks Misu :-)

  • @longvu2033
    @longvu2033 4 ปีที่แล้ว

    very nice explanation =))

  • @tanishktiwari5112
    @tanishktiwari5112 7 ปีที่แล้ว

    thanks for the video bro!...nicely done!!!! can you please make a tutorial on bitmask+dp.......it would be greatly appreciated

    • @gkcs
      @gkcs  7 ปีที่แล้ว +1

      Hi tanishq, the video for that is already out!
      th-cam.com/video/OEthLiejmHk/w-d-xo.html
      I suggest you have a look upto the 18th minute. Thats contains the explanation of bitmask and dp.

    • @tanishktiwari5112
      @tanishktiwari5112 7 ปีที่แล้ว +1

      thanks......to be honest I like your videos very much ,they are way better than tushar roy's ......keep up the good work bro :-).

  • @sivanipentapati5538
    @sivanipentapati5538 7 ปีที่แล้ว +3

    can u post the link of code??