Commits

meisterluk committed 761c33f

Rename basics.py to utils.py

  • Participants
  • Parent commits fe532af

Comments (0)

Files changed (8)

basics.py

-#!/usr/bin/env python
-
-'''
-    $Id: basics.py 1234 2009-06-10 04:28:51Z meisterluk $
-    Author: Lukas Prokop <admin@lukas-prokop.at>
-    Copyright: Emailware
-'''
-
-import random
-from euklid import euclidean as ggt
-
-def eulerian_function(num):
-    '''Eulerian function (phi)
-
-    Calculates the number of coprimes with num smaller
-    than num. eg.
-      phi(8) = |{1,3,5,7}| = 4
-    '''
-    # if num is prime number: return num - 1
-    if num == 1:
-        return 1
-    teiler = 0
-    for value in xrange(1, num):
-        if ggt(value, num) == 1:
-            teiler += 1
-    return teiler
-
-def satisfy_equation(e, eulerian):
-    '''Satisfies an equation'''
-    d = random.randint(2, e)
-    try:
-        while pow(d, e, eulerian) != 1:
-            d += 1
-    except KeyboardInterrupt:
-        print d
-        d = 1
-    return d
-
-def dez_bin(arg):
-    ''' Convert decimal numbers to binary numbers '''
-    arg = int(arg)
-    base = 2
-    bin = ''
-    while arg:
-        remainder = arg % base
-        bin += str(remainder)
-        arg = (arg - remainder) / base
-    return bin[::-1]
-
-def bin_dec(arg):
-    '''Convert binary numbers to decimal numbers'''
-    dec = 0
-    exponent = 0
-    for bin in list(arg).__reversed__():
-        if bin == '1':
-            dec += 2 ** exponent
-        elif bin == '0':
-            pass
-        else:
-            raise ValueError, 'binary alphabet is {0,1}'
-        exponent += 1
-    return dec
-
-def divisible(a, b):
-    '''Is a divisible with b? (True or False)'''
-    if b == 0:
-        return False
-    if (a / b) * b == a:
-        return True
-    else:
-        return False
-
-def find_coprime(num):
-    '''Find coprime (e) for number
-
-    satisfies...
-      1 < e < phi(number)
-    and 
-      ggt(i, number) == 1
-    '''
-    rand = random.random()
-    if rand > 0.5 and prime_number_test(num):
-        return random.choice(range(2, num))
-    coprime = num
-    while ggt(coprime, num) != 1:
-        rand = random.randint(1, num)
-        if coprime < 1:
-            coprime += rand
-        if coprime > (num - 1):
-            coprime -= rand
-        if num > 5:
-            coprime += 3
-        else:
-            coprime -= 1
-        if coprime < 1 or coprime > (num - 1):
-            coprime = num
-    return coprime
-
-def prime_number_test(num):
-    '''Is num a prime number?'''
-    if num % 2 == 0:
-        return False
-    for i in xrange(3, num, 2):
-        if ggt(num, i) != 1:
-            return False
-    return True
-
-def digit_length(x):
-    '''Returns the number of digits of a number
-
-    eg. 456 => 3
-    '''
-    return len(str(x))
-
-def prefix(x):
-    '''Adds a prefix to number
-    
-     x => 0x
-     5 => 05
-    xy => xy
-    20 => 20
-    '''
-    if x < 10:
-        return ' ' + str(x) + ')'
-    else:
-        return str(x) + ')'
-
-def arithmetics(a, b, mode=0):
-    '''A simple and stupid multiplication'''
-    try:
-        if isinstance(a, str):
-            a = int(a)
-        if isinstance(b, str):
-            b = int(b)
-    except ValueError:
-        return False
-
-    if mode == 0:
-        return a + b
-    elif mode == 1:
-        return a - b
-    elif mode == 2:
-        return a * b
-    elif mode == 3:
-        if min(a, b) == 0:
-            print 'Division by Zero'
-            return False
-        else:
-            return max(a, b) / min(a, b)
-    elif mode == 4:
-        return max(a, b) - min(a, b)
-    elif mode == 5:
-        if b == 0:
-            print 'Division by Zero'
-            return False
-        else:
-            return a % b
-    else:
-        return False
-
-bin_dez = bin_dec
-dec_bin = dez_bin
-
-if __name__ == '__main__':
-    num = 0
-    while num == 0:
-        try:
-            num = raw_input('Give me a number: ')
-            num = int(num)
-        except ValueError:
-            num = 0
-
-    binary = dez_bin(num)
-    print 'The binary representation of this number is %s' % binary
-    print 'The decimal representation of the binary number is %d again' % bin_dec(binary)
-    print 'The number has %d coprime(s) smaller %d' % (eulerian_function(num), num)
-    if prime_number_test(num):
-        print 'This number is a prime number'
-    else:
-        print 'This number is not a prime number'
-    if divisible(num, 5):
-        print 'This number is divisible by 5'
-    else:
-        print 'This number is not divisible by 5'

