{ "cells": [ { "cell_type": "markdown", "id": "4d757b3c", "metadata": {}, "source": [ "# July 21, 23 2021\n", "\n", "Lets review some of what we've studied, while also introducing some new stuff." ] }, { "cell_type": "code", "execution_count": 1, "id": "eaaf6d5c", "metadata": {}, "outputs": [], "source": [ "import operator" ] }, { "cell_type": "code", "execution_count": 2, "id": "43ce906d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['__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']\n" ] } ], "source": [ "print(dir(operator))" ] }, { "cell_type": "code", "execution_count": 3, "id": "d81f6825", "metadata": {}, "outputs": [], "source": [ "from operator import mul, add" ] }, { "cell_type": "code", "execution_count": 4, "id": "685ca9e6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mul(3, 2)" ] }, { "cell_type": "code", "execution_count": 5, "id": "232e869f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 * 2" ] }, { "cell_type": "code", "execution_count": 6, "id": "08ef5d99", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "155367767558744989035099915780681312" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "mul(52938745927349857293, 2934859238485984)" ] }, { "cell_type": "code", "execution_count": 7, "id": "a8dcbbd0", "metadata": {}, "outputs": [], "source": [ "from functools import reduce" ] }, { "cell_type": "code", "execution_count": 8, "id": "8c4154b5", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Everything went smoothly captain\n", "Close a file, log a message -- even if still exceptions\n", "We only get this far if any exceptions were handled\n" ] } ], "source": [ "try: \n", " assert 23**560 % 561 == 1 # perfectly OK\n", "except TypeError:\n", " print(\"That's a type error\")\n", "except AssertionError:\n", " print(\"I have a problem with that.\")\n", " raise # where you catch an exception, and re-raise it\n", "else:\n", " print(\"Everything went smoothly captain\")\n", "finally:\n", " print(\"Close a file, log a message -- even if still exceptions\")\n", " \n", "print(\"We only get this far if any exceptions were handled\")" ] }, { "cell_type": "code", "execution_count": 9, "id": "b718d427", "metadata": {}, "outputs": [], "source": [ "from primes import factors" ] }, { "cell_type": "code", "execution_count": 10, "id": "c69c4f1f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1, 3, 11, 17)" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "factors(561)" ] }, { "cell_type": "code", "execution_count": 11, "id": "da2ade15", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "reduce(add, (4, 3, 2, 1))" ] }, { "cell_type": "code", "execution_count": 12, "id": "eca4cdb7", "metadata": {}, "outputs": [], "source": [ "def factorial(n):\n", " if n==0:\n", " return 1\n", " return reduce(mul, range(1, n+1))" ] }, { "cell_type": "code", "execution_count": 13, "id": "ca44af8c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "factorial(0)" ] }, { "cell_type": "code", "execution_count": 14, "id": "52de186a", "metadata": {}, "outputs": [], "source": [ "import primes" ] }, { "cell_type": "code", "execution_count": 15, "id": "361a9f25", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['PrimeNumbers',\n", " '__builtins__',\n", " '__cached__',\n", " '__doc__',\n", " '__file__',\n", " '__loader__',\n", " '__name__',\n", " '__package__',\n", " '__path__',\n", " '__spec__',\n", " 'all_factors',\n", " 'eratosthenes',\n", " 'euler_test',\n", " 'factors',\n", " 'invmod',\n", " 'isprime',\n", " 'primes_gen',\n", " 'primesplay',\n", " 'xgcd']" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dir(primes)" ] }, { "cell_type": "code", "execution_count": 16, "id": "2470c2c0", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Eliminating multiples of: 2\n", "Eliminating multiples of: 3\n", "Eliminating multiples of: 5\n", "Eliminating multiples of: 7\n", "Eliminating multiples of: 11\n", "Eliminating multiples of: 13\n", "Eliminating multiples of: 17\n", "Eliminating multiples of: 19\n", "Eliminating multiples of: 23\n", "Eliminating multiples of: 29\n", "Eliminating multiples of: 31\n", "[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]\n" ] } ], "source": [ "print(primes.eratosthenes(1000))" ] }, { "cell_type": "markdown", "id": "133b884d", "metadata": {}, "source": [ "## A Tour with Stops\n", "\n", "Lets check out some other exhibits regarding the high school math topics at hand.\n", "\n", "* [Number Theory](https://nbviewer.jupyter.org/github/4dsolutions/School_of_Tomorrow/blob/master/NumberTheory.ipynb) (School of Tomorrow)\n", "* [Group Theory](https://github.com/4dsolutions/School_of_Tomorrow/blob/master/GroupTheory.ipynb) (School of Tomorrow)\n", "* [Numeracy Series](http://4dsolutions.net/ocn/numeracy0.html) (Oregon Curriculum Network)\n", "* [Tabular Python](TabularPython.ipynb) (this school)\n", "* [Trigonometry](Trig.ipynb) (this school)" ] }, { "cell_type": "code", "execution_count": 17, "id": "9911b2b2", "metadata": {}, "outputs": [], "source": [ "class Mod:\n", " \n", " _modulus = 12\n", " \n", " def __init__(self, n):\n", " self.value = n % self._modulus\n", " \n", " def __eq__(self, other):\n", " return self.value == other.value % self._modulus\n", " \n", " def __repr__(self):\n", " return \"({} mod {})\".format(self.value, self._modulus)\n", " \n", " def __add__(self, other):\n", " return type(self)(self.value + other.value)\n", " \n", " def __mul__(self, other):\n", " return type(self)(self.value * other.value)\n", " \n", " def __pow__(self, n):\n", " return type(self)(pow(self.value, n, self._modulus))" ] }, { "cell_type": "code", "execution_count": 18, "id": "c42f34a1", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(3 * 4) % 10" ] }, { "cell_type": "code", "execution_count": 19, "id": "9c620323", "metadata": {}, "outputs": [], "source": [ "m1 = Mod(1); m2 = Mod(2); m3 = Mod(3); m4 = Mod(4)" ] }, { "cell_type": "code", "execution_count": 20, "id": "bd4e0a2f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(3 mod 12)" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m3" ] }, { "cell_type": "code", "execution_count": 21, "id": "b7a4fab0", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(0 mod 12)" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m3.__mul__(m4)" ] }, { "cell_type": "code", "execution_count": 22, "id": "9c56f84a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(3 mod 12)" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m3 * m3 * m3" ] }, { "cell_type": "code", "execution_count": 23, "id": "fe6c6fff", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(0 mod 12)" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m4 * m3 * m3 * m2" ] }, { "cell_type": "code", "execution_count": 24, "id": "b0339c4c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "(4 * 3 * 2 * 1) % 12" ] }, { "cell_type": "markdown", "id": "790fe0d1", "metadata": {}, "source": [ "## Introducing Group Theory by Multiplying Totatives of N Modulo N" ] }, { "cell_type": "code", "execution_count": 25, "id": "1940a544", "metadata": {}, "outputs": [], "source": [ "from math import gcd" ] }, { "cell_type": "code", "execution_count": 26, "id": "52dcd75f", "metadata": {}, "outputs": [], "source": [ "def totient(n):\n", " totatives = [x for x in range(1, n) if gcd(x, n)==1]\n", " return len(totatives), totatives" ] }, { "cell_type": "code", "execution_count": 27, "id": "acd94d63", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(4, [1, 5, 7, 11])" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "totient(12)" ] }, { "cell_type": "markdown", "id": "51a8a21a", "metadata": {}, "source": [ "Getting totatives of 12 as Mod type numbers:" ] }, { "cell_type": "code", "execution_count": 28, "id": "7588bf2a", "metadata": {}, "outputs": [], "source": [ "m1 = Mod(1); m5 = Mod(5); m7 = Mod(7); m11 = Mod(11)" ] }, { "cell_type": "markdown", "id": "e512d7ae", "metadata": {}, "source": [ "#### Showing Closure:" ] }, { "cell_type": "code", "execution_count": 29, "id": "62ac6d46", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(7 mod 12)" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m1 * m7" ] }, { "cell_type": "code", "execution_count": 30, "id": "de7dc437", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(11 mod 12)" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m11 * m1" ] }, { "cell_type": "markdown", "id": "77284311", "metadata": {}, "source": [ "#### Showing inverses:" ] }, { "cell_type": "code", "execution_count": 31, "id": "b3157102", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1 mod 12)" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m5 * m5 # m5 is its own inverse" ] }, { "cell_type": "code", "execution_count": 32, "id": "d3b05292", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(7 mod 12)" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m7 * m11 * m11 " ] }, { "cell_type": "code", "execution_count": 33, "id": "4c3603ac", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1 mod 12)" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m11 * m11 # m11 is its own inverse" ] }, { "cell_type": "code", "execution_count": 34, "id": "0b8f3f0f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1 mod 12)" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m5 * m5" ] }, { "cell_type": "code", "execution_count": 35, "id": "e607758b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1 mod 12)" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m7 * m7 # m7 is its own inverse" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.9" } }, "nbformat": 4, "nbformat_minor": 5 }