{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Find a positive number n such that the digits in n * n and n * n * n are all unique and also include all 10 digits." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "8100" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "90*90" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "729000" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "90*90*90" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9801" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "99*99" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "970299" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "99*99*99" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10000" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "100*100" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1000000" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "100*100*100" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "since the answer should be n such that the combined number of digits in n * n and n * n * n should not be more than 10, we see that we can stop checking at 99!" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "69\n" ] } ], "source": [ "for n in range(1,100):\n", " alldigits = []\n", " n2digits = str(n * n)\n", " for d in n2digits:\n", " alldigits.append(int(d))\n", " n3digits = str(n * n * n)\n", " for d in n3digits:\n", " alldigits.append(int(d))\n", " if len(set(alldigits)) == 10:\n", " print(n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "we can make the program more efficient by starting at a higher number than 1" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1600" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "40*40" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "64000" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "40*40*40" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2500" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "50*50" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "125000" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "50*50*50" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2304" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "48*48" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "110592" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "48*48*48" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2209" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "47*47" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "103823" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "47*47*47" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2116" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "46*46" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "97336" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "46*46*46" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "69\n" ] } ], "source": [ "for n in range(47,100):\n", " alldigits = []\n", " n2digits = str(n * n)\n", " for d in n2digits:\n", " alldigits.append(int(d))\n", " n3digits = str(n * n * n)\n", " for d in n3digits:\n", " alldigits.append(int(d))\n", " if len(set(alldigits)) == 10:\n", " print(n)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.8.16" } }, "nbformat": 4, "nbformat_minor": 4 }