{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Powerful digit sum (Euler Problem 56)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "A googol ($10^{100}$) is a massive number: one followed by one-hundred zeros; $100^{100}$ is almost unimaginably large: one followed by two-hundred zeros. Despite their size, the sum of the digits in each number is only 1.\n", "\n", "Considering natural numbers of the form, $a^b$, where $a, b < 100$, what is the maximum digital sum?" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def get_digit_sum(n):\n", " s = 0\n", " while n != 0:\n", " s += (n % 10)\n", " n //= 10\n", " return s\n", "\n", "assert(get_digit_sum(1337) == 14)\n", "assert(get_digit_sum(100**100) == 1)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "972\n" ] } ], "source": [ "s = max([get_digit_sum(a**b) for a in range(1, 100) for b in range(1, 100)])\n", "print(s)\n", "assert(s == 972)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "completion_date": "Mon, 24 Dec 2018, 23:33", "kernelspec": { "display_name": "Python 3", "language": "python3.6", "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.6.5" }, "tags": [ "power", "brute force" ] }, "nbformat": 4, "nbformat_minor": 2 }