forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
1,007 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
""" | ||
A Krishnamurthy number is a number whose sum total of the factorials of each digit is equal to the number itself. | ||
Here's what I mean by that: | ||
"145" is a Krishnamurthy Number because, | ||
1! + 4! + 5! = 1 + 24 + 120 = 145 | ||
"40585" is also a Krishnamurthy Number. | ||
4! + 0! + 5! + 8! + 5! = 40585 | ||
"357" or "25965" is NOT a Krishnamurthy Number | ||
3! + 5! + 7! = 6 + 120 + 5040 != 357 | ||
The following function will check if a number is a Krishnamurthy Number or not and return a boolean value. | ||
""" | ||
|
||
|
||
def find_factorial(n): | ||
fact = 1 | ||
while n != 0: | ||
fact *= n | ||
n -= 1 | ||
return fact | ||
|
||
|
||
def krishnamurthy_number(n): | ||
if n == 0: | ||
return False | ||
sum_of_digits = 0 # will hold sum of FACTORIAL of digits | ||
temp = n | ||
|
||
while temp != 0: | ||
|
||
# get the factorial of of the last digit of n and add it to sum_of_digits | ||
sum_of_digits += find_factorial(temp % 10) | ||
|
||
# replace value of temp by temp/10 | ||
# i.e. will remove the last digit from temp | ||
temp //= 10 | ||
|
||
# returns True if number is krishnamurthy | ||
return (sum_of_digits == n) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
"""Magic Number | ||
A number is said to be a magic number, | ||
if the sum of its digits are calculated till a single digit recursively | ||
by adding the sum of the digits after every addition. | ||
If the single digit comes out to be 1,then the number is a magic number. | ||
Example: | ||
Number = 50113 => 5+0+1+1+3=10 => 1+0=1 [This is a Magic Number] | ||
Number = 1234 => 1+2+3+4=10 => 1+0=1 [This is a Magic Number] | ||
Number = 199 => 1+9+9=19 => 1+9=10 => 1+0=1 [This is a Magic Number] | ||
Number = 111 => 1+1+1=3 [This is NOT a Magic Number] | ||
The following function checks for Magic numbers and returns a Boolean accordingly. | ||
""" | ||
|
||
|
||
def magic_number(n): | ||
total_sum = 0 | ||
|
||
# will end when n becomes 0 | ||
# AND | ||
# sum becomes single digit. | ||
while n > 0 or total_sum > 9: | ||
|
||
# when n becomes 0 but we have a total_sum, | ||
# we update the value of n with the value of the sum digits | ||
if n == 0: | ||
n = total_sum # only when sum of digits isn't single digit | ||
total_sum = 0 | ||
total_sum += n % 10 | ||
n //= 10 | ||
|
||
# Return true if sum becomes 1 | ||
return total_sum == 1 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# extended_gcd(a, b) modified from | ||
# https://github.com/keon/algorithms/blob/master/algorithms/maths/extended_gcd.py | ||
|
||
def extended_gcd(a: int, b: int) -> [int, int, int]: | ||
"""Extended GCD algorithm. | ||
Return s, t, g | ||
such that a * s + b * t = GCD(a, b) | ||
and s and t are co-prime. | ||
""" | ||
|
||
old_s, s = 1, 0 | ||
old_t, t = 0, 1 | ||
old_r, r = a, b | ||
|
||
while r != 0: | ||
quotient = old_r // r | ||
|
||
old_r, r = r, old_r - quotient * r | ||
old_s, s = s, old_s - quotient * s | ||
old_t, t = t, old_t - quotient * t | ||
|
||
return old_s, old_t, old_r | ||
|
||
|
||
def modular_inverse(a: int, m: int) -> int: | ||
""" | ||
Returns x such that a * x = 1 (mod m) | ||
a and m must be coprime | ||
""" | ||
|
||
s, t, g = extended_gcd(a, m) | ||
if g != 1: | ||
raise ValueError("a and m must be coprime") | ||
return s % m |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
def power(a: int, n: int, r: int = None): | ||
""" | ||
Iterative version of binary exponentiation | ||
Calculate a ^ n | ||
if r is specified, return the result modulo r | ||
Time Complexity : O(log(n)) | ||
Space Complexity : O(1) | ||
""" | ||
ans = 1 | ||
while n: | ||
if n & 1: | ||
ans = ans * a | ||
a = a * a | ||
if r: | ||
ans %= r | ||
a %= r | ||
n >>= 1 | ||
return ans | ||
|
||
|
||
def power_recur(a: int, n: int, r: int = None): | ||
""" | ||
Recursive version of binary exponentiation | ||
Calculate a ^ n | ||
if r is specified, return the result modulo r | ||
Time Complexity : O(log(n)) | ||
Space Complexity : O(log(n)) | ||
""" | ||
if n == 0: | ||
ans = 1 | ||
elif n == 1: | ||
ans = a | ||
else: | ||
ans = power_recur(a, n // 2, r) | ||
ans = ans * ans | ||
if n % 2: | ||
ans = ans * a | ||
if r: | ||
ans %= r | ||
return ans | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +0,0 @@ | ||
|
||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
def multiply(matA: list, matB: list) -> list: | ||
""" | ||
Multiplies two square matrices matA and matB od size n x n | ||
Time Complexity: O(n^3) | ||
""" | ||
n = len(matA) | ||
matC = [[0 for i in range(n)] for j in range(n)] | ||
|
||
for i in range(n): | ||
for j in range(n): | ||
for k in range(n): | ||
matC[i][j] += matA[i][k] * matB[k][j] | ||
|
||
return matC | ||
|
||
def identity(n: int) -> list: | ||
""" | ||
Returns the Identity matrix of size n x n | ||
Time Complecity: O(n^2) | ||
""" | ||
I = [[0 for i in range(n)] for j in range(n)] | ||
|
||
for i in range(n): | ||
I[i][i] = 1 | ||
|
||
return I | ||
|
||
def matrix_exponentiation(mat: list, n: int) -> list: | ||
""" | ||
Calculates mat^n by repeated squaring | ||
Time Complexity: O(d^3 log(n)) | ||
d: dimesion of the square matrix mat | ||
n: power the matrix is raised to | ||
""" | ||
if n == 0: | ||
return identity(len(mat)) | ||
elif n % 2 == 1: | ||
return multiply(matrix_exponentiation(mat, n - 1), mat) | ||
else: | ||
tmp = matrix_exponentiation(mat, n // 2) | ||
return multiply(tmp, tmp) |
Oops, something went wrong.