The Fastest Multiplication Algorithm

แชร์
ฝัง
  • เผยแพร่เมื่อ 29 เม.ย. 2024
  • Keep exploring at ► brilliant.org/TreforBazett. Get started for free for 30 days - and the first 200 people get 20% off an annual premium subscription!
    How can you multiply two enormous numbers? We all learn an approach in school which has n^2 total multiplications by single digit numbers. The first improvement was the Karatsuba Algorithm which uses a divide and conquer approach and a bit of clever algebra to reduce 4 multiplication to 3 (in 2x2 case) and more generally to about n^1.58 multiplications asymptotically. For a computer with a 32-bit hardware multiplier, one can iteratively apply the approach until the numbers are within the size of the multiplier. Toom-Cook is a generalization of the Karatsuba Algorithm and is useful in various cryptography applications, but a real change happened with Schonhage-Strassen which use discrete fast fourier transforms to get to O(n log n log log n) complexity, used for applications like the Great Internet Mersenne Prime search. The theoretical best result is Harvey and Hoeven who achieved O(n log n), albeit this becomes more efficient for only impractically large numbers.
    0:00 Review of normal multiplication
    1:42 The Karatsuba Algorithm for 2x2
    3:32 Example of Karatsuba
    4:34 Karatsuba for larger numbers
    5:45 Complexity of Karatsuba for size 2^k
    7:09 Computer architecture and hardware multipliers
    8:26 Newer algorithms (Schonhage-Strassen, and Harvey and Hoeven)
    12:46 Brilliant.org/TreforBazett
    Check out my MATH MERCH line in collaboration with Beautiful Equations
    ►beautifulequations.net/pages/...
    COURSE PLAYLISTS:
    ►DISCRETE MATH: • Discrete Math (Full Co...
    ►LINEAR ALGEBRA: • Linear Algebra (Full C...
    ►CALCULUS I: • Calculus I (Limits, De...
    ► CALCULUS II: • Calculus II (Integrati...
    ►MULTIVARIABLE CALCULUS (Calc III): • Calculus III: Multivar...
    ►VECTOR CALCULUS (Calc IV) • Calculus IV: Vector Ca...
    ►DIFFERENTIAL EQUATIONS: • Ordinary Differential ...
    ►LAPLACE TRANSFORM: • Laplace Transforms and...
    ►GAME THEORY: • Game Theory
    OTHER PLAYLISTS:
    ► Learning Math Series
    • 5 Tips To Make Math Pr...
    ►Cool Math Series:
    • Cool Math Series
    BECOME A MEMBER:
    ►Join: / @drtrefor
    MATH BOOKS I LOVE (affilliate link):
    ► www.amazon.com/shop/treforbazett
    SOCIALS:
    ►Twitter (math based): / treforbazett
    ►Instagram (photography based): / treforphotography

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

  • @Bunnokazooie
    @Bunnokazooie 11 หลายเดือนก่อน +392

    I remember my professor saying nlogn might be possible, and then a few years later Harvey-Hoeven proved it! A video on the discrete fourier transform would be awesome. I want to understand how decomposing wave functions has applications in things like multiplication, factoring, and discrete logarithms.

    • @DrTrefor
      @DrTrefor  11 หลายเดือนก่อน +49

      Cool! Ya at some level it should be nlog n just based on the complexity of fast fourier transforms, so all the extra baggage in the early algorithms was just that

    • @DavidvanDeijk
      @DavidvanDeijk 11 หลายเดือนก่อน +8

      @@DrTrefor isnt FFT also a lot of multiplications?

    • @user-pb8yw8cw3s
      @user-pb8yw8cw3s 11 หลายเดือนก่อน +2

      Radix algorithm

    • @struful
      @struful 11 หลายเดือนก่อน +4

      I believe 3blue1brown did a very solid video on FFT a month or so ago

    • @rogerphelps9939
      @rogerphelps9939 11 หลายเดือนก่อน +1

      The multiplication algorithm is a bit like a convolution which can be replaced with a single multiplication in the transform domain.

  • @swagatochatterjee7104
    @swagatochatterjee7104 11 หลายเดือนก่อน +136

    The most brilliant part of Karatsuba's algorithm was that he took it up as a challenge to prove Kolmogorov wrong (claim that the lower bound cant be any lesser than n^2) and came up with this brilliant manipulation. Like damn dude , like how confident do you have to be to challenge and disprove perhaps one of the greatest mathematician of the USSR, rather the world! He did all this while he was just a student.

    • @DrTrefor
      @DrTrefor  11 หลายเดือนก่อน +10

      Very cool!

    • @wyattstevens8574
      @wyattstevens8574 6 หลายเดือนก่อน

      It was a very clever manipulation- maybe he went through the steps I outlined in my comment.

    • @swagatochatterjee7104
      @swagatochatterjee7104 6 หลายเดือนก่อน

      @@wyattstevens8574 bro I can't find your comment

  • @williamchamberlain2263
    @williamchamberlain2263 11 หลายเดือนก่อน +125

    Note that in a program multiplying by a power of two can be just a bit-shift. But you have to recognise when it's a power of two

    • @mertaliyigit3288
      @mertaliyigit3288 11 หลายเดือนก่อน +19

      The similar algorithm for every number is called binary exponentiation. For example, if you had to multiply x with 24, you first multiply x with 16 with bit shifting, then you multiple x with 8 with again bit shifting, then add those 2 numbers

    • @JasonMitchellofcompsci
      @JasonMitchellofcompsci 11 หลายเดือนก่อน +8

      @@8ightfold Yeah. But that if statement is branching. If you care about speed then clearly you are handling a lot of data so instead you can use AVX/SIMD to multiply many elements, and an unrolled loop to do many of those. The micro-code of your processor is very well coded to stream many of these operations back to back.
      Or you can ruin it all with an if statement.

    • @davidhand9721
      @davidhand9721 11 หลายเดือนก่อน +3

      Every old software multiply I see is like this:
      fn multiply(a, b) {
      sum = 0
      while (b) {
      sum += a if b & 1
      left shift a
      right shift b
      }
      }
      That's n time where a and b have n bits. Not sure why you guys are doing anything else.

    • @gregorymorse8423
      @gregorymorse8423 11 หลายเดือนก่อน +1

      So multiplication is O(1) or O(n) depending on if wires or RAM is in play if your number is in base b and you multiply by b^k. However since you must convert to and from that number system, the complexity of base conversion will again not be better than n log n. So the fact you point out though practically useful in digital systems isn't really going to improve things.

    • @gregorymorse8423
      @gregorymorse8423 11 หลายเดือนก่อน +1

      ​@David Hand addition is O(n) and therefore you just described gradeschool multiplication or an O(n^2) algorithm. Not sure what universe you are from but in this one addition is not free. Not to mention bit by bit is terrible in computing performance. All real implementations do it a machine word at a time treating a hardware machine multiply as O(1) which is massively faster than what you wrote e.g. 1024 times faster on a 32 bit system. After n is around 30 to 50, Karatsuba will start to outperform. You might want to try it and do performance measurements before jumping to conclusions.

  • @bhavesh.adhikari
    @bhavesh.adhikari 11 หลายเดือนก่อน +99

    Recently, I wrote a program in C implementing the grade-school multiplication algorithm. It's working fine for multiplying two numbers up to around 130 digits long, but the result for numbers greater than 130 digits starts to differ slightly from that of Wolfram Alpha. I don't know what the issue is. Well i guess it's time to try the Karatsuba algorithm and see how it goes. and as usual great video professor!!

    • @DrTrefor
      @DrTrefor  11 หลายเดือนก่อน +30

      hmm interesting, I wonder if it is some memory issue?

    • @bhavesh.adhikari
      @bhavesh.adhikari 11 หลายเดือนก่อน +13

      @@DrTrefor yeah it might be the reason. as a beginner i am very much satisfied with it hahaha, i will work on it later

    • @GhostyOcean
      @GhostyOcean 11 หลายเดือนก่อน +3

      What types are you working with? I know different types have different memory sizes for them. For example BOOL, LONG, and INT types use different numbers of bits to store their information.

    • @allanjmcpherson
      @allanjmcpherson 11 หลายเดือนก่อน +13

      Yeah, it sounds like you might be getting an overflow error somewhere along the way for large numbers. It might be useful to examine the start and end of the binary representation of the correct answer and the answer your program spits out. If the end agrees and the beginning doesn't, that support the idea that it's an overflow error.

    • @bhavesh.adhikari
      @bhavesh.adhikari 11 หลายเดือนก่อน +5

      @@GhostyOcean i am using character array to take input and then integer array of same size, then converting string input into integer by ascii adjustment then carrying out multiplication just like we human do. and storing the result in dynamically allocated resulting array.

  • @andrewharrison8436
    @andrewharrison8436 11 หลายเดือนก่อน +6

    There is this lovely tension between theory and practicality in maths.
    The Greeks: there are an infinity of primes because a finite list can be multiplied together, add 1 and factorise to get a new prime.
    Cryptographers: take 2 large primes and multiply, bet you can't find the original primes.
    Karatsuba: multiplying those primes is hard, let's make it faster.
    Schonhage-Strassen, and Harvey and Hoeven: for numbers so big we can't in fact work with them let's make it faster.
    Lovely - now we need a theoretical proof that n log(n) is indeed the limit.

  • @moreon340
    @moreon340 11 หลายเดือนก่อน +3

    The best part about O(n log n) multiplication isn't doing it (as is clear from the fact that we don't actually do it), it's analysing other things that use multiplication and just having it be O(n log n) and not muddy the entire analysis with more complicated terms.

  • @amoledzeppelin
    @amoledzeppelin 11 หลายเดือนก่อน +2

    Thanks, sticking to Karatsuba.

  • @johnchessant3012
    @johnchessant3012 11 หลายเดือนก่อน +9

    also the same Strassen as in the Solovay-Strassen primality test!

    • @DrTrefor
      @DrTrefor  11 หลายเดือนก่อน +5

      That's right, the dude was busy!

  • @BoBoN4Uto
    @BoBoN4Uto 10 หลายเดือนก่อน +2

    4:10 "you can immediatly tell what 7 times 8 is" me: 64... wait no...

  • @NowInAus
    @NowInAus 11 หลายเดือนก่อน +5

    Fascinating. Stumbled onto this and I’d never really thought about the rate determining steps in larger multiplication tasks. Thanks

  • @slembcke
    @slembcke 11 หลายเดือนก่อน +6

    The first time I heard about the FFT algorithm for multiplication, it seemed... impossible. What did multiplication have to do with the frequency domain after all? I didn't really look into it further since I didn't need to multiply giant numbers anyway. I think it was a 3bluebrown video talking about convolutions in probability, and used long multiplication as an example to introduce it. That was a giant AHA! moment. Of course multiplication is just convolution followed by the carry additions! I immediately opened up a python prompt and had to try it. Mind blown! What a beautifully weird algorithm. Using the FFT to convolve digits of a number in the frequency domain? Brilliant! Maybe a bit useless for everyday computing, but still!

  • @prashantsingh6370
    @prashantsingh6370 11 หลายเดือนก่อน +8

    I love learning how things actually works in mathematics for larger numbers.
    The love only increased further during my electrical engineering days where we had a subject, aptly, called Advanced Engineering Mathematics (AEM) 1 & 2.
    For sure it was difficult than anything I had done till then but at the same time it felt so cool to understand it and solve problems.
    In the end, AEM was amongst my top 4 highest scoring subjects. 😅

  • @interest21stcentury74
    @interest21stcentury74 11 หลายเดือนก่อน +2

    Hey dr, I hope you are doing well! Missed your classes (during the COVID era) and videos very much! Your videos helped me get an A on Calculus. Im also glad you got over 300k subscribers.
    I am also recommending your videos to my first year friends, because they are extremely helpful.

  • @rohanganapathy8
    @rohanganapathy8 11 หลายเดือนก่อน +13

    Would have loved to learn about the newer theorems but still was very interesting.

    • @DrTrefor
      @DrTrefor  11 หลายเดือนก่อน +10

      I am hoping to do a video at some point really diving into the discrete fast fourier transforms and how they are used for the more advanced algorithms:)

  • @rubixtheslime
    @rubixtheslime 11 หลายเดือนก่อน +2

    the fun thing about hardware multiplication is they often continue to break the problem down even further. from what i've heard, modern hardware generally breaks it down to 8x8 bit multiplications and then uses the fastest 8x8 bit multiplication known to mankind: a 65k entry table. and you even have a separate table for every 8x8 bit multiplication that has to be done. and while you could do 64x64 bit multiplication (which is implemented in x86_64, i have no idea how to get the full answer in C though) using 27 tables instead of 64 using Karatsuba, they're all done at the same time so in terms of speed that's just more layers of addition. so in case you were wondering how we manage to keep doubling those transistor counts...
    what i think is really interesting though is that in grade school you're effectively left to assume that division isn't much slower than multiplication. but in reality, multiplication has all these different ways you can improve, while with division it's things like "wow we cut it down by a factor of 4, still 10x slower than multiplication though because none of it is parallel" for hardware and "so basically you multiply a bunch of times because multiplication is just that much faster" for low complexity algorithms (just checked before posting this, turns out dividers can achieve the same complexity as multipliers, though once again, this is by turning it into multiple multiplications). also doesn't help division's case that integer dividers have to provide two almost unrelated answers at once (quotient and remainder), not just one big one.

    • @johncochran8497
      @johncochran8497 11 หลายเดือนก่อน

      I'd suggest you look up Dadda and Wallace trees. Additionally, you might find Booth's multiplication algorithm useful. One nice thing about Booth's method is that it handles signed numbers directly and with a minor modification, handles unsigned as well (basically extend the unsigned numbers an additional significant bit with a value of 0). At the hardware level, this allows for both signed and unsigned multiplies to be performed with essentially the same hardware and microcode.
      But as for this video, it's only useful for LARGE numbers. For smaller numbers such as used natively by 32, 64, and 128 bit computers, it's generally faster to use the straight forward O(n^2) algorithms or lookup tables.
      One minor note. If you look at Booth's method, a lot of naïve people will claim that it reduces the number of add operations for performing multiplication. This is wrong. On average the total number of adds will be the same for Booth and conventional. The big advantage to Booth's method is that for multi-bit versions, the values added (or subtracted) are simpler than the values used for conventional multi-bit multiplication. For instance, Radix-4 Booth uses the values 1x, 2x, -2x, -1x. All of which is a simple addition or subtraction of either the value directly, or a single bit shifted value. Whereas radix-4 conventional uses the values 1x, 2x, 3x. The values 1x and 2x are simple. But 3x can't be calculated via a simple shift. It requires both a shift and an addition. So it's considered "complex". And for Radix-8 Booth, the values required are 1x, 2x, 3x, 4x, -4x, -3x, -2x, -1x. So the only complex values needed is 3x, whereas for Radix-8 conventional, the complex values required are 3x, 5x, and 7x (6x is a simple shift of the 3x value).

  • @SameAsAnyOtherStranger
    @SameAsAnyOtherStranger 11 หลายเดือนก่อน +11

    Binary multiplication is a different beast all together. It's adding the multiplicand to a running sum of adding exponentiated copies of itself according to the reversed sequence of ones and zeros in the multiplier.
    But I'll definitely be trying this method on binary numbers.

    • @ashutoshmahapatra537
      @ashutoshmahapatra537 11 หลายเดือนก่อน +1

      I had tried it before in binary using bitshifts + recursion but still native multiplication will be faster which makes sense, since it doesn't involve any function calls, ig if it's done in a low level language difference can be seen..

    • @xGOKOPx
      @xGOKOPx 11 หลายเดือนก่อน

      ​ @ashutoshmahapatra537 If your compiler is any good then function calls to short functions should be removed during the optimisation stage. Native multiplication will however always be faster because it's done in hardware, not software. Algorithms mentioned in this video come into play when you have to multiply those enormous numbers which you can't represent with native data types.

  • @Mr.Nichan
    @Mr.Nichan 11 หลายเดือนก่อน +1

    "We all learn how to do this in high school."
    We learned it in elementary school. By high school, most people were getting rusty at it because we could use calculators, though I always lost my calculator and so got better. (I still was able to write this before I "instantly" knew what 7*8 was, though. It's 56, I remember now.) On yhe ither hand, I like the way you explicitly write all 4 1-digit multiplications instead of just writing out 2 multiplications, one for each of the bottom digits,like I learned to do.

  • @egycg3569
    @egycg3569 11 หลายเดือนก่อน +1

    What you're doing is invaluable Dr?
    Thanks once more

  • @RobertBraf
    @RobertBraf 11 หลายเดือนก่อน +5

    Thanks for this great vid, Professor!
    Do you also plan to branch to modular Multiplikation algorithms in one of your next videos?
    Regards, Rob.

    • @DrTrefor
      @DrTrefor  11 หลายเดือนก่อน +3

      good idea!

  • @jamesknapp64
    @jamesknapp64 11 หลายเดือนก่อน +4

    Would the Harvey and Hoeven be useful for checking Fermat number primality? I know F_33 is the first candidate and 2^(1729^12) would be on the order of 2^(2^129) thus F_129 (which doesn't appear to have factors currently) might be a candidate to try and use it with Pepin's Test.

  • @gblake321
    @gblake321 11 หลายเดือนก่อน +4

    This video was really helpful. I think I know enough to code it up on my own.

    • @DrTrefor
      @DrTrefor  11 หลายเดือนก่อน +2

      Nice!

  • @arctan-k
    @arctan-k 11 หลายเดือนก่อน +7

    Make a video about fast multiplication using fast fourier transform

    • @DrTrefor
      @DrTrefor  11 หลายเดือนก่อน +6

      I plan to!

  • @denverbraughler3948
    @denverbraughler3948 11 หลายเดือนก่อน

    If you do many multiplications by hand, you should soon notice that once you’ve multiplied for a particular digit, you never have to repeat it.
    You just copy it shifted over, the same as when multiplying by 1.
    It’s merely a copy operation.
    Never are n² digit-wise multiplications required.
    The maximum is 8n.

  • @thisukainol
    @thisukainol 9 หลายเดือนก่อน +1

    keep up the good work

  • @cs127
    @cs127 11 หลายเดือนก่อน +1

    Interesting algorithm!
    I love your videos!

  • @davidhand9721
    @davidhand9721 11 หลายเดือนก่อน +1

    Usually when I see a software multiply in old code, computing a*b goes like this:
    i = 0
    sum = 0
    while i++ < n {
    if b & 1 add a to sum
    shift a left
    shift b right
    }
    The adds and shifts and the and are all O(k) apiece, so the full multiply is O(n). Am I missing something here? Why are we mucking about with nlog n?
    I suppose it only works out this way in base 2, but only base 2 matters if we're computing complexity. In base 2, a pair of digits can only multiply to 1 or 0, whereas a pair of base 10 digits has to be an actual multiply instead of logical and, and the multiply in that case is not O(k).

    • @davidhand9721
      @davidhand9721 11 หลายเดือนก่อน

      You can replace the if with bitmasking a if you don't like the jump, too

    • @johncochran8497
      @johncochran8497 11 หลายเดือนก่อน

      Yes, you are missing something. The key thing to remember is that they are talking about LARGE numbers. So addition is an O(n) operation where n is the number of bits, not O(1). And for your multiply operation, you're doing n additions at a cost of O(n) each, giving an overall cost of O(n^2). Now, for modern processors, operations on the basic word size are O(1). But once again, in the grand scheme of things, those values that are basic to the CPU are small, whereas the values being talked about in the video are large values composed of thousands to millions of bits.

  • @General12th
    @General12th 11 หลายเดือนก่อน +4

    Hi Dr. Bazett!
    I bet these new algorithms will find a use eventually -- if not in this millennium, then certainly in subsequent ones!

    • @phitc4242
      @phitc4242 11 หลายเดือนก่อน

      it already has, i'm pretty sure (in python for example)

  • @harmsc12
    @harmsc12 11 หลายเดือนก่อน +1

    I was taught a slightly different method for multiplying multi-digit numbers, but the point of the video still stands.
    Instead of just looking at individual digits and having n^2 numbers that need adding together at the end, I was taught to run the entire top number against each digit of the bottom number, with proper carrying, which gave me just n numbers to add together, with n being the number of digits in the bottom number.

    • @DrTrefor
      @DrTrefor  11 หลายเดือนก่อน +1

      Ya fair enough, I sort of think of this as equivalent (you are still doing n^2 single digit multiplications, just organizing your work on the page a bit differently of when you do the additions)

    • @harmsc12
      @harmsc12 11 หลายเดือนก่อน

      @@DrTrefor That's also fair enough.

  • @wyattstevens8574
    @wyattstevens8574 6 หลายเดือนก่อน

    Here are the missing steps that Karatsuba (probably) went through to improve this: (i.e. here's how ad+bc equals the new 10s term)
    (a*10^n+b)(c*10^n+d)=
    ac*10^2n+(ad+bc)*10^n+bd
    Focusing on the middle term alone, ad+bc= ad+bc+ac+bd-ac-bd= (a+b)(c+d)-ac-bd
    ac*10^2n+((a+b)(c+d)-ac-bd)*10^n+bd
    And the product at 5:00 should be (if I'm right- I went Karatsuba all the way down) 1,117,262,504,544,938. Well- that's what I got.

  • @jmich7
    @jmich7 11 หลายเดือนก่อน

    What a super teacher!

  • @vineetkaddu1214
    @vineetkaddu1214 11 หลายเดือนก่อน +2

    Hi, you said at 3:00 that we need to do 3 single digit multiplications. But couldn't a+b and c+d also be 2 digit numbers? Thank you for this video , an awesome Explanation of this algorithm!
    Edit: Ok I get it. a+b or c+d would be guaranteed much smaller than a*10 + b for very large numbers, Thanks again!

  • @jek__
    @jek__ 10 หลายเดือนก่อน

    When you said enormous numbers and recursively apply the algorithm the first thing I thought was stack overflow lol

  • @tolkienfan1972
    @tolkienfan1972 10 หลายเดือนก่อน

    I've implemented fft multiply. I have a suspicion that a careful implementation with hand tuning can make it beat out the n^2 algo for much smaller n. I had to work on something else tho. Maybe I'll get back to it. The thing is, a naive implementation includes a lot of redundant operations, affecting the hidden constant.

  • @MarekKnapek
    @MarekKnapek 11 หลายเดือนก่อน

    In case of 32bit multiplication hardware... you can use such HW to multiply any two 16bit numbers and get 32bit result without loosing any information.

  • @Omeomeom
    @Omeomeom 11 หลายเดือนก่อน

    i think everyone should figure out the most efficient algorithms for multiplication and addition and why they are what they are. I feel like that would be a lot of value created.

  • @Snowflake_tv
    @Snowflake_tv 11 หลายเดือนก่อน +1

    I've just read a description of "About"tab, and I think I'll like you😄

  • @europeanmappin
    @europeanmappin 11 หลายเดือนก่อน

    0:48 not sure if its just us, but we instead do 6x2, 6x3 and then merge them while calculating, then we do 5x2, 5x3 and merge those while calculating, then we do x+x=x

  • @abdurrahmanlabib916
    @abdurrahmanlabib916 11 หลายเดือนก่อน +5

    Hello professor. Maybe you can make another playlist on another math course in the future which would help us a lot! Thank you.

    • @DrTrefor
      @DrTrefor  11 หลายเดือนก่อน +2

      Lot's more coming...what would you like to see?

    • @abdurrahmanlabib916
      @abdurrahmanlabib916 11 หลายเดือนก่อน +1

      @@DrTrefor maybe calculus of complex variables or real analysis

  • @WhitEagle7
    @WhitEagle7 11 หลายเดือนก่อน +1

    0:08 bro you learned the multiplication in high school? bro...

  • @sp0_od597
    @sp0_od597 11 หลายเดือนก่อน +1

    In regards to the 32 bit by 32 bit multiplication, you probably would not want to break them up into 32/31 bit segments. The reason being, is you will get a 32 bit integer back. Assuming you are multiplying 2 random 32 bit numbers, the odds of getting a number where you need more than 32 bits to represent is very high. The computer would then just roll over and just give you the last 32 digits and get rid of the rest.
    Imagine the 2 integers you are multiplying are 2^31 * 2^31 = 2^62. This is clearly not representable using 32 bits. You would want to break them up into smaller segments and the best way to segment the number without overflowing would be 16 bit segments since worst case scenario is
    (2^16 - 1) * (2^16 - 1) < 2^16 * 2^26 = 2^32
    which is within 32 bits so the 16 bit segments will never overflow.

    • @oscarsmith3942
      @oscarsmith3942 11 หลายเดือนก่อน

      many computer instruction sets have a high_mul instruction that will give you the upper bits of the multiplication so the way presented is often going to be faster.

  • @youneschafi5273
    @youneschafi5273 11 หลายเดือนก่อน +1

    2:59 a+b and c+d are not necessarly single digits.

  • @johnnyvsx
    @johnnyvsx 11 หลายเดือนก่อน

    Reminds me of the FFT algorithm for computing the DFT.

  • @JohnDlugosz
    @JohnDlugosz 11 หลายเดือนก่อน

    3:00 In binary, you don't have any "single digit" multiplies at all, since they are all 1 or 0. Instead, you have the decision to add or skip the add. The number of adds (and decisions on whether to perform or skip) is equal to the number of bits in the smaller factor.
    So if you consider n to be the number itself, multiplication has k log n steps.
    If you consider n to be the size (length) of the input; that is, the number of bits in the factors to be multiplied. then multiplying has kn steps.
    Really, if you wanted to make multiplication of large numbers fast on a computer, it would do to implement a special type of memory that has an adder and a shifter built-in, that joins the individual cells together for as large of a number as you want.
    That is, given two numbers stored in this memory A and B, it could perform B+=A in constant time and shift A right one bit in constant time. It's important to note that the number of logic gates is constant for each cell (word or byte) so scales linearly with the number of cells.

  • @henriquelizsandeflores4662
    @henriquelizsandeflores4662 11 หลายเดือนก่อน

    It's fascinating how we used logarithms to make counts faster and now we use them to measure how method is doing counts faster

  • @cem_kaya
    @cem_kaya 11 หลายเดือนก่อน +1

    isn't toom-cook O(N+e) epsilon as small as you want ?

  • @theedspage
    @theedspage 8 หลายเดือนก่อน

    Do you have a court in Brilliant, Dr. Bazett?

  • @SoftYoda
    @SoftYoda 11 หลายเดือนก่อน

    Is there any scientific reading analyzing the type of number we use in scientific or non-scientific computation ? (what size) from 0BC to 2023 ?

  • @misterkite
    @misterkite 11 หลายเดือนก่อน

    Feels like this is a hint for a handful of Project Euler solutions.

  • @KangJangkrik
    @KangJangkrik 11 หลายเดือนก่อน

    I like the topology picture at his shirt

  • @Peppermint3387
    @Peppermint3387 11 หลายเดือนก่อน

    How does first algo works with odd amount of number given or odd and even number given

  • @DrZygote214
    @DrZygote214 10 หลายเดือนก่อน +1

    @9:00 "For numbers after about 10 to the 96th..." Is that a typo? 10^96? That is a grokking huge number, far far huger than 2^64. If this is true then this algorithm is not worth it even for x64 architecture.

  • @readjordan2257
    @readjordan2257 10 หลายเดือนก่อน

    5:30 This reminds me of the DeepMind matrix multiplication thing, or the one-up to the 2*2 decomposition of larger matricies. That saves up to 33% less calculation. I forget that guys name.

    • @readjordan2257
      @readjordan2257 10 หลายเดือนก่อน

      9:40 yep. That all just came together.

  • @theowleyes07
    @theowleyes07 10 หลายเดือนก่อน

    Have you tried Vedic Way of Multiplication
    So the Example you gave
    32*56
    The way a Vedic Mathematician Have done in
    (3*5) at tens Place
    (3*6+5*2) then this
    (2*6)at last.
    So the way you get 28 can be done by 3*6+5*2

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

      He says that's basically the grade-school n² algorithm.

  • @AxiomTutor
    @AxiomTutor 11 หลายเดือนก่อน +2

    Yeah so much nicer to write the carry inline, never thought of that.

  • @stephenkolostyak4087
    @stephenkolostyak4087 11 หลายเดือนก่อน +2

    multiplication is first being taugh tin high school, now?
    ...

  • @AttackOnTyler
    @AttackOnTyler 11 หลายเดือนก่อน

    I can't believe I've never seen 1:09 multiplication where the "cross" of the bottom one's digit to the top ten's digit is completely stored for the additive solution section. Only the tens addition of a phantom ten's on the top ten's digit. This visual makes more sense of large number multiplication.

  • @aldob5681
    @aldob5681 11 หลายเดือนก่อน +2

    high school? or primary school?

  • @batoulnoureddine
    @batoulnoureddine 11 หลายเดือนก่อน

    Can make a video explaining where did the log actually came from? Imran how did we start using log and ln in the first place??

    • @tissuepaper9962
      @tissuepaper9962 11 หลายเดือนก่อน

      It's just a natural, necessary extension of exponentiation. Exponentiation without logarithms would be like addition without subtraction.
      The story of Euler's number _e_ and the natural log is slightly more interesting. _e_ was chosen so that _e^x_ would be its own derivative.

  • @johnvriezen4696
    @johnvriezen4696 11 หลายเดือนก่อน +2

    Hmm.. in your first example, two digits X two digits, you had to do four multiplications using the high school method. But if you choose to represent your numbers in a base greater than the larger of the two numbers, then it will always be a one digit number X one digit number, and you never have to do more than one multiplication. Multiplications can be done in constant time. Q.E.D. 🙂

    • @DrTrefor
      @DrTrefor  11 หลายเดือนก่อน +4

      Haha true! Ya if we just built hardware multiplies of arbitrary size it would multiply in one step!

  • @SuperDeadparrot
    @SuperDeadparrot 11 หลายเดือนก่อน +1

    I’m wondering if you can use a Fourier Transform on the entries.

    • @DrTrefor
      @DrTrefor  11 หลายเดือนก่อน +2

      Indeed, that's the basis for the more modern methods

  • @MissNorington
    @MissNorington 11 หลายเดือนก่อน

    3:15 mentions computers, but the title doesn't specify this. Fastest multiplication algorithm for computers would be a better description.

  • @satya456srav6
    @satya456srav6 11 หลายเดือนก่อน +4

    super professor!📚😃🏃‍♀️

    • @DrTrefor
      @DrTrefor  11 หลายเดือนก่อน +2

      Thank you!!

    • @SuperDeadparrot
      @SuperDeadparrot 11 หลายเดือนก่อน

      I also studied math at University of Toronto. Back when Prof Eric Moore was just Senior Tutor Moore.

  • @puppergump4117
    @puppergump4117 11 หลายเดือนก่อน +1

    ”We all learn this in high school, for instance”
    Well we usually learn that in grade school. Or I'd hope so.

  • @jamescraft5300
    @jamescraft5300 11 หลายเดือนก่อน +1

    0:06 highschool?

  • @ilikeapplejuice8658
    @ilikeapplejuice8658 11 หลายเดือนก่อน +2

    In highschool? Im sorry i learned multiplication in grade 2

  • @Snowflake_tv
    @Snowflake_tv 11 หลายเดือนก่อน

    04:06 I've memorized single-digit-multiplication, but recently I'm trying not to use the memorization. I'm trying to imagine a rectangle to multiply.

  • @DaneBrooke
    @DaneBrooke 11 หลายเดือนก่อน

    You have to also count the shift operations.

    • @DrTrefor
      @DrTrefor  11 หลายเดือนก่อน

      Shifts (i.e. multiplication by 2 in binary) are computationally cheap from what I understand

    • @socraticmathtutor1869
      @socraticmathtutor1869 7 หลายเดือนก่อน

      I think the shifts won't actually end up getting implemented; rather, they just kind of fall into place by getting the indexes right. To illustrate how this kind of thing works with a toy model, notice that the fastest way to compute x*(2^64) + y isn't to start by computing x*(2^64), it's actually two do the whole thing in one traversal by repeatedly computing x[i+1] + y[i]. I'm not an expert, but I have a feeling that Karatsuba's algo would typically be implemented in a similar way.

  • @dumnor
    @dumnor 11 หลายเดือนก่อน +2

    I believe every problem has n log n solution.

    • @DrTrefor
      @DrTrefor  11 หลายเดือนก่อน +2

      it's not a bad null hypothesis!

  • @Fidelity_Investments
    @Fidelity_Investments 11 หลายเดือนก่อน +2

    sorry, highschool? I learned this in 3rd grade

  • @anntakamaki1960
    @anntakamaki1960 11 หลายเดือนก่อน

    “Seven times eight and you immediately know the answer” 🤔 😬

  • @MathOrient
    @MathOrient 11 หลายเดือนก่อน +2

    Interesting :)😃

  • @cheto96
    @cheto96 11 หลายเดือนก่อน +3

    Did you just say you learn multiplying in HS 0:08

    • @DrTrefor
      @DrTrefor  11 หลายเดือนก่อน +2

      I learned it way back in the before times that is all a murky blurr:D

  • @wafikiri_
    @wafikiri_ 11 หลายเดือนก่อน +3

    If you multiply two binary numbers of length n, you have to perform n shifts left on one number (padding 0 as least significant bit), and at most n additions to it of the other number, one per each one found in the latter, zeros don't count, looking from the least significant bit. Isn't this the fastest multiplication? Order n, for sums, no multiplications.

    • @gdclemo
      @gdclemo 11 หลายเดือนก่อน

      You are talking about n additions of arbitrary length numbers, when you break that down into additions of fixed size integers you are back to O(n^2).

    • @wafikiri_
      @wafikiri_ 11 หลายเดือนก่อน

      @@gdclemo Yes, whether you use breaking integers down (for conventional computers) or hybrid conventional with Turing processors, a sum requires to go along successive pairs of ciphers n or n+1 times. Thus, O(n) times. And O(n) number sums, therefore, require O(n²) elemental operations
      But my point was that, with binary numbers, you don't need multiplications! Whatever the multiplication method, whatever the base, you still need O(n) sums of numbers, therefore O(n²) elemental operations just for sums, plus the elemental or combined multiplications in bases greater than two.

    • @gdclemo
      @gdclemo 11 หลายเดือนก่อน

      @@wafikiri_ Well you've just broken the multiplications down to their elemental components to the extent that you're just multiplying by zero or one, which are of course trivial. But those operations still add up to O(n^2) in total which is much slower than the optimal algorithm. Big-O notation doesn't care what the operations are or how long they take individually, only how they grow.

  • @anamoyeee
    @anamoyeee 11 หลายเดือนก่อน

    Nah, better do 1729 dimensional fourier transform

  • @SvenWM
    @SvenWM 11 หลายเดือนก่อน +1

    given enogh storage space you could just look at a table with the solutions for any multiplications. guess that is a bit of cheating but O=1 is technically possible

    • @DrTrefor
      @DrTrefor  11 หลายเดือนก่อน +1

      At some point, searching within the table would be the hard part!

  • @livedandletdie
    @livedandletdie 11 หลายเดือนก่อน

    56*32 is easy to do... it's just (40+16)*(40-8) or 40²+(16-8)*40-16*8 3 multiplications and 3 additions.
    I mean if we did numbers like 96*93 it's stupidly easy, it's 8900+28 I did 1 multiplication a simple one, -7*-4... That's all I needed. And how I came up with those numbers, oh boy.
    96*93=(100-4)(100-7) 100²+100*((-4)+(-7)) + (-4)(-7) And you still say that's 3 multiplications... and I say it's not, 96-100=-4 , 93-100=-7 96-7=89 -4×-7=28 89 concatenate 28 is 8928...
    1 multiplication the rest is addition.

  • @andjohnmurugan100
    @andjohnmurugan100 11 หลายเดือนก่อน +1

    Why does he randomly start to talk in italian?

  • @okaro6595
    @okaro6595 11 หลายเดือนก่อน +1

    Who does it the way you first presented? You of course first multiply 32 by 6 and then 32 by 5 one shifted. The you add.

    • @DrTrefor
      @DrTrefor  11 หลายเดือนก่อน +2

      Well this is the same, you are just writing less down, when you multiply 32x6 you are presumably doing 2x6 and 30*6 in your head and adding them. And I do too, I normally only write down 2 lines not 4 but I'm writing all 4 to make explicit that there are 4 single digit multiplications involved.

  • @wyboo2019
    @wyboo2019 11 หลายเดือนก่อน +1

    if i'm not mistaken, and its kind of cheating, but i'm pretty sure early mathematicians multiplied numbers with logarithm lookup tables. for example, say you wanted to do:
    31415*92653
    what you can do is:
    ln(31415*92653)=ln(31415)+ln(92653)
    look up these logarithms in your lookup table to get:
    10.35504076483705 + 11.43661661111373
    which is much easier to do:
    = 21.79165737595078
    then use your logarithm lookup table in reverse:
    = 2,910,693,995
    really nowadays you'll always have your phone with you for a calculator, and if you don't have your phone you also probably don't carry around a logarithm lookup table (although that would be so cool to own actually), it's still super useful to know in some oddly specific circumstances
    i think i might buy a pocket logarithm lookup table just in case

    • @andrewharrison8436
      @andrewharrison8436 11 หลายเดือนก่อน +2

      The problem is that the look up of the logarithm has built in accuracy limits - my paper copy is 5 digits so completely superceeded by computer calculation.

  • @colecook834
    @colecook834 11 หลายเดือนก่อน

    This looks like recursive functions from coding.

  • @moadot720
    @moadot720 11 หลายเดือนก่อน

    4:32 "A little bit"...? (No offense.)

  • @joyjin538
    @joyjin538 11 หลายเดือนก่อน

    With hand? Use log and log tables. That's what they are invented for originally.

  • @jakesnake5534
    @jakesnake5534 11 หลายเดือนก่อน

    Isn't binaty multiplication almost as fast as addition though?

    • @DrTrefor
      @DrTrefor  11 หลายเดือนก่อน

      multiplication by 2 is very fast in binary specifically, but not any two numbers.

    • @jakesnake5534
      @jakesnake5534 11 หลายเดือนก่อน

      @@DrTrefor can't you multiply by any number you want by doing bitshifts and addition really easily?
      I.E. to multiply by 5, shift bits2 left, and add the original value once,
      Or to multiply by 6 shift bits 2 left and add the original value twice?

  • @AlanAlmeidadeAndrade
    @AlanAlmeidadeAndrade 11 หลายเดือนก่อน

    Looks like o dont know instantly whats 7 times 8 is

  • @danielguy3581
    @danielguy3581 11 หลายเดือนก่อน

    What I once thought was some yokel's ignorant misunderstanding of language, I now realize is the common way Americans pronounce 'Fourier'. I get it if you feel this is petty and that math should stand above such matters of culture and language - by all means, call it "wave decomposition" or somesuch. But if you're going to name something after a person - might as well get their name right.

    • @tissuepaper9962
      @tissuepaper9962 11 หลายเดือนก่อน

      Go be insufferable somewhere else, Daniel. Maybe you should learn Ancient Aramaic so you can say that Biblical name of yours properly. After all, if you're going to name something after a person, you might as well get their name right.

  • @vivekm.s8819
    @vivekm.s8819 11 หลายเดือนก่อน +3

    First view proffesor.❤

    • @DrTrefor
      @DrTrefor  11 หลายเดือนก่อน +2

      Nice one!

  • @yjlom
    @yjlom 11 หลายเดือนก่อน

    nah, I prefer my doubly iterated succession

  • @CM-dx6xu
    @CM-dx6xu 11 หลายเดือนก่อน +1

    Engineers would just round those to at most 3 significant figures then multiply it 😂 no need to fuss

    • @DrTrefor
      @DrTrefor  11 หลายเดือนก่อน +1

      😂

  • @MrMaelstrom07
    @MrMaelstrom07 11 หลายเดือนก่อน

    Anyone else hungry for carrot soup now?

  • @omri.d
    @omri.d 11 หลายเดือนก่อน +1

    I don't think you can use big O to the complexity, the Karatsuba always does that number of multiplication, not in the worst case

    • @DrTrefor
      @DrTrefor  11 หลายเดือนก่อน

      Well it always does that if it is exactly 2^k, otherwise there is extra inefficiencies by going up to the nearest power of 2

    • @omri.d
      @omri.d 11 หลายเดือนก่อน

      So it's even MORE wrong, the big O in the video is the best time, not the wrost

    • @MythicWiz
      @MythicWiz 11 หลายเดือนก่อน +1

      if it always do 3^n,then it still do 3^n in the worst case isn't it?the same thing happens to merge sort with O(n log n)
      in every scenario

    • @MythicWiz
      @MythicWiz 11 หลายเดือนก่อน

      @@omri.d also even with 2^k+1 digits it does 3^(k+1) computes which is 3*3^k that still falls in the same complexity so it's not wrong

  • @DanDart
    @DanDart 11 หลายเดือนก่อน

    Have the times table literally in a table :p

    • @tissuepaper9962
      @tissuepaper9962 11 หลายเดือนก่อน +1

      Many hardware implementations of multiplication break big numbers down into 8-bit chunks and then use a 65565-entry lookup table.

  • @kkanden
    @kkanden 11 หลายเดือนก่อน +2

    you learn multiplication in high school what????

  • @Warlord_Megatron
    @Warlord_Megatron 11 หลายเดือนก่อน

    With due respect i just wanna say this is utterly useless

  • @DirtyDickMurdok
    @DirtyDickMurdok 10 หลายเดือนก่อน

    For 1 day I would like to know what it was like to be that smart