Binary Exponentiation Algorithm Explained

preview_player
Показать описание
Hi guys,

My name is Michael Lin and this is my programming youtube channel. I like C++ and please message me or comment on what I should program next. It really helps out a lot.

Rate, Comment, Subscribe

Personal Accounts:
Instagram: @michaellin250
Twitter: @Michaellin250
Рекомендации по теме
Комментарии
Автор

Recursively:
```
def b_exp(num, power)
return num if power == 1

# integer division, e. g. 7 / 3 returns 2
half_power = power / 2

# control for whether we can split the power evenly
one_or_num = power.even? ? 1 : num

half_exp = b_exp(num, half_power)
half_exp * half_exp * one_or_num
end
```

It works (on large numbers even faster than native Ruby method that's implemented in C), but how would you tail call optimise it?

Granted, it'll blow the stack only if your power is around 2^(your stack size) order of magnitude (i. e. really large), but still.

em_the_bee
Автор

hey man can we use this approch to solve fibonaci number

tarundhouni
Автор

bro I cant understand the mod which you took last

RAMKUMAR-ebkq
Автор

I do not understsand how this is less computation.
At the end, we are still flipping the same amount of bits when we are doing the last multiplication, no?

jintsuubest
Автор

hey, I checked your hackerrank profile and it showed you got c++ basic certificate but there is no skills test for c++ so how did you get those then?

MilindGupta
Автор

Sorry but I feel your explaination isn't good at all. cp-algorithms has all of this and I need a more detailed guidance from you.

MinhHien-ntyx