How can we multiply large integers quickly? (Karatsuba algorithm) - Inside code

Ғылым және технология

Source code: gist.github.com/syphh/0df7faf...
🔴 Learn graph theory algorithms: inscod.com/graphalgo
⚙ Learn dynamic programming: inscod.com/dp_course
💡 Learn to solve popular coding interview problems: inscod.com/50problems_course
⌛ Learn time and space complexity analysis: inscod.com/complexity_course
🔁 Learn recursion: inscod.com/recursion_course
NB: This video is ad-free, you can choose to support Inside code by purchasing one of the courses above or dropping a super thanks!
NB2: Discounts of courses above are permanent
I also post content on LinkedIn (inscod.com/linkedin) and Instagram (inscod.com/instagram)

Пікірлер: 161

  • @insidecode
    @insidecode Жыл бұрын

    Discover the new graph theory algorithms course: inscod.com/graphalgo 🔴 / \ 🔵-🔴 | | 🔴-🔵

  • @Paul-vg8vc
    @Paul-vg8vc Жыл бұрын

    I believe there's a typo at 6:01 - the fifth line should say "(a+b)(c+d)-ac-bd"

  • @markd2797
    @markd27972 жыл бұрын

    Jeez, you explain this very well. You don’t explain material in a generic way. Also, I love the animation.

  • @insidecode

    @insidecode

    2 жыл бұрын

    Oh thanks a lot!

  • @yaswanthnani6563
    @yaswanthnani65636 ай бұрын

    this is a very well done video, the animation and the color palate are really smooth and the explanation is clear as day.

  • @adityas8436
    @adityas8436 Жыл бұрын

    Great platform and very helpful!! Keep Going! One of the rare channels which explain algorithm design in such depth...

  • @richardtobing5012
    @richardtobing50122 жыл бұрын

    this is a very well done video, the animation and the color palate are really smooth and the explanation is clear as day. Subscribed.

  • @insidecode

    @insidecode

    2 жыл бұрын

    Thanks a lot for your comment!

  • @sahiljain3083
    @sahiljain3083 Жыл бұрын

    amazing explanation,short and brief...made the concept easy for me. thanks 😍

  • @ryansamarakoon8268
    @ryansamarakoon82682 жыл бұрын

    This is amazing!! I learnt so much not just about the algorithm, but key concepts like merge sort, masters method and more. I'm waiting for the day you blow up 🔥 def sending this one to my friends

  • @insidecode

    @insidecode

    2 жыл бұрын

    Amazing! Thanks for the comment and the support

  • @samarthtandale9121
    @samarthtandale91218 ай бұрын

    Great explanation ! Underrated 100%

  • @pirateboygaming2726
    @pirateboygaming27266 ай бұрын

    Best explanation of karatsuba algorithm.

  • @antoine2571
    @antoine257110 күн бұрын

    Exactly what I was looking for Thanks !

  • @infwin5944
    @infwin5944 Жыл бұрын

    Fantastic and easy to understand tutorial! Just want to point out that the last line of code might not work in the case of an odd number of digits, since you calculate half with n//2.

  • @TheSacredSteve
    @TheSacredSteve2 жыл бұрын

    Very well done video. I appreciate the care and quality.

  • @hansoflaherty9864
    @hansoflaherty9864 Жыл бұрын

    thank you!! best explanation of the concept imo

  • @sir_serhii
    @sir_serhii2 жыл бұрын

    One minor thing that is missing here is how to Actually calculate big numbers that don't fit regular programming primitives as Int or Double

  • @piyushsrivastava5581
    @piyushsrivastava55812 жыл бұрын

    Thank you for this video. Keep growing. It would be so great if in future you plan on making a course on data structures & algorithms- would probably be something to watch out for whenever computer geeks open youTube.

  • @insidecode

    @insidecode

    2 жыл бұрын

    Thanks for your suggestions

  • @saqlainsajid4067
    @saqlainsajid40672 жыл бұрын

    Some feedback about calculating the time complexity you say, the return statement has complexity O(n) but if you observe closely, the whole return statement is filled with O(1) operations, I think the function "ad_plus_bc" has complexity of T(n/2)+O(n), because it has a subtraction operation, subtracting/adding has complexity of O(n) The overall expression of complexity is correct. T(n) = 3T(n/2) + O(n) + O(1), where O(1) can be ignored in the presence of O(n)

  • @gorantlakarthik2709
    @gorantlakarthik27092 жыл бұрын

    Apart from good explanation. The entire presentation is also so satisfying and I can see u put a lot of work behind it. Do more of them we will keep supporting you sir.

  • @insidecode

    @insidecode

    2 жыл бұрын

    Thanks a lot!

  • @AnuPAMAMUsiC
    @AnuPAMAMUsiC2 ай бұрын

    this video saved my life! thanks

  • @markocastillo3485
    @markocastillo34852 жыл бұрын

    This is some high quality material. Really appreciate it.

  • @insidecode

    @insidecode

    2 жыл бұрын

    Thanks a lot!

  • @user-rf4vc7mt4d
    @user-rf4vc7mt4d9 ай бұрын

    Brilliant video. Thank you so much

  • @harshmangalamverma
    @harshmangalamverma7 ай бұрын

    thanks for the video explaination. also, note that 1.58 is read as one point five eight, not one point fifty eight.

  • @kshitijzutshi8278
    @kshitijzutshi82782 жыл бұрын

    Hi, PLEASE create more videos like this and Also add videos to playlist for easy lookup. Really appreciate it. Algo/problems related to Data science would be great as well.

  • @insidecode

    @insidecode

    2 жыл бұрын

    Thanks for your suggestion!

  • @matteofioretti8218
    @matteofioretti82189 ай бұрын

    Amazingly clear

  • @mohamedibrahimbehery3235
    @mohamedibrahimbehery32352 жыл бұрын

    Just wow! Thanks man... Keep up the good work!

  • @insidecode

    @insidecode

    2 жыл бұрын

    You're welcome!

  • @nihal-kabir
    @nihal-kabir10 ай бұрын

    teaching can't be better. thank you.

  • @ahmedalaaeldin7362
    @ahmedalaaeldin7362 Жыл бұрын

    Great explanation, Thank you

  • @lcarliner
    @lcarliner2 жыл бұрын

    With quantum computer memories sometime in the future, if read only quantum memories could be invented, then huge indexed table could be used for instantaneous results. For single value function like trig functions, read only index tables would have advantage of single clock speed and absolute accuracy, as it is my understanding that different approximations have needed accuracy for only portions of the number space.

  • @10_bhaveshbathija31
    @10_bhaveshbathija31 Жыл бұрын

    great explanation thanks alot for saving the day

  • @ravinderkaushik3394
    @ravinderkaushik33948 ай бұрын

    Brilliant explaination

  • @modysy1660
    @modysy16602 жыл бұрын

    This tip seems very helpful. Thank you for sharing

  • @insidecode

    @insidecode

    2 жыл бұрын

    You're welcome!

  • @mrwhosethebeast1898
    @mrwhosethebeast1898 Жыл бұрын

    amazing explaination bro!

  • @shlomighty
    @shlomighty10 ай бұрын

    Amazing explanation

  • @slavii5772
    @slavii57722 жыл бұрын

    Never knew about this,might come in handy Thanks :)

  • @insidecode

    @insidecode

    2 жыл бұрын

    You're welcome!

  • @donovanmurray7005
    @donovanmurray70053 ай бұрын

    Thank you this is so helpful :)

  • @someoneunknown2720
    @someoneunknown27202 жыл бұрын

    I never knew that there is another algorithm to multiply. Thanks for increasing my knowledge 😍😍🥰🙏✌️

  • @insidecode

    @insidecode

    2 жыл бұрын

    You're welcome!

  • @dhruvsingh1850
    @dhruvsingh1850 Жыл бұрын

    Subscribed Sir,Amazing work

  • @BELLAOUAR_Mahmoud
    @BELLAOUAR_Mahmoud2 жыл бұрын

    Ur explanation is very clear i hope to make more videos about unknown topics in computer science well done bro .

  • @insidecode

    @insidecode

    2 жыл бұрын

    Thankss

  • @BELLAOUAR_Mahmoud

    @BELLAOUAR_Mahmoud

    2 жыл бұрын

    ​@@insidecode u deserve more .

  • @AshwaqKHayat
    @AshwaqKHayat Жыл бұрын

    Thank you!!

  • @user-je3fk3ks5f
    @user-je3fk3ks5f8 ай бұрын

    informative, thanks

  • @enoon_23984
    @enoon_239847 ай бұрын

    beautiful ty

  • @poppymoonpaperie5458
    @poppymoonpaperie5458 Жыл бұрын

    wow this helped so much thanks youuuu!!!!

  • @insidecode

    @insidecode

    Жыл бұрын

    You're welcome!

  • @AlpPamuk
    @AlpPamuk2 жыл бұрын

    fascinating!

  • @jatinjain850
    @jatinjain850 Жыл бұрын

    Thx a lot

  • @baxtables
    @baxtables2 жыл бұрын

    Nice man 👍 keep up the good work

  • @insidecode

    @insidecode

    2 жыл бұрын

    Thanks!

  • @delyartabatabai9636
    @delyartabatabai96362 жыл бұрын

    very great explanation!

  • @insidecode

    @insidecode

    2 жыл бұрын

    Thanks!

  • @waisyousofi9139
    @waisyousofi9139 Жыл бұрын

    thanks

  • @omridrori3286
    @omridrori3286 Жыл бұрын

    hey amaizing video but i dont get it why in the time complexity calaculate the sum require o(n) it all sum why not o(1)? i mean at 8:20 at the below statement ?

  • @hb3643
    @hb36432 жыл бұрын

    Great content. Thank you

  • @insidecode

    @insidecode

    2 жыл бұрын

    You're welcome!

  • @aryan7069_
    @aryan7069_2 жыл бұрын

    wow , its always worth watching

  • @insidecode

    @insidecode

    2 жыл бұрын

    Thanks!

  • @virajgoyanka5150
    @virajgoyanka51502 жыл бұрын

    This is helpful .. Thumbs up!

  • @insidecode

    @insidecode

    2 жыл бұрын

    Thanks!

  • @cristhiansamatapumahualcca5678
    @cristhiansamatapumahualcca5678 Жыл бұрын

    nice job

  • @anonymoususer2271
    @anonymoususer2271 Жыл бұрын

    Aw this is amazing

  • @RageRabbitGames
    @RageRabbitGames2 жыл бұрын

    very helpful video, thanks heaps

  • @insidecode

    @insidecode

    2 жыл бұрын

    You're welcome!

  • @HollowNyte
    @HollowNyte2 жыл бұрын

    Amazing Video!

  • @insidecode

    @insidecode

    2 жыл бұрын

    Thanks!

  • @jeffbezos3942
    @jeffbezos39422 жыл бұрын

    This channel only need playlist clarification and it is perfect!

  • @insidecode

    @insidecode

    2 жыл бұрын

    I'll make playlists then

  • @jeffbezos3942

    @jeffbezos3942

    2 жыл бұрын

    @@insidecode Thank you!

  • @insidecode

    @insidecode

    2 жыл бұрын

    @@jeffbezos3942 You're welcome!

  • @anantshekhar698
    @anantshekhar698 Жыл бұрын

    Amazing

  • @ghanshyampanchal2527
    @ghanshyampanchal25272 жыл бұрын

    Really really useful

  • @insidecode

    @insidecode

    2 жыл бұрын

    thanks!

  • @kirankumar2348
    @kirankumar23482 жыл бұрын

    Hi bro, that's realy high quality content I love ❤❤❤❤❤❤❤your accent daaam. Keep doing, Love from India🇮🇳🇮🇳.

  • @insidecode

    @insidecode

    2 жыл бұрын

    Thanks a lot man

  • @florinmatei8846
    @florinmatei88462 жыл бұрын

    I was wondering from some time ago if Toom-4 is equivalent with Karatsuba , from a complexity point of view, but that looks a bit impossible. Lately , I was able only to come with this. Considerring Toom-4 and also Toom-4 added those two terms multiplications (the ones from middle ), which are more like K-idea, only with two more multiplication comparing too Toom-4. (so we got 4+3 vs 4+5 complexity dilema). those 5 multiplications terms computed are all following some simple rule for product of 2 coetients and other 2, no matter which one of the 5 mult term we chose. So we might be able to reach to the conclusion that we can find the method Florin-4, Florin-8, etc, no so sure about its complexity thow so this may be just some joke offered by me to anyone wish to verify its complexity. Thank You, I like these style of videos on KZread! ^_^ P.S. digging this out i was needed to reformulate the K-idea , both with Toom one, by switching from geometric progressions to something more oriented on adds n diffs instead coetient products that looks the same to diffs that look the same. But this is a bit too complex to me, a bit hard to get the job done, need meds 2 times a day in any case, I mean only to be able to put these here, for example. Thank You! :-)

  • @mehdididit

    @mehdididit

    2 жыл бұрын

    Mucho texto

  • @florinmatei8846

    @florinmatei8846

    2 жыл бұрын

    @@mehdididit I agree, I talk too much since I know myself >

  • @florinmatei8846

    @florinmatei8846

    2 жыл бұрын

    @@mehdididit Alrite, here some more nonsense, that might worth some translation 🙂 consider ca in mod normal, logica de tip chat bot sau click programming / logic explorer, pentru utilizatori incepatori din gimnaziu/liceu ar cam trebui sa mearga binisor, ma gandesc ca macar atata pedagogie cibernetica ar cam trebui sa se gaseasca pe lume, spre exemplu la programarea in basic, sa ofere pe post de "mutare" din partea computerului, cateva optiuni pentru urmatoarea nstructiune de inserat , doar cu un click, in viitorul mic cod sursa al rutnei respective, provocarea fiind ca la per total, aplicatia gen logic explorer sa ma arate a ceva. Multumesc frumos!

  • @rohankhandelwal2021
    @rohankhandelwal20212 жыл бұрын

    thank u a lot bro .... best video ;)

  • @insidecode

    @insidecode

    2 жыл бұрын

    You're welcome!

  • @vikrambharadwaj6349
    @vikrambharadwaj63492 жыл бұрын

    Beautiful!

  • @insidecode

    @insidecode

    2 жыл бұрын

    Thank you!

  • @florinmatei8846
    @florinmatei88462 жыл бұрын

    An algo optimisation idea that can be inspired by the hardware predication techniques, applied to soft data numbers to be multiplied, arraies to be multiplied, others may too, I think that may give us something to think about. Never tested nor verified by me, sorry! :-)

  • @gauravchaudhari618
    @gauravchaudhari6182 жыл бұрын

    awesome man immediately subscribe to your channel

  • @insidecode

    @insidecode

    2 жыл бұрын

    Thanks!

  • @jacktrainer4387
    @jacktrainer43872 жыл бұрын

    Fantastic as usual! May I ask what program you use to do your videos?

  • @insidecode

    @insidecode

    2 жыл бұрын

    Thanks! I make the slides with PowerPoint

  • @eriklonnrot9313

    @eriklonnrot9313

    2 жыл бұрын

    Hahahahaha i was expecting some rare program, thats amazon amazon slides bro. Keep it simple

  • @zareenfatima4742
    @zareenfatima4742 Жыл бұрын

    How do we do this if we have odd lengths of numbers?

  • @rajarshimandal3235
    @rajarshimandal32352 жыл бұрын

    Thank u sir

  • @insidecode

    @insidecode

    2 жыл бұрын

    You're welcome!

  • @rehabemadel-dein6950
    @rehabemadel-dein6950 Жыл бұрын

    How to solve it using array by storing two numbers in 1D array with help of 2D array

  • @pokortec9773
    @pokortec97732 жыл бұрын

    The best channel

  • @insidecode

    @insidecode

    2 жыл бұрын

    Hey thanks!

  • @yusufk5919
    @yusufk59196 ай бұрын

    goated video

  • @DavidFMayerPhD
    @DavidFMayerPhD2 жыл бұрын

    How does this work IN PRACTICE for standard 64 bit binary machines? Is there REALLY a savings? Or are much larger numbers (256 bit) needed for a practical improvement? Has this ever actually been implemented into processor microcode? If so, how did it work out?

  • @kwanarchive

    @kwanarchive

    2 жыл бұрын

    According to Wikipedia, at least 320-640 bits.

  • @vishalplayzz2580
    @vishalplayzz2580 Жыл бұрын

    nice man

  • @insidecode

    @insidecode

    Жыл бұрын

    Thanks for watching!

  • @erforderlich5274
    @erforderlich5274 Жыл бұрын

    His speech melody tells 'it's all very simple, seeee?' - My brain sounds drop to 40hz.

  • @YoussefAhmed-is3gf
    @YoussefAhmed-is3gf2 жыл бұрын

    you are super awesome

  • @insidecode

    @insidecode

    2 жыл бұрын

    Thanks!

  • @vinit.khandelwal
    @vinit.khandelwal2 жыл бұрын

    Need more such

  • @insidecode

    @insidecode

    2 жыл бұрын

    The next videos will all be about CS algorithms!

  • @lo9manechrf819
    @lo9manechrf8192 жыл бұрын

    سبحان الله والله عرفت accent تعك دزيرية 😂 Anyways, thanks for the information it was really helpful, keep going 💜💜

  • @insidecode

    @insidecode

    2 жыл бұрын

    You're welcome kho

  • @LFIILM
    @LFIILM11 ай бұрын

    thanks a loot

  • @tastes-like-straberries
    @tastes-like-straberries2 жыл бұрын

    I wrote a code similar to this but while running it for inputs of 8 digits and more, I'm getting recursion max depth reached error. Any idea how to solve this?

  • @insidecode

    @insidecode

    2 жыл бұрын

    Very your base cases, make sure the code is stopping at some point, try to print the parameters to see where it's stuck

  • @tastes-like-straberries

    @tastes-like-straberries

    2 жыл бұрын

    @@insidecode yes thanks! I figured out the issue

  • @insidecode

    @insidecode

    2 жыл бұрын

    You're welcome!

  • @skay1012
    @skay10123 ай бұрын

    cool ♣

  • @kishanmittal431
    @kishanmittal4312 жыл бұрын

    It should be (a+b)(c+d) and you solved for that only but you have written (a+c)(b+d) at 6:10

  • @insidecode

    @insidecode

    2 жыл бұрын

    Yup that's true thanks for mentioning it

  • @suyziljackson8202
    @suyziljackson82027 ай бұрын

    is there another way to get 3 multiplications?

  • @ahmedanwar5950
    @ahmedanwar59502 жыл бұрын

    I can't understand why we store n // 2 in the variable half , I know it gives a wrong answer if we don't do that but why ?

  • @insidecode

    @insidecode

    2 жыл бұрын

    Technically you don't need to do it, you can write n//2 everywhere but using a variable gives a more understandable code

  • @kostavsheoran1530
    @kostavsheoran15302 жыл бұрын

    Pardon me but i m still confused how does exactly it reduces the time complexity than the old way, i see a lot of arithmetic in this method too?

  • @KahHongTan

    @KahHongTan

    2 жыл бұрын

    The trick is the part at 3:26. Divide and conquer works only if breaking a problem down and dealing with the sum of those smaller chunks is easier. It might not be, you might just end up with many small problems which added up is still the same problem. This is the case if you split two numbers you want to multiply, now you have twice the number of multiplications of integers half the original size. Split again, now 4 times the number of multiplications with integers of a quarter size. Same problem. But here, whenever you split two integers there's 3 new multiplications instead of 4. So you get to split the integers in half without doubling the multiplications needed.

  • @ash4173
    @ash417311 ай бұрын

    u da goat no bap

  • @savashzaynal6502
    @savashzaynal65022 жыл бұрын

    I have a fat expensive algorithm book in my hand that could not even explain how x became a*10^(n/2) + b. Yet it only took you 10 seconds to explain it...

  • @konstantinrebrov675
    @konstantinrebrov6756 ай бұрын

    When I was in university I was assigned to implement this algorithm, and I struggled to understand it.

  • @AlpPamuk
    @AlpPamuk2 жыл бұрын

    gooddddd

  • @lukayt-content1613
    @lukayt-content1613 Жыл бұрын

    Nice video but i don t understand why do we need this. Cant we just multiply the numbers? Initially i thought that the method will be for that numbers that when are multiplied are giving a very large number that doesn t fit into long long or double.

  • @insidecode

    @insidecode

    Жыл бұрын

    It's not about overflow because because we could use strings instead anyway, it's about the method we use to actually multiply the numbers. For small numbers we can use the brute force method we all know, but for big numbers, it's better to use the Karatsuba algorithm because it's faster

  • @Fred2-123

    @Fred2-123

    Жыл бұрын

    It is for numbers that have hundreds of digits. Think about numbers like the number of nanoseconds since Jan 1, 1492. Or pi to the 100'th decimal place. On a 32 bit machine, what if you need to multiply two 14-digit numbers.

  • @ahadali3908
    @ahadali390816 күн бұрын

    PayPal link for paying back for this amazing explanation?

  • @arquier
    @arquier2 жыл бұрын

    That algorithm is used to multiply big Matrix right?

  • @insidecode

    @insidecode

    2 жыл бұрын

    just integers, maybe you're talking about Chain matrix multiplication?

  • @arquier

    @arquier

    2 жыл бұрын

    @@insidecode yes, looks like it can be applied on that case too

  • @sudalaitech4019
    @sudalaitech40193 ай бұрын

    Why calculating the result takes O(n)?

  • @sethdon1100

    @sethdon1100

    Ай бұрын

    Addition digit by digit is an O(n) for n digits

  • @ardiris2715
    @ardiris2715 Жыл бұрын

    This begs to be a homework problem in recursive LISP. Using binary numbers. LOL

  • @user-dk6qw3mz7l
    @user-dk6qw3mz7lАй бұрын

    Calculator was made for the math💀💀

  • @RUBAYATKHAN89
    @RUBAYATKHAN892 ай бұрын

    Did not understand why there's O(n) ? All the operations are O(1)

  • @sethdon1100

    @sethdon1100

    Ай бұрын

    Additions

  • @nursultanzhumabaev9354
    @nursultanzhumabaev93542 жыл бұрын

    I don't get it, why n is 2?

  • @insidecode

    @insidecode

    2 жыл бұрын

    Where did you see that n is 2?

  • @OKeefeist
    @OKeefeist Жыл бұрын

    If you talked a bit slower and clearer would be a 10/10

  • @eternalthoughts3778
    @eternalthoughts37782 жыл бұрын

    How u got 5,8

  • @insidecode

    @insidecode

    2 жыл бұрын

    In the code, you can see that we're recursively calling the function thrice, once to calculate a*c, once to calculate b*d, and once to calculate (a+b)*(c+d). And in the node where we calculate 14*35, splitting the two numbers into two parts give us a=1, b=4, c=3, and d=5. So 5,8 comes from the fact that we want to calculate (a+b)*(c+d), which is (1+4)*(3+5), which is 5*8

  • @eternalthoughts3778

    @eternalthoughts3778

    2 жыл бұрын

    @@insidecode thank you for your patience,I never expected u will reply comments

  • @insidecode

    @insidecode

    2 жыл бұрын

    @@eternalthoughts3778 haha why wouldn't I?

  • @zulqarnain_haider
    @zulqarnain_haider Жыл бұрын

    any uetian present

  • @LeonidSaykin
    @LeonidSaykin2 жыл бұрын

    that doesn't look quicker or simpler

  • @insidecode

    @insidecode

    2 жыл бұрын

    It's quicker for large numbers, Karatsuba algorithm has an O(n^1.58) time complexity while the brute force method has an O(n²) complexity

  • @rounaksarkar3084
    @rounaksarkar3084 Жыл бұрын

    Avik ims pimro whatever you teamch

Келесі