Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
create the Math folder add some math algorithms.
  • Loading branch information
HirunaVishwamith committed Nov 11, 2022
commit cc63cd787f1801f1b57a63fa4dd64c727856ddf7
28 changes: 28 additions & 0 deletions src/main/python/algorithms/math/GCD.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'''
* @author (original JAVA) William Fiset, [email protected]
* (conversion to Python) Hiruna Vishwamith, [email protected]
* @date 11 Nov 2022


An implementation of finding the GCD of two numbers. Time Complexity: Time Complexity ~O(log(a + b))

'''

def gcd(a,b):
if a<0:
a=-a
if b==0:
return a
else:
return gcd(b,a%b)


print(gcd(12, 18)) # 6
print(gcd(-12, 18)) # 6
print(gcd(12, -18)) # 6
print(gcd(-12, -18)) # 6
print(gcd(5, 0)) # 5
print(gcd(0, 5)) # 5
print(gcd(-5, 0)) # 5
print(gcd(0, -5)) # 5
print(gcd(0, 0)) # 0
35 changes: 35 additions & 0 deletions src/main/python/algorithms/math/IsPrime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'''
* @author (original JAVA) William Fiset, [email protected]
* (conversion to Python) Hiruna Vishwamith, [email protected]
* @date 11 Nov 2022

Tests whether a number is a prime number or not Time Complexity: O(sqrt(n))

'''



import math

def isPrime(n):
if n<2:
return False
if (n==2)or(n==3):
return True
if n%2==0 or n%3==0:
return False

limit=int(math.sqrt(n))
for i in range(5,limit+1,6):
if n%i==0 or (n%(i+2)==0):
return False

return True

print(isPrime(5))
print(isPrime(31))
print(isPrime(1433))
print(isPrime(8763857775536878331))



32 changes: 32 additions & 0 deletions src/main/python/algorithms/math/LCM.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'''
* @author (original JAVA) William Fiset, [email protected]
* (conversion to Python) Hiruna Vishwamith, [email protected]
* @date 11 Nov 2022


An implementation of finding the LCM of two numbers. Time Complexity ~O(log(a + b))

'''


def gcd(a, b):
if a < 0:
a = -a
if b == 0:
return a
else:
return gcd(b, a % b)


def lcm(a, b):
lcm = (a//gcd(a, b))*b
if lcm > 0:
return lcm
else:
return -lcm


print(lcm(12, 18)) # 36
print(lcm(-12, 18)) # 36
print(lcm(12, -18)) # 36
print(lcm(-12, -18)) # 36
44 changes: 44 additions & 0 deletions src/main/python/algorithms/math/ModInv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'''
* @author (original JAVA) William Fiset, [email protected]
* (conversion to Python) Hiruna Vishwamith, [email protected]
* @date 11 Nov 2022


An implementation of the modPow(a, n, mod) operation.
Time Complexity O(lg(n))

'''

import math


# This function performs the extended euclidean algorithm on two numbers a and b.
# The function returns the gcd(a,b) as well as the numbers x and y such
# that ax + by = gcd(a,b). This calculation is important in number theory
# and can be used for several things such as finding modular inverses and
# solutions to linear Diophantine equations.


def egcd(a, b):
if b == 0:
return (a, 1, 0)
else:
g, y, x = egcd(b, a % b)
return (g, x, y - (a // b) * x)


# Returns the modular inverse of 'a' mod 'm'
# Make sure m > 0 and 'a' & 'm' are relatively prime.
def modInv(a, m):

if m < 0:
raise Exception(' mod must be > 0')
a = ((a % m)+m) % m
g, x, y = egcd(a, m)
if g != 1:
return None
return ((x % m)+m) % m


print(modInv(2, 5)) # 2*3 mod 5=1
print(modInv(4, 18)) # 4*x mod 18=1
87 changes: 87 additions & 0 deletions src/main/python/algorithms/math/ModPow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
'''
* @author (original JAVA) William Fiset, [email protected]
* (conversion to Python) Hiruna Vishwamith, [email protected]
* @date 11 Nov 2022


An implementation of the modPow(a, n, mod) operation.
Time Complexity O(lg(n))

'''

import math


# Computes the Greatest Common Divisor (GCD) of a & b
def gcd(a, b):
if a < 0:
a = -a
if b == 0:
return a
else:
return gcd(b, a % b)

# This function performs the extended euclidean algorithm on two numbers a and b.
# The function returns the gcd(a,b) as well as the numbers x and y such
# that ax + by = gcd(a,b). This calculation is important in number theory
# and can be used for several things such as finding modular inverses and
# solutions to linear Diophantine equations.


def egcd(a, b):
if a < 0:
a = -a
if b == 0:
return (b, 1, 0)
else:
g, y, x = egcd(b, a % b)
return (g, x, y - (a // b) * x)


# Returns the modular inverse of 'a' mod 'm'
# Make sure m > 0 and 'a' & 'm' are relatively prime.
def modInv(a, m):

a = ((a % m)+m) % m
g, x, y = egcd(a, m)
return ((x % m)+m) % m

# Computes a^n modulo mod very efficiently in O(lg(n)) time.
# This function supports negative exponent values and a negative
# base, however the modulus must be positive.


def modPow(a, n, mod):
if mod < 0:
raise Exception('mod must be positive')

# To handle negative exponents we can use the modular
# inverse of a to our advantage since: a^-n mod m = (a^-1)^n mod m

if n < 0:
if gcd(a, mod) != 1:
raise Exception("If n < 0 then must have gcd(a, mod) = 1")
return modPow(modInv(a, mod), -n, mod)
if n == 0:
return 1
p = a
r = 1
i = 0
while n != 0:
mask = 1 << i
if n & mask == mask:
r = (((r * p) % mod) + mod) % mod
n -= mask
p = ((p * p) % mod + mod) % mod
i+=1
return ((r % mod) + mod) % mod


# 3 ^ 4 mod 1000000

r1 = modPow(3, 4, 10**6) #81
r2 = modPow(-45, 12345, 987654321) #323182557
r3 = modPow(6, -66, 101) #84
r4 = modPow(-5, -7, 1009) #675

print(r1,r2,r3,r4)