{ "cells": [ { "cell_type": "markdown", "id": "161f1d9b-7001-4478-bbb8-ce1fd8043591", "metadata": {}, "source": [ "##### Python for High School (Summer 2022)\n", "\n", "* [Table of Contents](PY4HS.ipynb)\n", "* \"Open\n", "* [![nbviewer](https://raw.githubusercontent.com/jupyter/design/master/logos/Badges/nbviewer_badge.svg)](https://nbviewer.org/github/4dsolutions/elite_school/blob/master/Py4HS_sphere_packing_classroom.ipynb)\n", "\n", "### Sphere Packing\n", "\n", "* [Generating the FCC](https://github.com/4dsolutions/Python5/blob/master/Generating%20the%20FCC.ipynb)\n", "* [Crystal Ball Sequence](https://github.com/4dsolutions/Python5/blob/master/STEM%20Mathematics.ipynb)\n", "* [OEIS A005901](https://oeis.org/A005901)\n", "\n", "![](http://www.4dsolutions.net/ocn/graphics/cubanim.gif)\n", "
\n",
    " 1 -- center ball\n",
    "12 -- layer 1\n",
    "42 -- layer 2\n",
    "92 -- layer 3\n",
    "...\n",
    "
" ] }, { "cell_type": "code", "execution_count": 41, "id": "c081ad1b-0721-4369-ab5f-c56d2610813a", "metadata": {}, "outputs": [], "source": [ "def a005901(layer : int) -> int:\n", " \"\"\"\n", " https://oeis.org/A005901\n", " layer >= 0\n", " \"\"\"\n", " if int(layer) < 0:\n", " raise ValueError\n", " if not (int(layer) == float(layer)):\n", " raise ValueError\n", " \n", " if layer == 0:\n", " return 1\n", " return 10 * layer ** 2 + 2" ] }, { "cell_type": "code", "execution_count": 37, "id": "609a2d74-16a3-40d7-a177-026482891d39", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 12, 42, 92, 162, 252, 362, 492, 642, 812, 1002, 1212, 1442, 1692, 1962, 2252, 2562, 2892, 3242, 3612, 4002, 4412, 4842, 5292, 5762, 6252, 6762, 7292, 7842, 8412]\n" ] } ], "source": [ "print([a005901(x) for x in range(30)])" ] }, { "cell_type": "code", "execution_count": 16, "id": "c33ed68d-9898-4be0-a18f-b7e06b576cff", "metadata": {}, "outputs": [], "source": [ "def accum_a005901(layer):\n", " \"\"\"\n", " A005902 cumulative version of A005901\n", " \"\"\"\n", " total = 0\n", " for x in range(layer + 1):\n", " total = total + a005901(x)\n", " return total" ] }, { "cell_type": "markdown", "id": "77cc6f74-56cf-4c42-b01a-dc8db611cd35", "metadata": {}, "source": [ "Upon reading the notes for [A005902](https://oeis.org/A005902) we find there's a more direct way to compute the cumulative number of balls i.e. we do not have to sum the balls in each layer out to some target layer. Enter the target layer $L$ directly into this formula:\n", "\n", "$$\n", "S = \\frac{(2L + 1)(5L^{2} + 5L + 3)}{3}\n", "$$" ] }, { "cell_type": "code", "execution_count": 43, "id": "39f8127b-e480-4cde-8e42-0c678706fda7", "metadata": {}, "outputs": [], "source": [ "import sympy as sp" ] }, { "cell_type": "code", "execution_count": 48, "id": "e838d9d0-cd20-4037-a27a-2d41ab1986b3", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\frac{\\left(2 L + 1\\right) \\left(5 L^{2} + 5 L + 3\\right)}{3}$" ], "text/plain": [ "(2*L + 1)*(5*L**2 + 5*L + 3)/3" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L = sp.Symbol('L')\n", "expr = (2*L + 1)*(5*L**2 + 5*L + 3)/3\n", "expr" ] }, { "cell_type": "code", "execution_count": 55, "id": "90e1cee6-54c0-4402-873f-1945efd1bc30", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\displaystyle \\frac{10 L^{3}}{3} + 5 L^{2} + \\frac{11 L}{3} + 1$" ], "text/plain": [ "10*L**3/3 + 5*L**2 + 11*L/3 + 1" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "formula = expr.expand()\n", "formula" ] }, { "cell_type": "code", "execution_count": 57, "id": "a6464325-f5ac-4eae-a480-6f1f0de240ae", "metadata": {}, "outputs": [], "source": [ "def short_cut(layer):\n", " if layer == 0:\n", " return 1\n", " return ((2 * layer + 1) * (5 * layer**2 + 5 * layer + 3)) // 3 # see below" ] }, { "cell_type": "code", "execution_count": 61, "id": "7fdd39c5-1013-4e7a-b759-45ba35d3522c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3871" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "short_cut(10)" ] }, { "cell_type": "code", "execution_count": 38, "id": "50cf7bb2-9958-435c-ad6b-195af3467d07", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 13, 55, 147, 309, 561, 923, 1415, 2057, 2869, 3871, 5083, 6525, 8217, 10179, 12431, 14993, 17885, 21127, 24739, 28741, 33153, 37995, 43287, 49049, 55301, 62063, 69355, 77197, 85609]\n" ] } ], "source": [ "print([short_cut(x) for x in range(30)])" ] }, { "cell_type": "code", "execution_count": 63, "id": "ef798a5c-acc1-440c-b034-73b8ddc7deee", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3871" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(formula.evalf(subs={L:10}))" ] }, { "cell_type": "code", "execution_count": 56, "id": "a24fecc2-5c61-4db1-9c5b-fd21210dbd0b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 13, 55, 147, 309, 561, 923, 1415, 2057, 2869, 3871, 5083, 6525, 8217, 10179, 12431, 14993, 17885, 21127, 24739, 28741, 33153, 37995, 43287, 49049, 55301, 62063, 69355, 77197, 85609]\n" ] } ], "source": [ "print([int(expr.evalf(subs={L:x})) for x in range(30)])" ] }, { "cell_type": "markdown", "id": "91958f11-5f72-4429-abcd-7dc81002597d", "metadata": {}, "source": [ "### Remember how divsion works: / versus //" ] }, { "cell_type": "code", "execution_count": 70, "id": "7150b7c6-7664-4436-8079-6df7d54c9ebc", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.0" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "6 // 2.1" ] }, { "cell_type": "code", "execution_count": 40, "id": "3fe97b3e-617f-4a2b-baf1-a3462fabc53a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "6 // 2" ] }, { "cell_type": "code", "execution_count": 65, "id": "690b4cbb-91e8-49f8-b19a-c182282dd2a7", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "6 // 5" ] }, { "cell_type": "code", "execution_count": null, "id": "39fcc1fa-50f6-4afa-aba2-35f9b2d1382d", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.12" } }, "nbformat": 4, "nbformat_minor": 5 }