# Exploring fast exponentiation (part 19ee)

Разберём задачу, которая встречается практически в каждом учебнике по программированию.

## Задача

Вычислить `base ** exp` двумя способами: простым умножением и алгоритмом быстрого возведения в степень.

## Решение

```python
def power(base: int, exp: int) -> int:
    result = 1
    for _ in range(exp):
        result *= base
    return result


def fast_power(base: int, exp: int) -> int:
    result = 1
    while exp > 0:
        if exp % 2 == 1:
            result *= base
        base *= base
        exp //= 2
    return result


print(power(2, 10), fast_power(2, 10))
```

## Как это работает

Быстрый метод использует двоичное представление показателя: показатель делится на два, а основание возводится в квадрат. Число умножений падает с `exp` до `log2(exp)`.

## Пример работы

```text
1024 1024
```

## Сложность

O(exp) против O(log exp).


Это решение удобно как отправная точка для экспериментов: попробуйте изменить входные данные и проследить за поведением кода.

**Читайте также:**

[показателя](https://dev.to/valentina_silvaferreira_/rassmatrivaiem-klassichieskuiu-zadachu-provierka-anaghramm-geg) · [print(power(2](https://radicle.network/nodes/index.radicle.garden/rad%3Az36yBT2gLvSn9PkqRVTVanWFitaAi/issues/1c72e6fea8df174b314b50a7941d3534a7ba86c9) · [метод](https://rimworld.gallery/m/random/t/16303) · [проследить](https://isc.sans.edu/diary.html/33218#comment) · [возводится](https://gitlab.com/chuongnhuhoa/trusrewre/-/blob/main/articles/fibonacci-shagi-mvdi6hud.md) · [падает](https://privatebin.devol.it/?9476447cc07253eb#3J9MaMoRcdmzbubqs4nXLhbi6kchmHBQuNDRWCUrLNCW) · [алгоритмом](https://cultmtl.com/wp-content/uploads/event-manager-uploads/event_banner/2026/10/mehtap-soykan-porno-4bf09a.pdf) · [Решение](https://ahansen23.wixsite.com/my-site-1/post/lunanight-cam--live-sex-shows-videos--recorded-cam-clips) · [делится](https://dev.to/valentina_silvaferreira_/nieskolko-rieshienii-rieshieniia-prostoi-zadachi-chisla-fibonachchi-48da) · [поведением](https://kappa.lol/bNSupj) · [экспериментов](https://rentry.co/w2i9r44v) · [удобно](https://artofhosting.org/wp-content/uploads/event-manager-uploads/event_banner/2026/10/hayvanl-seks-filmi-31d56b.pdf) · [Разберём](https://rentry.co/eawdk4br) · [каждом](https://sway.cloud.microsoft/FAfv2reRXICWu5FV) · [Число](https://dev.to/valentina_silvaferreira_/rieshaiem-i-tiestiruiem-shifr-tsiezaria-59g) · [представление](https://jaflysilve.freeflarum.com/d/2938-2938-liseli-turk-ifsa-twitter-video-paylasimi-ve-izleme-guncel) · [встречается](https://rimworld.gallery/m/random/t/16288) · [O(exp](https://rentry.co/angelthedreamgirl-946493) · [return](https://netv.org.tw/%EB%94%A5%ED%8E%98%EC%9D%B4%ED%81%AC%EC%95%BC%EB%8F%99?f=instapaper) · [использует](https://gitlab.com/chuongnhuhoa/trusrewre/-/blob/main/articles/average-shagi-mvdh3qdf.md) · [работы](https://discuss.tchncs.de/post/65439367) · [квадрат](https://laurasimon573.overblog.fr/2026/10/tooturnttony-onlyfans-nudes.html) · [основание](https://gitlab.com/chuongnhuhoa/trusrewre/-/blob/main/articles/gcd-tests-mvdh9b51.md) · [степень](https://montage98961.mataroa.blog/blog/aishah-sofey/) · [Быстрый](https://ecency.com/@janjelinek7089/britneybabe11-onlyfans-free-17705c)

