Lets review some of what we've studied, while also introducing some new stuff.
import operator
print(dir(operator))
['__abs__', '__add__', '__all__', '__and__', '__builtins__', '__cached__', '__concat__', '__contains__', '__delitem__', '__doc__', '__eq__', '__file__', '__floordiv__', '__ge__', '__getitem__', '__gt__', '__iadd__', '__iand__', '__iconcat__', '__ifloordiv__', '__ilshift__', '__imatmul__', '__imod__', '__imul__', '__index__', '__inv__', '__invert__', '__ior__', '__ipow__', '__irshift__', '__isub__', '__itruediv__', '__ixor__', '__le__', '__loader__', '__lshift__', '__lt__', '__matmul__', '__mod__', '__mul__', '__name__', '__ne__', '__neg__', '__not__', '__or__', '__package__', '__pos__', '__pow__', '__rshift__', '__setitem__', '__spec__', '__sub__', '__truediv__', '__xor__', '_abs', 'abs', 'add', 'and_', 'attrgetter', 'concat', 'contains', 'countOf', 'delitem', 'eq', 'floordiv', 'ge', 'getitem', 'gt', 'iadd', 'iand', 'iconcat', 'ifloordiv', 'ilshift', 'imatmul', 'imod', 'imul', 'index', 'indexOf', 'inv', 'invert', 'ior', 'ipow', 'irshift', 'is_', 'is_not', 'isub', 'itemgetter', 'itruediv', 'ixor', 'le', 'length_hint', 'lshift', 'lt', 'matmul', 'methodcaller', 'mod', 'mul', 'ne', 'neg', 'not_', 'or_', 'pos', 'pow', 'rshift', 'setitem', 'sub', 'truediv', 'truth', 'xor']
from operator import mul, add
mul(3, 2)
6
3 * 2
6
mul(52938745927349857293, 2934859238485984)
155367767558744989035099915780681312
from functools import reduce
try:
assert 23**560 % 561 == 1 # perfectly OK
except TypeError:
print("That's a type error")
except AssertionError:
print("I have a problem with that.")
raise # where you catch an exception, and re-raise it
else:
print("Everything went smoothly captain")
finally:
print("Close a file, log a message -- even if still exceptions")
print("We only get this far if any exceptions were handled")
Everything went smoothly captain Close a file, log a message -- even if still exceptions We only get this far if any exceptions were handled
from primes import factors
factors(561)
(1, 3, 11, 17)
reduce(add, (4, 3, 2, 1))
10
def factorial(n):
if n==0:
return 1
return reduce(mul, range(1, n+1))
factorial(0)
1
import primes
dir(primes)
['PrimeNumbers', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', 'all_factors', 'eratosthenes', 'euler_test', 'factors', 'invmod', 'isprime', 'primes_gen', 'primesplay', 'xgcd']
print(primes.eratosthenes(1000))
Eliminating multiples of: 2 Eliminating multiples of: 3 Eliminating multiples of: 5 Eliminating multiples of: 7 Eliminating multiples of: 11 Eliminating multiples of: 13 Eliminating multiples of: 17 Eliminating multiples of: 19 Eliminating multiples of: 23 Eliminating multiples of: 29 Eliminating multiples of: 31 [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]
Lets check out some other exhibits regarding the high school math topics at hand.
class Mod:
_modulus = 12
def __init__(self, n):
self.value = n % self._modulus
def __eq__(self, other):
return self.value == other.value % self._modulus
def __repr__(self):
return "({} mod {})".format(self.value, self._modulus)
def __add__(self, other):
return type(self)(self.value + other.value)
def __mul__(self, other):
return type(self)(self.value * other.value)
def __pow__(self, n):
return type(self)(pow(self.value, n, self._modulus))
(3 * 4) % 10
2
m1 = Mod(1); m2 = Mod(2); m3 = Mod(3); m4 = Mod(4)
m3
(3 mod 12)
m3.__mul__(m4)
(0 mod 12)
m3 * m3 * m3
(3 mod 12)
m4 * m3 * m3 * m2
(0 mod 12)
(4 * 3 * 2 * 1) % 12
0
from math import gcd
def totient(n):
totatives = [x for x in range(1, n) if gcd(x, n)==1]
return len(totatives), totatives
totient(12)
(4, [1, 5, 7, 11])
Getting totatives of 12 as Mod type numbers:
m1 = Mod(1); m5 = Mod(5); m7 = Mod(7); m11 = Mod(11)
m1 * m7
(7 mod 12)
m11 * m1
(11 mod 12)
m5 * m5 # m5 is its own inverse
(1 mod 12)
m7 * m11 * m11
(7 mod 12)
m11 * m11 # m11 is its own inverse
(1 mod 12)
m5 * m5
(1 mod 12)
m7 * m7 # m7 is its own inverse
(1 mod 12)