euler/ipython/EulerProblem020.ipynb

88 lines
1.7 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Euler Problem 20\n",
"\n",
"n! means n × (n 1) × ... × 3 × 2 × 1\n",
"\n",
"For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800,\n",
"and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.\n",
"\n",
"Find the sum of the digits in the number 100!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Write our own factorial implementation and get the solution."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def factorial(n):\n",
" assert(n > 0)\n",
" if n == 1:\n",
" return 1\n",
" else:\n",
" return n * factorial(n - 1)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"648\n"
]
}
],
"source": [
"f100 = factorial(100)\n",
"print(sum(map(int, str(f100))))\n",
"assert(sum(map(int, str(f100))) == 648)"
]
}
],
"metadata": {
"completion_date": "Fri, 5 Sep 2014, 10:42",
"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.5.4"
},
"tags": [
"factorial"
]
},
"nbformat": 4,
"nbformat_minor": 0
}