From 36ecbc75f829d2a921b90c3233afa9f0c804b5ef Mon Sep 17 00:00:00 2001 From: Erfan Alimohammadi Date: Tue, 14 May 2019 17:12:15 +0430 Subject: [PATCH 1/5] Fix typo --- strings/manacher.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/strings/manacher.py b/strings/manacher.py index 9a44b19ba77a..e73e173b43e0 100644 --- a/strings/manacher.py +++ b/strings/manacher.py @@ -1,4 +1,4 @@ -# calculate palindromic length from center with incresmenting difference +# calculate palindromic length from center with incrementing difference def palindromic_length( center, diff, string): if center-diff == -1 or center+diff == len(string) or string[center-diff] != string[center+diff] : return 0 From 0520bce50b00226df42cf018a40a8468fc172735 Mon Sep 17 00:00:00 2001 From: Erfan Alimohammadi Date: Mon, 27 May 2019 17:23:33 +0430 Subject: [PATCH 2/5] Add fermat's little theorem --- maths/fermat_little_theorem.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 maths/fermat_little_theorem.py diff --git a/maths/fermat_little_theorem.py b/maths/fermat_little_theorem.py new file mode 100644 index 000000000000..f21164847cf5 --- /dev/null +++ b/maths/fermat_little_theorem.py @@ -0,0 +1,30 @@ +# Python program to show the usage of fermat's little theorem in a division +# Time complexity: O(log p) +# 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 + + +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: +print((a / b) % p == (a * binary_exponentiation(b, p - 2, p)) % p) + +# using Python operators: +print((a / b) % p == (a * b ** (p - 2)) % p) From e9da6d5b26af6442828284548886e4f708c790bb Mon Sep 17 00:00:00 2001 From: John Law Date: Mon, 27 May 2019 21:13:53 +0800 Subject: [PATCH 3/5] Update fermat_little_theorem.py --- maths/fermat_little_theorem.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maths/fermat_little_theorem.py b/maths/fermat_little_theorem.py index f21164847cf5..5682f6b6287f 100644 --- a/maths/fermat_little_theorem.py +++ b/maths/fermat_little_theorem.py @@ -1,6 +1,6 @@ -# Python program to show the usage of fermat's little theorem in a division +# Python program to show the usage of Fermat's little theorem in a division # Time complexity: O(log p) -# According to fermat's little theorem, (a / b) mod p always equals a * (b ^ (p - 2)) mod p +# 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 From 4e10b6a79aff888f0369b393452c55e656746dd3 Mon Sep 17 00:00:00 2001 From: Erfan Alimohammadi Date: Mon, 27 May 2019 22:49:35 +0430 Subject: [PATCH 4/5] Fix comments --- maths/fermat_little_theorem.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/maths/fermat_little_theorem.py b/maths/fermat_little_theorem.py index 5682f6b6287f..34a610138976 100644 --- a/maths/fermat_little_theorem.py +++ b/maths/fermat_little_theorem.py @@ -1,5 +1,4 @@ # Python program to show the usage of Fermat's little theorem in a division -# Time complexity: O(log p) # 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 @@ -23,7 +22,7 @@ def binary_exponentiation(a, n, mod): a = 1000000000 b = 10 -# using binary exponentiation function: +# using binary exponentiation function, O(log(p)): print((a / b) % p == (a * binary_exponentiation(b, p - 2, p)) % p) # using Python operators: From 9f7025094ef5e135beb2a858b70de982de813936 Mon Sep 17 00:00:00 2001 From: Erfan Alimohammadi Date: Mon, 27 May 2019 22:56:16 +0430 Subject: [PATCH 5/5] Add Wikipedia reference --- maths/fermat_little_theorem.py | 1 + 1 file changed, 1 insertion(+) diff --git a/maths/fermat_little_theorem.py b/maths/fermat_little_theorem.py index 34a610138976..93af98684894 100644 --- a/maths/fermat_little_theorem.py +++ b/maths/fermat_little_theorem.py @@ -1,6 +1,7 @@ # 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):