From c92aff27da5f75a7ef21d17b73bfd6a007cabe45 Mon Sep 17 00:00:00 2001 From: filifa Date: Mon, 14 Apr 2025 22:30:45 -0400 Subject: [PATCH] add problem 30 --- problem0030.ipynb | 75 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 problem0030.ipynb diff --git a/problem0030.ipynb b/problem0030.ipynb new file mode 100644 index 0000000..486bba2 --- /dev/null +++ b/problem0030.ipynb @@ -0,0 +1,75 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "cbbd0d3f", + "metadata": {}, + "source": [ + "# [Digit Fifth Powers](https://projecteuler.net/problem=30)\n", + "\n", + "These sorts of numbers are called [perfect digital invariants](https://en.wikipedia.org/wiki/Perfect_digital_invariant) (that page talks about the concept for arbitrary bases, but we obviously only need to worry about base 10).\n", + "\n", + "It's not difficult to implement the PDI function from the page linked above, and to check numbers one by one to see if they are a PDI, but how do we know when to stop searching? Well, let $f(n)$ be the fifth power PDI function, and consider the largest possible six digit number, 999999. The sum of the fifth powers of the digits is $f(999999) = 6 \\times 9^5 = 354294$. If we replaced any digits in 999999, they would have to be smaller than 9, so the power digit sum would be smaller than 354294. Therefore, for any $n \\leq 999999$, $f(n) \\leq 354294$, so for $n$ to *equal* $f(n)$, $n$ must be less than or equal to 354294, as well.\n", + "\n", + "(There's a formal proof on the page linked above that shows the upper bound can be reduced even further, but I think the logic is harder to follow than what's presented here, and using this bound solves the problem quickly enough.)" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "e8ab0179", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "443839\n" + ] + } + ], + "source": [ + "def F(n, p):\n", + " total = 0\n", + " while n != 0:\n", + " total += (n % 10) ** p\n", + " n //= 10\n", + " \n", + " return total\n", + "\n", + "\n", + "print(sum(n for n in range(2, 354295) if n == F(n, 5)))" + ] + }, + { + "cell_type": "markdown", + "id": "f0754e9f", + "metadata": {}, + "source": [ + "## Relevant sequences\n", + "* Perfect digital invariants: [A252648](https://oeis.org/A252648)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "SageMath 9.5", + "language": "sage", + "name": "sagemath" + }, + "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.11.2" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}