calc_hvaduhlqpdo.py

 
 import string
 import caesar
-from basics import prefix
+from utils import prefix
 
 def run():
     print 'I will only calculate with all lowercase characters'
 __name__bu = __name__
 __name__ = 'interface'
 
-import basics
+import utils
 import errors
 
 import calc_120
                         (['limit', long],)],
                 'b' : ['Trial Division', 'trial_division.trial_division(%d, True)', \
                         (['number', int],)],
-                'c' : ['Eulerian function', 'basics.eulerian_function(%d)', \
+                'c' : ['Eulerian function', 'utils.eulerian_function(%d)', \
                         (['number', int],)],
-                'd' : ['Is divisible?', 'basics.divisible(%d, %d)', \
+                'd' : ['Is divisible?', 'utils.divisible(%d, %d)', \
                         (['divisible', int], ['by', int])],
                 'e' : ['Modern Euklidean algorithm', 'euklid.euclidean_modern(%d, %d)', \
                         (['a', int], ['b', int])],
                         (['a', int], ['^b', int], ['mod c', int])],
                 'y' : ['MD5 Brute Force', 'md5_brute_force.run()'],
                 'z' : ['Find coprime of number with 1 < coprime < number', \
-                    'basics.find_coprime(%d)', (['number', int],)],
+                    'utils.find_coprime(%d)', (['number', int],)],
                 'A' : ['Print all 26 Caesar possibilities', 'calc_hvaduhlqpdo.run()'],
