diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9b7ba47 --- /dev/null +++ b/.gitignore @@ -0,0 +1,18 @@ +# Created by https://www.toptal.com/developers/gitignore/api/venv +# Edit at https://www.toptal.com/developers/gitignore?templates=venv + +### venv ### +# Virtualenv +# http://iamzed.com/2009/05/07/a-primer-on-virtualenv/ +.Python +[Bb]in +[Ii]nclude +[Ll]ib +[Ll]ib64 +[Ll]ocal +[Ss]cripts +pyvenv.cfg +.venv +pip-selfcheck.json + +# End of https://www.toptal.com/developers/gitignore/api/venv diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..dffe746 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2025 Shaporova-Nadya + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/src/fast_pow.py b/src/fast_pow.py index 1ba1dc1..b17fa05 100644 --- a/src/fast_pow.py +++ b/src/fast_pow.py @@ -1,6 +1,12 @@ def fastPow(number, power): - result = number - while power != 1: - result *= result - power = power // 2 + if power < 0: + return 1 + result = 1 + + while power > 0: + if power % 2 == 1: + result *= number + number *= number + power //= 2 + return result diff --git a/test/test_fast_pow.py b/test/test_fast_pow.py index 5e4aebd..ac5b5ef 100644 --- a/test/test_fast_pow.py +++ b/test/test_fast_pow.py @@ -1,9 +1,11 @@ -from src.fast_pow import fastPow - +import pytest +from fast_pow import fastPow def test_two_power_two(): assert fastPow(2, 2) == 4 - def test_negative(): - assert fastPow(-1, 4) == 1 + assert fastPow(256, 0) == 1 + +def test_neg(): + assert fastPow(2, -4) == 1