Skip to content

Commit

Permalink
Test maths (#220)
Browse files Browse the repository at this point in the history
* added test suite for base_conversion.py

* added test suite for extended_gcd.py

* improved the code of the gcd-function

* added test suite for gcd.py

* fixed a bug in the function strobogrammaticInRange(...)

* removed the print

* added test suite for generate_strobogrammatic.py

* added variables for the lengths

* added test suite for is_strobogrammatic.py

* renamed the function find_next_square(...)

* added test suite for the file next_perfect_square.py

* test for the file primes_sieve_of_eratosthenes.py

* test for the file prime_test.py

* test for the file pythagoras.py

* test for the file rabin_miller.py

* added a docstring

* fixed my assert

* added test for the file rsa.py

* added random.seed() in genprime(k)

* fixed rsa encryption

* fixed rsa.py
  • Loading branch information
christianbender authored and goswami-rahul committed Apr 13, 2018
1 parent 2f0aad7 commit 541a3fb
Show file tree
Hide file tree
Showing 10 changed files with 242 additions and 55 deletions.
2 changes: 1 addition & 1 deletion maths/extended_gcd.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ def extended_gcd(a,b):
old_s, s = s, old_s - quotient * s
old_t, t = t, old_t - quotient * t

return old_s, old_t, old_r
return old_s, old_t, old_r
5 changes: 2 additions & 3 deletions maths/gcd.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ def gcd(a, b):
"""Computes the greatest common divisor of integers a and b using
Euclid's Algorithm.
"""
while True:
if b == 0:
return a
while b != 0:
a, b = b, a % b
return a


def lcm(a, b):
Expand Down
13 changes: 7 additions & 6 deletions maths/generate_strobogrammtic.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ def gen_strobogrammatic(n):
:type n: int
:rtype: List[str]
"""
result = helper(n, n)
return result
return helper(n, n)


def helper(n, length):
Expand Down Expand Up @@ -43,19 +42,21 @@ def strobogrammaticInRange(low, high):
"""
res = []
count = 0
for i in range(len(low), len(high)+1):
low_len = len(low)
high_len = len(high)
for i in range(low_len, high_len + 1):
res.extend(helper2(i, i))
for perm in res:
if len(perm) == len(low) and int(perm) < int(low):
if len(perm) == low_len and int(perm) < int(low):
continue
elif len(perm) == len(high) and int(perm) > int(high):
elif len(perm) == high_len and int(perm) > int(high):
continue
else:
count += 1
return count


def helper2(self, n, length):
def helper2(n, length):
if n == 0:
return [""]
if n == 1:
Expand Down
3 changes: 2 additions & 1 deletion maths/next_perfect_square.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ def find_next_square(sq):

# Another way:

def find_next_square(sq):
def find_next_square2(sq):
x = sq**0.5
return -1 if x % 1 else (x+1)**2

29 changes: 1 addition & 28 deletions maths/prime_test.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""
prime_test(n) returns a True if n is a prime number else it returns False
"""
import unittest

def prime_test(n):
if n <= 1:
Expand Down Expand Up @@ -34,30 +33,4 @@ def prime_test2(n):
# if input number is less than
# or equal to 1, it is not prime
else:
return False


class TestSuite (unittest.TestCase):
def test_prime_test(self):
"""
checks all prime numbers between 2 up to 100.
Between 2 up to 100 exists 25 prime numbers!
"""
counter = 0
for i in range(2,101):
if prime_test(i):
counter += 1
self.assertEqual(25,counter)
def test_prime_test2(self):
"""
checks all prime numbers between 2 up to 100.
Between 2 up to 100 exists 25 prime numbers!
"""
counter = 0
for i in range(2,101):
if prime_test(i):
counter += 1
self.assertEqual(25,counter)

if __name__ == "__main__":
unittest.main()
return False
2 changes: 1 addition & 1 deletion maths/primes_sieve_of_eratosthenes.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def primes(x):
primes = [] # List of Primes
if x >= 2:
primes.append(2) # Add 2 by default
for i in range(0, sieve_size):
for i in range(sieve_size):
if sieve[i] == 1:
value_at_i = i*2 + 3
primes.append(value_at_i)
Expand Down
2 changes: 1 addition & 1 deletion maths/pythagoras.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ def pythagoras(opposite,adjacent,hypotenuse):
else:
return "You already know the answer!"
except:
print ("Error, check your input. You must know 2 of the 3 variables.")
raise ValueError("invalid argument were given.")
13 changes: 11 additions & 2 deletions maths/rabin_miller.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,22 @@ def pow2_factor(n):
power += 1
return power, n

def pow_3(a, b, c):
"""
helper function for a, b and c integers.
"""
return a**b % c

"""
Rabin-Miller primality test
returning False implies that n is guarenteed composite
returning True means that n is probably prime
with a 4 ** -k chance of being wrong
"""
def is_prime(n, k):
#precondition
assert n >= 5, "the to tested number must been >= 5"

r, d = pow2_factor(n - 1)

"""
Expand All @@ -23,13 +32,13 @@ def is_prime(n, k):
an invalid witness guarentees n is composite
"""
def valid_witness(a):
x = pow(a, d, n)
x = pow(int(a), int(d), int(n))

if x == 1 or x == n - 1:
return False

for _ in range(r - 1):
x = pow(x, 2, n)
x = pow(int(x), int(2), int(n))

if x == 1:
return True
Expand Down
35 changes: 23 additions & 12 deletions maths/rsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,22 @@
generate a prime with k bits
"""
def genprime(k):
random.seed()
while True:
n = random.randrange(2 ** (k - 1),2 ** k)
n = random.randrange(int(2 ** (k - 1)),int(2 ** k))
if is_prime(n,128):
return n


"""
calculate the inverse of a mod m
that is, find b such that (a * b) % m == 1
"""
def modinv(a, m):
x, y, g = extended_gcd(a,m)
return x % m
b = 1
while ((a*b) % m != 1):
b += 1
return b

"""
the RSA key generating algorithm
Expand All @@ -68,13 +72,20 @@ def generate_key(k):
l = (p - 1) * (q - 1) # calculate totient function
d = modinv(e,l)

return n, e, d
return int(n), int(e), int(d)

def encrypt(data, e, n):
return pow(int(data), int(e), int(n))

def decrypt(data, d, n):
return pow(int(data), int(d), int(n))



#sample usage:
# n,e,d = generate_key(16)
# data = 20
# encrypted = pow(data,e,n)
# decrypted = pow(encrypted,d,n)
# assert decrypted == data

"""
sample usage:
n,e,d = generate_key(1024)
data = 1337
encrypted = pow(data,e,n)
decrypted = pow(encrypted,d,n)
assert decrypted == data
"""
Loading

0 comments on commit 541a3fb

Please sign in to comment.