mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-30 16:31:08 +00:00
c909da9b08
* pre-commit: Upgrade psf/black for stable style 2023 Updating https://github.com/psf/black ... updating 22.12.0 -> 23.1.0 for their `2023 stable style`. * https://github.com/psf/black/blob/main/CHANGES.md#2310 > This is the first [psf/black] release of 2023, and following our stability policy, it comes with a number of improvements to our stable style… Also, add https://github.com/tox-dev/pyproject-fmt and https://github.com/abravalheri/validate-pyproject to pre-commit. I only modified `.pre-commit-config.yaml` and all other files were modified by pre-commit.ci and psf/black. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
31 lines
816 B
Python
31 lines
816 B
Python
# Python program to show the usage of Fermat's little theorem in a division
|
|
# According to Fermat's little theorem, (a / b) mod p always equals
|
|
# a * (b ^ (p - 2)) mod p
|
|
# Here we assume that p is a prime number, b divides a, and p doesn't divide b
|
|
# Wikipedia reference: https://en.wikipedia.org/wiki/Fermat%27s_little_theorem
|
|
|
|
|
|
def binary_exponentiation(a, n, mod):
|
|
if n == 0:
|
|
return 1
|
|
|
|
elif n % 2 == 1:
|
|
return (binary_exponentiation(a, n - 1, mod) * a) % mod
|
|
|
|
else:
|
|
b = binary_exponentiation(a, n / 2, mod)
|
|
return (b * b) % mod
|
|
|
|
|
|
# a prime number
|
|
p = 701
|
|
|
|
a = 1000000000
|
|
b = 10
|
|
|
|
# using binary exponentiation function, O(log(p)):
|
|
print((a / b) % p == (a * binary_exponentiation(b, p - 2, p)) % p)
|
|
|
|
# using Python operators:
|
|
print((a / b) % p == (a * b ** (p - 2)) % p)
|