What is Fast Exponentiation?

Поделиться
HTML-код
  • Опубликовано: 31 дек 2024

Комментарии • 156

  • @hardtruth6566
    @hardtruth6566 7 лет назад +49

    Please, keep continuing creating these videos. You will eventually get more subs. And thank you so much for creating these wonderful tutorials keeping competitive programming in mind. We will be waiting for more :-)

    • @gkcs
      @gkcs  7 лет назад +2

      Thanks!

    • @pi_rand4266
      @pi_rand4266 3 года назад

      Now he has pi lakh subscribers

  • @kunalyadav4776
    @kunalyadav4776 4 года назад +8

    The energy you put in every video is inspiring .

  • @Chsatyanarayana
    @Chsatyanarayana 7 лет назад +13

    Such creativity in programming with new chef in middle of this competition is worthwhile notation.Thanku Gaurav.

    • @gkcs
      @gkcs  7 лет назад +1

      Thanks :-)

  • @shibashisdeb4136
    @shibashisdeb4136 2 года назад

    This the best explanation of fast exponentiation by far! Thank you.

  • @jvsnyc
    @jvsnyc 5 лет назад +1

    Some people have correctly pointed out there is more that can be done on this topic, but I think it is a great first video or introduction. I find a nice balance between geniuses trying to show us things where there are tons of things that are just obvious to them but not to the viewers, and watching regular dummies plod thru something they barely understand. I did need to watch this. I was working on a solution without recursion that saved some stack space (who cares) but was significantly harder to code....

  • @Cube2deth
    @Cube2deth 4 года назад +2

    Brilliant brother just brilliant your energy, enthusiasm and pedagogy are second to none for DSA on RUclips I love you keep it going 👌❤️

    • @gkcs
      @gkcs  4 года назад

      Thank you 😁

  • @krishnamalhotra3206
    @krishnamalhotra3206 4 года назад +2

    I just got started with CP and before diving directly into pool of codechef question, i needed to get my basics strong. Just a week back LOVE BABBAR uploaded a video describing all necessary maths required before actually solving questions. So he told to checkout fast exponatiation and here i am. It’s really good i must say. I got each and every point clearly. Keep posting good stuff sir. :)

    • @jayshree9037
      @jayshree9037 4 года назад +1

      I am here fron love babbar vid too although i have been here from a long time too

  • @yashsinghal3404
    @yashsinghal3404 4 года назад

    You have explained this method very nicely and i have understood it in no time

  • @dharmeshsharma5106
    @dharmeshsharma5106 5 лет назад +5

    Thank you sir, You are helping me a lot to improve my programming skills.

  • @justforknowledge6367
    @justforknowledge6367 6 лет назад +3

    Thank you, Mr. Sen. Please continue to post more tricks in fast computations.

    • @gkcs
      @gkcs  6 лет назад

      Thanks!

  • @vaibhavsingh2197
    @vaibhavsingh2197 3 года назад

    Watched multiple times but finally got it..thanks Gaurav bhai✌🙂

  • @arghyamazumdar5767
    @arghyamazumdar5767 7 лет назад

    Please post more videos and we will continue to share so that the channel increases popularity

  • @LakshmikanthAyyadevara
    @LakshmikanthAyyadevara 5 лет назад +1

    great explanation for divide and concur algorithm and fast exponent

  • @suuujuuus
    @suuujuuus Год назад +1

    Summary for non-modulo exponentiation
    x^0 = 1
    x^1 = x
    x^e = (
    IF e modulo 2 congurent 0
    THEN (x×x)^(e/2)
    ELSE x × x^(e-1)
    )
    Calculates in less steps than multiplying x*x...*x e-times: e steps vs ~ log 2 e steps
    Uses: x^e = x^(2*e/2) = (x^2)^(e/2)
    Since roots are expensive to calculate for either computer or human mind we want the exponent to stay in IN, and do a little if-case instead to keep e divisible by 2.

    • @suuujuuus
      @suuujuuus Год назад

      Summary for modulo exponentiation
      Term (1)
      x^e % m
      Similar idea to half exponent, calculate this instead:
      Term (2)
      ((x^(e/2) % m)*(x^(e/2) % m)) % m = (x^(e/2) % m)^2 %m
      Just have to prove that this is equal to x^e%m.
      x^(e/2) % m congurent r => x^(e/2) = m * k + r; r 0
      x^1 % m -> x%m
      x^e % m -> (
      IF e % 2 congurent 0
      THEN (x^(e/2) % m)^2 % m
      ELSE (x%m)*(x^(e-1)%m) % m
      )
      I'm using this modulo rule for ELSE case
      a % b congurent c
      d % b congurent e
      ad % b congurent ce

    • @suuujuuus
      @suuujuuus Год назад

      There is a more elegant way to calculate power modulo which simply refines the idea:
      x^e%m :=
      Convert e to binary = list ebin
      d = 1
      FOR (b in ebin) DO (
      d = d^2 % m
      IF (b == 1) THEN d= d*x%m
      )
      RETURN d

  • @dikshasingh6174
    @dikshasingh6174 6 лет назад +2

    Thank you so much for making this effort and sharing your knowledge with us ,It's of great help

  • @yanshuodong9509
    @yanshuodong9509 2 года назад

    Thank you, Gaurav Sen! A very good tutorial!!

  • @samarthjoshi973
    @samarthjoshi973 6 лет назад +5

    Wonderful video sir, but marker wasn't that visible.
    Good job sir, keep inspiring us and create such beautiful content.
    CP beginners and curious minds need such videos.

    • @gkcs
      @gkcs  6 лет назад

      Thanks Samarth!

  • @abhijeetkumar3569
    @abhijeetkumar3569 4 года назад +1

    a^n%m = ((a%m)*n)%m
    Example:
    8^8%6 = ((8%6)*8)%6 = (2*8)%6 = 16%6 = 4
    Something I got while thinking about this problem statement, didn't really think of all the corner cases though.

    • @gkcs
      @gkcs  4 года назад

      Yes. We use this to keep the range within 32 bits.

  • @navneetgupta7967
    @navneetgupta7967 3 года назад

    Basically, you have shot this video on different days. By the way Great content

  • @balajisv4052
    @balajisv4052 6 лет назад +1

    You are like a super computer who thinks quickly. (but quiet slow at addition. XD). You explain it to us after number of times of practices so that you may understand the steps you are writing and you can co-relate them easily. But viewers who are new to this concepts are looking for some clear and patient explanation. Please bring that Gaurav. My kind suggestion and request:) Because I find your videos are damn useful and I want you not to lose any fellow students of your teaching.

    • @gkcs
      @gkcs  6 лет назад

      I'll try and do that 😛

  • @gerardohurtado3394
    @gerardohurtado3394 7 лет назад +4

    Thanks! you made it easier to understand

    • @gkcs
      @gkcs  7 лет назад

      Thanks!

  • @Soh965
    @Soh965 4 года назад +2

    That was really cool! Maybe a little bit more in depth examples would have guided me (a novice) to better understanding! but I researched into the matter for better understanding! Anyway, Thank you for a great explanation!

  • @NitishRaj
    @NitishRaj 5 лет назад

    For Fast exponentiation mostly used in cryptography with some modulus work. So solving this would depends upon choice of either Fermat , FEMA or Euler is preferable.. Implementation could be like yours with some memoization techniques. You have explained a just small bite of it.

  • @abdulmateen1618
    @abdulmateen1618 6 лет назад

    Excellent video. Your explanation is on point.

  • @akshanshsharma6025
    @akshanshsharma6025 3 года назад

    Waao that's awesome guru👍👍👍

  • @jayseagal3681
    @jayseagal3681 3 года назад

    Thank you so much for this bro you are such an inspiration

  • @mahf7739
    @mahf7739 8 месяцев назад

    Thank you, very well explained

  • @Pro3512
    @Pro3512 6 лет назад +2

    Very Nice Explanation !! Just one doubt when its N is odd , we should take the floor value so i guess the component R which is (N/2) should also demand for a code changes as R is now floor(N/2) ?

    • @gkcs
      @gkcs  6 лет назад +1

      We can do a P*P*x in that case, where x is the base and P the current product.

  • @mayukhsengupta1747
    @mayukhsengupta1747 7 лет назад +1

    thanx for the channel sir... helping a lot... keep unloading please....

    • @gkcs
      @gkcs  7 лет назад

      mayukh Sengupta Thanks!

    • @mayukhsengupta1747
      @mayukhsengupta1747 7 лет назад

      sir please try to post a video on introduction to dynamic programming in near future... would be gr8 help... :-)

  • @sumitchauhan3933
    @sumitchauhan3933 7 лет назад

    I did understand the logic behind finding subset using bit .Would you make a tutorial on it. Plss

  • @SKIP-sq5uk
    @SKIP-sq5uk 2 года назад

    what is the range of M in the % code ???

  • @saumyachauhan2381
    @saumyachauhan2381 4 года назад

    Your videos are great!!!..I'm a newbie in cp and these are really helpful :-)

  • @sudheerays9559
    @sudheerays9559 5 лет назад +1

    If anybody wants a problem on this topic
    Here is the link :
    www.codechef.com/JUNE19B/problems/RSIGNS
    Welcome :)

  • @prasanthcherukuri7955
    @prasanthcherukuri7955 6 лет назад

    Sir ,at last why don't you mod a in recursion with m in else case(when N/2 is odd)..15:27

  • @martjao
    @martjao 7 лет назад

    Thank you!, your video helped me a lot to understand better

    • @gkcs
      @gkcs  7 лет назад

      Thanks!

  • @AmitSingh-ut4wt
    @AmitSingh-ut4wt 5 лет назад

    Very Nice Explanation

  • @shivangsaxena494
    @shivangsaxena494 4 года назад

    i didnt understand one thing.. in that code, you wrote R=pow(a,N/2)... but wouldnt that take the program back to the same function even before the lines that are written below are executed?

  • @GaroSlayer
    @GaroSlayer 6 лет назад +1

    Really good explanation

    • @gkcs
      @gkcs  6 лет назад

      Thanks!

  • @qbzon4851
    @qbzon4851 2 года назад

    Thank you! I understand!💖

  • @VikramSingh-oq7pe
    @VikramSingh-oq7pe 7 лет назад

    Thanks, keep making videos I am sure you will earn more subscribers gradually.

    • @gkcs
      @gkcs  7 лет назад

      Thanks Vikram!

  • @ankushpanchal3196
    @ankushpanchal3196 3 года назад +1

    Maza aa Gaya..❤️

  • @amey7064
    @amey7064 5 лет назад

    For Python, what is the time complexity of n**m ?

  • @futurestar1000
    @futurestar1000 7 лет назад

    More efficient approaches please !!!
    concept explanation was good.

    • @gkcs
      @gkcs  7 лет назад

      Thanks for the feedback Arpit!
      This approach is pretty efficient, O(log(N)) is the best we can hope for. There is another approach which is more often used, called binary exponentiation. I will get to that soon :-)

  • @keshavagrawal4088
    @keshavagrawal4088 3 года назад

    The same is given on GFG, but I understood it here.

  • @sadi4266
    @sadi4266 7 лет назад

    If power is 50000,, then how u do the such big power though divide by 2,50000/2 can't calculate by power (2,25000)

  • @vishalkhandate2464
    @vishalkhandate2464 3 года назад

    The value of log 10 base 2 = 3.32.
    So log (10^12) base 2 = 12*log (10) base 2
    = 12*3.32 = 39.84 ≈ 40

  • @PierLim
    @PierLim 5 лет назад

    Thank you Gaurav, this helped!

  • @anubhavsinghal9218
    @anubhavsinghal9218 5 лет назад

    That's a really useful video.

  • @Page_Turners_Hub
    @Page_Turners_Hub 4 года назад

    Thanks for the video. It was helpful

  • @aravindbadavath7369
    @aravindbadavath7369 4 года назад

    while am doing pow(2,1000000005) it is giving me some different number 180942713 ??
    can anyone help me out

  • @madhukiranattivilli2321
    @madhukiranattivilli2321 2 года назад

    Hi Gaurav,
    For aⁿ%b algo, don't u think this might be faster? 👇
    aⁿ%b = (a²%b)ˆn/2 %b
    This too runs in O(logn), but may return pretty quick sometimes, when 'a' becomes 1 due to the impact of the modulus operator
    Cud u please comment?

  • @arpitjainiitr
    @arpitjainiitr 5 лет назад

    Thank you for the explanation of Fast Modulo, really helpful !! It could have been better with an example .

  • @rajeshdansena
    @rajeshdansena 6 лет назад +1

    I went through the code and saw that you have used int and long. As you said in video, What happens if a^N crosses the long and int limits.? Can you please make the changes in code or probably make a video on designing a data structure to store data which is of very high precision integer number like 10^10000 or may be more the. That.

    • @gkcs
      @gkcs  6 лет назад +1

      Biginteger stores very large values, but the point where this technique is useful is when exponentiating while taking a remainder.
      Like 10^1000 mod 47.

    • @rajeshdansena
      @rajeshdansena 6 лет назад +1

      Gaurav Sen got you.! appreciate the quick response. Keep up the good work Buddy :)

  • @suyashmusalgaonkar4041
    @suyashmusalgaonkar4041 6 лет назад

    You sir ,just earned a subscriber

  • @atiyainamdar6458
    @atiyainamdar6458 5 лет назад

    Sometimes students watch in a hurry ,this could be more precise and short

  • @ngneerin
    @ngneerin 8 месяцев назад

    I am interested to know how did you calculate 2 power 30 to be around 1 billion so quick. Is it memory or some trick?

    • @gkcs
      @gkcs  8 месяцев назад +1

      It's memory.
      I know that 2^10 is 1000,
      Double that exponent and you get a million.
      Triple it to get a billion.

  • @AlayDhagia
    @AlayDhagia 4 года назад

    how do we find M for a particular 'a' and 'n'

    • @gkcs
      @gkcs  4 года назад

      M is part of the problem statement. It is usually a large prime number.

  • @nikhilsrivastava9120
    @nikhilsrivastava9120 7 лет назад

    Please post a video solution of COOK-82B.
    I am not able to understand how to get such ideas in maths questions.
    Please make one :)

    • @gkcs
      @gkcs  7 лет назад

      Hey Nikhil, will look into it :)

    • @nikhilsrivastava9120
      @nikhilsrivastava9120 7 лет назад

      Hey, thanks but I understood the approach.
      Not worth making a video :P

  • @subhabera5775
    @subhabera5775 4 года назад

    Thanks this was quite helpful

  • @jadekevinbestami4003
    @jadekevinbestami4003 7 лет назад

    very nice explanation thank you!

    • @gkcs
      @gkcs  7 лет назад

      Thanks Jade!

  • @purvampujari
    @purvampujari 6 лет назад +2

    More problems on dp and graphs bro , Thankyou !

  • @shailshah9021
    @shailshah9021 2 года назад

    def my_pow(self, x: float, n: int) -> float:
    if n < 0: return self.my_pow(1/x, -n)
    if n == 0: return 1
    half_pow = self.my_pow(x, n // 2)
    return half_pow * half_pow * (1 if n % 2 == 0 else x)

  • @Apurva021
    @Apurva021 5 лет назад +1

    Thank you so much brother.. this helped a lot :-)

  • @sourovsarkar8654
    @sourovsarkar8654 2 года назад

    isn't it the big mod ?? or am I mixing up things ?

  • @bhagwatibhalotia7636
    @bhagwatibhalotia7636 7 лет назад +1

    What is the complexity of Math.pow(),? Why can't we simply use it in the case when we don't need to perform %M?

    • @gkcs
      @gkcs  7 лет назад +2

      Math.pow has a complexity of order n.

    • @rohanjain4238
      @rohanjain4238 5 лет назад

      @@gkcs it also allow power upto 10^5

    • @gkcs
      @gkcs  5 лет назад +1

      @@rohanjain4238 Try it in a contest then 😂

    • @rohanjain4238
      @rohanjain4238 5 лет назад

      @@gkcs i here to support you man 👍

    • @gkcs
      @gkcs  5 лет назад +1

      @@rohanjain4238 Ah, sorry I misunderstood 😛

  • @Mr-Producer.
    @Mr-Producer. 6 лет назад

    i didnt get one main thing why do we even need to to split??if we can just use that number raised to the power n.

    • @gkcs
      @gkcs  6 лет назад

      How will you find it to the power n, first? :)

    • @Mr-Producer.
      @Mr-Producer. 6 лет назад

      Gaurav Sen By the use of pow (number,n)function

    • @gkcs
      @gkcs  6 лет назад

      That might take O(n) time. Internally, the pow(number, n) function is using a slower algorithm to compute the exponent, at least in Java.
      This method computes in O(log(N)) time.

    • @Mr-Producer.
      @Mr-Producer. 6 лет назад

      Gaurav Sen thank-you Gaurav :) . Just one more question I wanted to know if it makes any difference in c++?

  • @swayamraina4564
    @swayamraina4564 6 лет назад

    Can you please explain the iterative version of the code,
    private static long power(long x, long y, long m) {
    long ans = 1L;
    while (y>0) {
    if ((y&1)==1) {
    ans = (ans * x) % m;
    }
    y = y>>1;
    x = (x * x) % m;
    }
    return ans % m;
    }

  • @stanger963
    @stanger963 5 лет назад

    You are extremely smart

  • @Kushagra_21
    @Kushagra_21 4 года назад

    what if N is negative

  • @vibewith100
    @vibewith100 5 лет назад

    Thanks Bro, It really help me

  • @SarojMohan783
    @SarojMohan783 6 лет назад +1

    helpful video sir

    • @gkcs
      @gkcs  6 лет назад

      Thanks!

  • @yashmundra8497
    @yashmundra8497 6 лет назад

    in the proof for the identity with mod ,why u wrote r squared mod m instead of r mod m+r mod m?

    • @gkcs
      @gkcs  6 лет назад

      We are finding the exponent using multiplication. The identity is ((a+r)*(a+r))%m.

  • @harshitjain6241
    @harshitjain6241 7 лет назад

    The videos was great but the marker was not visible sometimes and also the lighting issue!! Anyways Keep it up

    • @gkcs
      @gkcs  7 лет назад

      Hi Harshit!
      Thanks for noting that, I will keep that in mind next time!

  • @amitswami4984
    @amitswami4984 7 лет назад

    Can you make a tutorial on soft heap..Please?

    • @gkcs
      @gkcs  7 лет назад

      +Amit Swami Sure! Will add that to the list :-)

    • @gkcs
      @gkcs  7 лет назад +1

      Hey Amit, I just read up quite a bit on the soft heap data structure. And while it has interesting properties, it isn't practically applicable. So, for now, I will keep from making the video :-)
      If you want some good sources to learn about them, here are a few links:
      stackoverflow.com/questions/26126170/soft-heaps-what-is-corruption-and-why-is-it-useful
      www.cs.princeton.edu/~chazelle/pubs/sheap.pdf
      Cheers!

  • @kashyapmahanta289
    @kashyapmahanta289 7 лет назад

    Hello sir can u upload a tutorial on FFT??

    • @gkcs
      @gkcs  7 лет назад

      Hi Kashyap!
      FFT looks tough! I am adding that to the list, but I can't promise anything on that soon :-/

  • @sunilanumolu9211
    @sunilanumolu9211 7 лет назад

    it is simply suberb,excellent!!

    • @gkcs
      @gkcs  7 лет назад

      Thanks Sunil!

  • @greyrabbit2157
    @greyrabbit2157 5 лет назад

    Very helpful !

  • @chandrahas2567
    @chandrahas2567 5 лет назад +1

    Finally solved like a true engineer : lol

    • @chandrahas2567
      @chandrahas2567 5 лет назад

      Hey @Gaurav sen I would be happy if you take some time and reply for this.
      I have studied Engineering in a local state college. It's been around 2 months I joined in a company, which pays me 7Lpa.
      I know that this is not a good package but for local college guys its a very good package ( coz avg package in local colleges is 3.5Lpa and I know avg is around 10+ in NIT's, IIT's).
      Where I'm right now:
      ----------------
      - Coding: Need to learn trees, graphs, Dp and standard/repeated/imp algorithms
      - Projects: I didn't do any projects till now ( like no development at all).
      - Technologies: I know java, .net, angular, c#
      So can someone tell me what are all the required things to learn for getting placed in a Good company ( SDE1,2 role ), How to get Interview calls or Interview chances from good companies. And especially what strategy(like time and hours per day) I should be following to achieve it.
      I would be thankful for any advice or any video or article link regarding this.

    •  5 лет назад +1

      @@chandrahas2567 refer quora for such suggestions . do we development like learn about html +css , js , nodejs, npm , js frameworks, js libraries ,webpack, database (mongodb), docker , etc...

  • @Suresh-Vuppala
    @Suresh-Vuppala 7 лет назад

    can u plz upload Matrix Exponentiation & CRT ?

    • @gkcs
      @gkcs  7 лет назад

      Working on Matrix Exponentiation now! Do you mean Chinese Remainder Theorem as CRT?

    • @Suresh-Vuppala
      @Suresh-Vuppala 7 лет назад

      yes

  • @IVIose
    @IVIose 6 лет назад

    excellent video

    • @gkcs
      @gkcs  6 лет назад

      Thanks!

  • @supriyantapoddar6129
    @supriyantapoddar6129 7 лет назад

    Finally, how could I get the value of a^N from (a^N%M)?????????? (For the last method)

    • @gkcs
      @gkcs  7 лет назад

      Supriyanta Poddar, you can't. That question is often asked in competitive programming, so the second technique talks about it.
      If you want a^b, you need to use the first technique.

    • @supriyantapoddar6129
      @supriyantapoddar6129 7 лет назад

      okay...thanks....could you please help me find the factorial of nos more than 50 in C++ which are not fit in 8bit...!!!!!!

    • @gkcs
      @gkcs  7 лет назад

      You could try the BigInteger class in C++. It allows operations on large integers by internally keeping an array of digits.

    • @supriyantapoddar6129
      @supriyantapoddar6129 7 лет назад

      thanks...lets see... :)

  • @PrathamGupta2408
    @PrathamGupta2408 4 года назад

    Hi! Are yu wearing contact lens?

  • @woofelator
    @woofelator 3 года назад

    Now the question is.... why do I never think of these things?

  • @k.k.harjeeth5422
    @k.k.harjeeth5422 Год назад

    good one !

    • @gkcs
      @gkcs  Год назад

      Thanks!

  • @yash.dwivedi
    @yash.dwivedi 4 года назад

    Thanks sir

  • @robertcraig2662
    @robertcraig2662 5 лет назад

    Thanks brother!!!

  • @sheshitkarthikeya1528
    @sheshitkarthikeya1528 6 лет назад

    Awesome bro!!

  • @rohitthapliyal4246
    @rohitthapliyal4246 7 лет назад

    Perfect!

    • @gkcs
      @gkcs  7 лет назад +1

      Thanks Rohit!

  • @halanasser5589
    @halanasser5589 5 лет назад

    Please explain Chinese reminder therom in RSA decryption

  • @adityabantwal
    @adityabantwal 7 лет назад

    thanks

    • @gkcs
      @gkcs  7 лет назад

      :-)

  • @lennelbalando5615
    @lennelbalando5615 3 года назад

    nice

  • @adithyabhat4770
    @adithyabhat4770 5 лет назад

    AMAZING

  • @cse37ramantripathi72
    @cse37ramantripathi72 4 года назад

    todd!

  • @RINMeghana
    @RINMeghana 6 лет назад

    Hiii 2**3 please tell me this type of question s

  • @surajkushwah3221
    @surajkushwah3221 5 лет назад

    pow() sorry I am noob

  • @ewan5536
    @ewan5536 7 лет назад +3

    Your Indian accent is so strong you should slow down so I can understand your English 🤔

    • @SarojMohan783
      @SarojMohan783 6 лет назад +5

      I think u should learn english 1st thomas if u cant understand such clear english. Pehaps u hv learnt hebrew in place
      of english . You belong to that class of people who do not hv any work and see videos only to comment rubbish

  • @subhabera5775
    @subhabera5775 4 года назад

    Thanks this was quite helpful

  • @whatidashdashdash8866
    @whatidashdashdash8866 5 лет назад

    Thanks