Exponentiation - Calculate Pow(x,n) using recursion

แชร์
ฝัง
  • เผยแพร่เมื่อ 6 ม.ค. 2025

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

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

    watching in 2020 ,after 8 years still a lot better explanation than many tutorials on youtube , (quarantine)

    • @Akash-ry1xc
      @Akash-ry1xc 4 ปีที่แล้ว

      true dude

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

      2021 now and it's still so relevant.

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

      2024 still relevant

  • @shatrughngupta5419
    @shatrughngupta5419 7 ปีที่แล้ว +43

    For people who are confused that in odd case, it will be O(n):
    It is still O(log n) because in next call, n will become even.
    P(17) => P(16) => P(8) => P(4) => P(2) => P(1) => P(0)
    P(15) => P(14) => P(7) => P(6) => P(3) => P(2) => P(1) => P(0)
    You can actually tweak the odd case as: x^((n-1)/2) * x^((n-1)/2) * x [as (n-1)/2 + (n-1)/2 + 1 = n]. It will still be O(log n) but the number of calls would reduce a bit. It is also now evident from the looks of the expression that it will be O(log n).
    P(17) => P(8) => P(4) => P(2) => P(1) => P(0)
    P(15) => P(7) => P(3) => P(1) => P(0)

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

      where did you learn these things man?? you are a genius!!

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

      @@shashankbangera7753 Take an analysis of algorithms course.

  • @FirstLast-ws7zw
    @FirstLast-ws7zw 8 ปีที่แล้ว +45

    Now I believe the author who said when you look at recursive code for the first time it looks like black magic.

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

    Watching it again after an year and still no one is better than you

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

    one of the best explanations on this question so far!

  • @Shikschauhan
    @Shikschauhan 3 ปีที่แล้ว +1

    Watching in 2021... and its still 1000x better than any tutorial

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

    You can improve this further by doing the y calculation just before the first if statement and returning y*y inside the first else if and returning y*y*x inside the else. Here's the code in C++:
    double myPow(double x, int n) {
    if(n == 0) return 1;
    double y = myPow(x,n/2);
    if (n % 2 == 0) return y*y;
    else return n < 0 ? 1/x*y*y : x*y*y;
    }
    The last line accounts for the fact that n can be negative, in which case we should return 1/x*y*y.

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

    Thank you so much. This is the best explanation I found online ☺️

  • @da2lordr.i.m_875
    @da2lordr.i.m_875 3 ปีที่แล้ว

    The video was posted back in 2012 still better than recent videos on the same topic.

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

    Wonderful solution!! Keep up the good work!

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

    How does y

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

    your explanation is so amazing thank you so much man

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

    very useful video...Now, I clearly understand about power function in program and I also written my own code in c.
    #include
    int power(int x,int n){
    if(n==0)
    return 1;
    else if(n%2==0){
    int y=power(x,n/2);
    return y*y;
    }
    else{
    int y=power(x,n/2);
    return y*y*x;
    }
    }

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

    In case of pinto's algorithm the odd case can be handled in this manner: x^n = x^floor(n/2) *x^floor(n/2) *x.. It that case efficiency can be highly increased if n is odd...

  • @toastersman217
    @toastersman217 10 ปีที่แล้ว

    Very well explained and very interesting! Thank you for these videos!

  • @mycodeschool
    @mycodeschool  12 ปีที่แล้ว

    Thanks Anshul !

    • @AshrafKhan-vt4ph
      @AshrafKhan-vt4ph 5 ปีที่แล้ว

      Sir how computer know that y=power(x,n/2) is actually y=x^n/2????? That is my complete confusion in this program .please.....tell me

    • @AshrafKhan-vt4ph
      @AshrafKhan-vt4ph 5 ปีที่แล้ว

      Sir please explain

  • @tomasz-rozanski
    @tomasz-rozanski 8 ปีที่แล้ว

    You can add one more condition, which (for n >= 1) will save you one extra function call:
    *if (n == 1) return x;*

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

    couldn't but comment.....very nice explanation ..thanks a lot

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

    dude.. nice videos you make .... just arrange them in sequence... like after this there should be the calculation of time complexities but next video in the list is modular exponentiation... thanks

  • @NhiNguyen-yo2pm
    @NhiNguyen-yo2pm 2 ปีที่แล้ว

    excellent explanation!

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

    Can't thank you enough :) great vids ... keep it up

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

    What will be complexity of pinto's code if we don't use y , just use redundant calls again ?

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

    I got referred to this channel by my university

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

    What about the case when x and n are not only integers but any double value?

  • @NithishKumar-se9ev
    @NithishKumar-se9ev 4 ปีที่แล้ว +1

    One more optimization:
    for n=odd,don't need to do x*pow(x,n-1)
    instead just return y*y*x

    • @NikhilKumar-tk3rh
      @NikhilKumar-tk3rh 2 ปีที่แล้ว

      I don't think that it will work...beacuse you need recursive call to reach base condition..

    • @NithishKumar-se9ev
      @NithishKumar-se9ev 2 ปีที่แล้ว

      @@NikhilKumar-tk3rh
      I'm sure that'll work...
      Try for x^n, x=2,n=3
      y=Pow(2, 3/2)
      =Pow(2, 1)
      =2
      2^3=y*y*x
      =2*2*2
      =8

    • @NikhilKumar-tk3rh
      @NikhilKumar-tk3rh 2 ปีที่แล้ว

      @@NithishKumar-se9ev just did some workout on paper...Looks like it will work 👍

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

    anyone can share the code? I tried the exact code and it gives stack overflow error.

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

    What would be the Time complexity ?

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

    this was very helpful, thanks a lot

  • @QuinsCoins
    @QuinsCoins 8 ปีที่แล้ว

    Awesome explanation, thank you!!!

  • @PawanKumar-sw6sk
    @PawanKumar-sw6sk 7 ปีที่แล้ว +1

    After calling the base case(x^0), how does it find (x^(n/2))?

    • @NikhilKumar-tk3rh
      @NikhilKumar-tk3rh 2 ปีที่แล้ว

      all previous call will be pushed into callsatack and will be in pause state until its turn of execution comes..

  • @atulavhad1661
    @atulavhad1661 8 ปีที่แล้ว

    Which software do you use?I also wish to make some competitive coding videos.

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

    How does java know pow(x,n-1)== x^n-1??

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

      same doubt

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

      Java knows multiplication so essentially, you are reducing the problem to the base case scenario which is x^k where k is 0 and solving up the recursive tree using multiplication.

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

    output = num * RecursivePower(nb, power/2) * RecursivePower(nb, power/2) // does this work for odd, if yes, why?

  • @476megaman
    @476megaman 7 ปีที่แล้ว

    is Albert's algorithm an example of divide and conquer?

  • @AnkitAryaSingh
    @AnkitAryaSingh 8 ปีที่แล้ว +1

    you are awesome man !

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

    how it will work after removing pow function return

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

    n is odd x^n can be written as x * x^(n-1)/2 * x^(n-1)/2

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

    Pinto is so smart!

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

    thanks, nice explanation !

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

    How is Pinto's solution O(log n)?
    It still O(n) in case of odd numbers. Shouldn't it be Ω(log n) since it's a lower bound.

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

      Exactly!

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

      It is still O(log n). Please see this comment on main thread: th-cam.com/video/wAyrtLAeWvI/w-d-xo.html&lc=z13igvcwuracwzvuh23lfp4zzwnhg5135
      EDIT: Copy-paste the above link in a new tab, clicking it won't work.

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

      Shatrughn Gupta yes I realized that shortly after commenting. Thanks :)

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

      for the odd case, we can use x*pow(x,n/2)*pow(x,n/2) that will work better than x*pow(x,n-1).

    • @NikhilKumar-tk3rh
      @NikhilKumar-tk3rh 2 ปีที่แล้ว

      it will work since in the odd case recursive call for n-1 is called and it becomes even then again call for n/2 resumes...

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

    simply the best

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

    Thank you!

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

    Is this for java

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

    Amazing!

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

    for the odd case, we can use x*pow(x,n/2)*pow(x,n/2) that will work better than x*pow(x,n-1).

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

      this channel will also help you th-cam.com/channels/oEt3glB4rWSq5zEhSGhUWA.html?view_as=subscriber

  • @anshuljain8998
    @anshuljain8998 12 ปีที่แล้ว

    Good Explanation!!!!

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

    nice one thanks.

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

    Hey, this doesnt handle the case where n is negative

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

    this one is even better
    int power(int x, unsigned int y)
    {
    int temp;
    if( y == 0)
    return 1;
    temp = power(x, y/2);
    if (y%2 == 0)
    return temp*temp;
    else
    return x*temp*temp;
    }
    int main() {
    printf("%d",power(2,2));
    return 0;
    }

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

    Where can I find pinto? I wanna thank him:))

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

    G.O.A.T

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

    wheres the code

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

      The one making the video is dead brother

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

      @@vedsinha9905That's very sad. What is the name of him?

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

      @@ksmfg4326 Harsha Suryanarayana; aka HumbleFool

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

    Nice

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

    Hey, your voice seems very familiar. Were you in Neso academy?

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

    Pinto is OP

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

    albert is now depressed.

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

    awesomeeeeeeeeee!!!!!!!!!!!!!!!!!!!!!!!