{ "cells": [ { "cell_type": "markdown", "id": "b380a067", "metadata": {}, "source": [ "[Home](Home.ipynb)\n", "\n", "# Logarithms\n", "\n", "I got [this question](https://qr.ae/pG4Lso) on Quora the other day, asking how to distill a number into powers of 3, never using the same power more than once. \n", "\n", "For example we would like to start with the biggest power of 3 that goes into it, and then treat the remainder to similar analysis, using ever smaller powers of 3.\n", "\n", "The question turns out to be asking how to convert a number to base 3. We need a set of terms, each the product of a coefficent and a base (3) to a highest degree (power), descending to zero." ] }, { "cell_type": "code", "execution_count": 1, "id": "619b2f11", "metadata": {}, "outputs": [], "source": [ "from math import log, floor" ] }, { "cell_type": "code", "execution_count": 2, "id": "69c52391", "metadata": {}, "outputs": [], "source": [ "def distill(n, b=10):\n", " \"\"\"\n", " Assume the n starts in base 10, \n", " where b is the target base\n", " \"\"\" \n", " terms = [] \n", " while n > 0: \n", " # b to what power gets closest to n without going over?\n", " degree = floor(log(n, b)) # rounding down to nearest int\n", " q, r = divmod(n, b**degree) \n", " terms.append((q, degree)) # q * b ** degree term\n", " n = r # what's still left to handle?\n", " return terms " ] }, { "cell_type": "code", "execution_count": 3, "id": "af1dfa74", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[(1, 8), (1, 7), (1, 6), (2, 5), (1, 3), (1, 2), (1, 0)]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "distill(10000, 3)" ] }, { "cell_type": "code", "execution_count": 4, "id": "0deafd2e", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[(1, 3), (1, 1)]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "distill(10, 2)" ] }, { "cell_type": "markdown", "id": "a4b0d93d", "metadata": {}, "source": [ "$$\n", "(3^{8}) + (3^{7}) + (3^{6}) + 2(3^{5}) + (3^{3}) + (3^{2}) + (3^{0})\n", "$$\n" ] }, { "cell_type": "code", "execution_count": 5, "id": "f6914e3a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[(3, 2), (14, 1), (8, 0)]" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "distill(1000, 16)" ] }, { "cell_type": "markdown", "id": "a15201f0", "metadata": {}, "source": [ "$$\n", "3(3^{2}) + 14(3^{1}) + 8(3^{0})\n", "$$" ] }, { "cell_type": "code", "execution_count": 6, "id": "5d4fe2f4", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[(1, 4), (1, 3), (2, 0)]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "distill(110, 3)" ] }, { "cell_type": "markdown", "id": "73d11aff", "metadata": {}, "source": [ "$$\n", "(3^{4}) + (3^{3}) + 2(3^{0})\n", "$$" ] }, { "cell_type": "code", "execution_count": 7, "id": "e5d056d4", "metadata": {}, "outputs": [], "source": [ "def base10(tuples, base=10):\n", " \"\"\"\n", " base is the base used for the tuples, \n", " return number in base 10\n", " \"\"\"\n", " total = 0\n", " for t in tuples:\n", " coeff, degree = t\n", " total = total + coeff * base ** degree\n", " return total" ] }, { "cell_type": "code", "execution_count": 8, "id": "4883dbc4", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[(1, 3), (1, 1)]" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tups = distill(10, 2)\n", "tups" ] }, { "cell_type": "markdown", "id": "3f4c2944", "metadata": {}, "source": [ "$$\n", "(2^{3}) + (2^{1})\n", "$$" ] }, { "cell_type": "code", "execution_count": 9, "id": "df8a3672", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "base10(tups, base=2)" ] }, { "cell_type": "code", "execution_count": 10, "id": "d6e38c6f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[(2, 3), (7, 2), (1, 1)]" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tups = distill(10000, 16)\n", "tups" ] }, { "cell_type": "markdown", "id": "1d627861", "metadata": {}, "source": [ "$$\n", "2(16^{3}) + 7(16^{2}) + 16^{1}\n", "$$" ] }, { "cell_type": "code", "execution_count": 11, "id": "c7a5810c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10000" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "base10(tups, 16)" ] } ], "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 }