{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "FGV - Escola de Matemática Aplicada\n", "==\n", "Curso de Verão - Introdução à Programação com a Linguagem Python\n", "--" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Project Euler:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Resolução do problema 34:" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Enunciado:\n", "\n", "145 é um numero curioso pois 1! + 4! + 5! = 1 + 24 + 120 = 145.\n", "\n", "Encontre a soma de todos os números que são iguais a soma dos fatoriais de seus dígitos.\n", "\n", "Nota: como 1! = 1 e 2! = 2 não são somas, eles não são incluídos.\n", "\n", "https://projecteuler.net/index.php?section=problems&id=34" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Resolução:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import time" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def fatorial(n):\n", " fat = 1\n", " for i in range(1,n+1):\n", " fat *= i\n", " return fat" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "40320\n" ] } ], "source": [ "x = fatorial(8)\n", "print(x)" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{0: 1, 1: 1, 2: 2, 3: 6, 4: 24, 5: 120, 6: 720, 7: 5040, 8: 40320, 9: 362880}" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fatoriais = {num:fatorial(num) for num in range(10)}\n", "fatoriais" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [], "source": [ "def soma_fat(n):\n", " soma = 0\n", " for num in str(n):\n", " #soma += fatorial(int(num))\n", " soma += fatoriais[int(num)]\n", " return soma" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "145\n" ] } ], "source": [ "y = soma_fat(145)\n", "print(y)" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[145, 40585]\n", "29.7142000198\n" ] } ], "source": [ "t0 = time.time()\n", "l = []\n", "for numero in range(3,2540161):\n", " if soma_fat(numero) == numero:\n", " l.append(numero)\n", "print(l)\n", "tempo = time.time() - t0\n", "print(tempo)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "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.7.6" } }, "nbformat": 4, "nbformat_minor": 1 }