-                'B' : ['Get binary representation of decimal number', 'basics.dez_bin(%d)', \
+                'B' : ['Get binary representation of decimal number', 'utils.dez_bin(%d)', \
                     (['decimal number', int],)],
-                'C' : ['Get decimal representation of binary number', 'basics.bin_dez(%d)', \
+                'C' : ['Get decimal representation of binary number', 'utils.bin_dez(%d)', \
                     (['binary number', int],)],
-                'D' : ['Prime number test', 'basics.prime_number_test(%d)', \
+                'D' : ['Prime number test', 'utils.prime_number_test(%d)', \
                     (['number to check', int],)],
-                'E' : ['A simple addition', 'basics.arithmetics(%d, %d, 0)', \
+                'E' : ['A simple addition', 'utils.arithmetics(%d, %d, 0)', \
                     (['a', int], ['+b', int])],
-                'F' : ['A simple substraction', 'basics.arithmetics(%d, %d, 1)', \
+                'F' : ['A simple substraction', 'utils.arithmetics(%d, %d, 1)', \
                     (['a', int], ['-b', int])],
-                'G' : ['A simple multiplication', 'basics.arithmetics(%d, %d, 2)', \
+                'G' : ['A simple multiplication', 'utils.arithmetics(%d, %d, 2)', \
                     (['a', int], ['*b', int])],
-                'H' : ['A division (divisor is smaller argument)', 'basics.arithmetics(%d, %d, 3)', \
+                'H' : ['A division (divisor is smaller argument)', 'utils.arithmetics(%d, %d, 3)', \
                     (['a', int], ['/b', int])],
-                'I' : ['A substraction of smaller argument', 'basics.arithmetics(%d, %d, 4)', \
+                'I' : ['A substraction of smaller argument', 'utils.arithmetics(%d, %d, 4)', \
                     (['a', int], ['-b', int])],
-                'J' : ['Calculate a mod b', 'basics.arithmetics(%d, %d, 5)', \
+                'J' : ['Calculate a mod b', 'utils.arithmetics(%d, %d, 5)', \
                     (['a', int], ['%b', int])],
                 'R' : ['RSA', 'self.call_cryptosystem(rsa.RSA)']
         }
     Copyright: GPL
 '''
 
-from basics import find_coprime as coprime
+from utils import find_coprime as coprime
 from euklid import euclidean as ggt
 from euklid import extended_euclidean as ext
 from sieve import sieve_of_eratosthenes as sieve
 #!/usr/bin/env python
 
-from basics import dez_bin
+from utils import dez_bin
 
 def square(base, c):
     times = 0
 #!/usr/bin/env python
 
-import basics
+import utils
 
 a = 123
 c = 84
-b = basics.dez_bin(c)
+b = utils.dez_bin(c)
 m = 13
 
 rem = 1

trial_division.py

 
 import math
 import sieve
-from basics import divisible
+from utils import divisible
 
 def trial_division(num, complete=False):
     primes = sieve.sieve_of_eratosthenes(num)
+#!/usr/bin/env python
+
+'''
+    $Id: utils.py 1234 2013-03-31 01:44:52Z meisterluk $
+    Author: Lukas Prokop <admin@lukas-prokop.at>
+    Copyright: Emailware
+'''
+
+import random
+from euklid import euclidean as ggt
+
+def eulerian_function(num):
+    '''Eulerian function (phi)
+
+    Calculates the number of coprimes with num smaller
+    than num. eg.
+      phi(8) = |{1,3,5,7}| = 4
+    '''
+    # if num is prime number: return num - 1
+    if num == 1:
+        return 1
+    teiler = 0
+    for value in xrange(1, num):
+        if ggt(value, num) == 1:
+            teiler += 1
+    return teiler
+
+def satisfy_equation(e, eulerian):
+    '''Satisfies an equation'''
+    d = random.randint(2, e)
+    try:
+        while pow(d, e, eulerian) != 1:
+            d += 1
+    except KeyboardInterrupt:
+        print d
+        d = 1
+    return d
+
+def dez_bin(arg):
+    ''' Convert decimal numbers to binary numbers '''
+    arg = int(arg)
+    base = 2
+    bin = ''
+    while arg:
+        remainder = arg % base
+        bin += str(remainder)
+        arg = (arg - remainder) / base
+    return bin[::-1]
+
+def bin_dec(arg):
+    '''Convert binary numbers to decimal numbers'''
+    dec = 0
+    exponent = 0
+    for bin in list(arg).__reversed__():
+        if bin == '1':
+            dec += 2 ** exponent
+        elif bin == '0':
+            pass
+        else:
+            raise ValueError, 'binary alphabet is {0,1}'
+        exponent += 1
+    return dec
+
+def divisible(a, b):
+    '''Is a divisible with b? (True or False)'''
+    if b == 0:
+        return False
+    if (a / b) * b == a:
+        return True
+    else:
+        return False
+
+def find_coprime(num):
+    '''Find coprime (e) for number
+
+    satisfies...
+      1 < e < phi(number)
+    and 
+      ggt(i, number) == 1
+    '''
+    rand = random.random()
+    if rand > 0.5 and prime_number_test(num):
+        return random.choice(range(2, num))
+    coprime = num
+    while ggt(coprime, num) != 1:
+        rand = random.randint(1, num)
+        if coprime < 1:
+            coprime += rand
+        if coprime > (num - 1):
+            coprime -= rand
+        if num > 5:
+            coprime += 3
+        else:
+            coprime -= 1
+        if coprime < 1 or coprime > (num - 1):
+            coprime = num
+    return coprime
+
+def prime_number_test(num):
+    '''Is num a prime number?'''
+    if num % 2 == 0:
+        return False
+    for i in xrange(3, num, 2):
+        if ggt(num, i) != 1:
+            return False
+    return True
+
+def digit_length(x):
+    '''Returns the number of digits of a number
+
+    eg. 456 => 3
+    '''
+    return len(str(x))
+
+def prefix(x):
+    '''Adds a prefix to number
+    
+     x => 0x
+     5 => 05
+    xy => xy
+    20 => 20
+    '''
+    if x < 10:
+        return ' ' + str(x) + ')'
+    else:
+        return str(x) + ')'
+
+def arithmetics(a, b, mode=0):
+    '''A simple and stupid multiplication'''
+    try:
+        if isinstance(a, str):
+            a = int(a)
+        if isinstance(b, str):
+            b = int(b)
+    except ValueError:
+        return False
+
+    if mode == 0:
+        return a + b
+    elif mode == 1:
+        return a - b
+    elif mode == 2:
+        return a * b
+    elif mode == 3:
+        if min(a, b) == 0:
+            print 'Division by Zero'
+            return False
+        else:
+            return max(a, b) / min(a, b)
+    elif mode == 4:
+        return max(a, b) - min(a, b)
+    elif mode == 5:
+        if b == 0:
+            print 'Division by Zero'
+            return False
+        else:
+            return a % b
+    else:
+        return False
+
+bin_dez = bin_dec
+dec_bin = dez_bin
+
+if __name__ == '__main__':
+    num = 0
+    while num == 0:
+        try:
+            num = raw_input('Give me a number: ')
+            num = int(num)
+        except ValueError:
+            num = 0
+
+    binary = dez_bin(num)
+    print 'The binary representation of this number is %s' % binary
+    print 'The decimal representation of the binary number is %d again' % bin_dec(binary)
+    print 'The number has %d coprime(s) smaller %d' % (eulerian_function(num), num)
+    if prime_number_test(num):
+        print 'This number is a prime number'
+    else:
+        print 'This number is not a prime number'
+    if divisible(num, 5):
+        print 'This number is divisible by 5'
+    else:
+        print 'This number is not divisible by 5'