Home
About
Resume
Projects
Links
Blog
Download notebook
{ "cells": [ { "cell_type": "markdown", "id": "a469358e-2f57-4bd6-9c8d-c59f3d77920b", "metadata": {}, "source": [ "### Q34\n", "Find the sum of all numbers which are equal to the sum of the factorial of their digits." ] }, { "cell_type": "code", "execution_count": 1, "id": "255771bb-8696-4c1c-aeb2-f56c3bc62237", "metadata": {}, "outputs": [], "source": [ "def factorial(n):\n", " return n*factorial(n-1) if n > 1 else 1\n", "\n", "def is_digit_factorial(x,factorial_map= { i:factorial(i) for i in range(0,10) }):\n", " if x == sum(factorial_map[int(v)] for v in str(x)):\n", " return True\n", " return False\n", "\n", "def sum_digit_factorial(max_len=5*9*8*7*6*5*4*3*2*1):\n", " result = 0\n", " for i in range(10,max_len+1):\n", " if is_digit_factorial(i):\n", " result+=i \n", " return result" ] }, { "cell_type": "code", "execution_count": 2, "id": "63599c30-aa22-42ed-9680-d7cd9c0f1f39", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 1.34 s, sys: 2.88 ms, total: 1.34 s\n", "Wall time: 1.34 s\n" ] }, { "data": { "text/plain": [ "40730" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "sum_digit_factorial()" ] }, { "cell_type": "code", "execution_count": null, "id": "e55a5bc9-fbe9-4deb-adfa-e81c6f90d40f", "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.8.10" } }, "nbformat": 4, "nbformat_minor": 5 }
Previous Notebook:
Project Euler Q33
Next Notebook:
Project Euler Q35
Loading