Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test maths #220

Merged
merged 22 commits into from
Apr 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
ff85f72
added test suite for base_conversion.py
christianbender Apr 1, 2018
0c29dc6
added test suite for extended_gcd.py
christianbender Apr 1, 2018
979dbb9
improved the code of the gcd-function
christianbender Apr 1, 2018
0f23231
added test suite for gcd.py
christianbender Apr 1, 2018
593c950
fixed a bug in the function strobogrammaticInRange(...)
christianbender Apr 1, 2018
67174c8
removed the print
christianbender Apr 1, 2018
5f7e5f5
added test suite for generate_strobogrammatic.py
christianbender Apr 1, 2018
6062940
added variables for the lengths
christianbender Apr 1, 2018
e13da58
added test suite for is_strobogrammatic.py
christianbender Apr 1, 2018
4eb35fb
renamed the function find_next_square(...)
christianbender Apr 1, 2018
710cfcb
added test suite for the file next_perfect_square.py
christianbender Apr 1, 2018
2475b23
test for the file primes_sieve_of_eratosthenes.py
christianbender Apr 12, 2018
39b69ba
test for the file prime_test.py
christianbender Apr 12, 2018
4538607
test for the file pythagoras.py
christianbender Apr 12, 2018
da4635c
test for the file rabin_miller.py
christianbender Apr 12, 2018
beb0254
added a docstring
christianbender Apr 12, 2018
6a65463
fixed my assert
christianbender Apr 12, 2018
0a129a3
added test for the file rsa.py
christianbender Apr 12, 2018
a7cdb03
added random.seed() in genprime(k)
christianbender Apr 12, 2018
382804f
fixed rsa encryption
christianbender Apr 12, 2018
d558795
fixed rsa.py
christianbender Apr 12, 2018
5e40931
fixed rsa.py
christianbender Apr 12, 2018
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
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