euler/ipython/EulerProblem053.ipynb

119 lines
2.5 KiB
Plaintext
Raw Normal View History

2018-12-24 16:57:03 +01:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Combinatoric selections (Euler Problem 53)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"[https://projecteuler.net/problem=53](https://projecteuler.net/problem=53)\n",
"\n",
"There are exactly ten ways of selecting three from five, 12345:\n",
"\n",
"$123, 124, 125, 134, 135, 145, 234, 235, 245, 345$\n",
"\n",
"In combinatorics, we use the notation, 5C3 = 10.\n",
"\n",
"In general,\n",
"\n",
"$nCr = \\frac{n!}{r!(nr)!}$, where r ≤ n, n! = n×(n1)×...×3×2×1, and 0! = 1.\n",
"\n",
"It is not until n = 23, that a value exceeds one-million: 23C10 = 1144066.\n",
"\n",
"How many, not necessarily distinct, values of nCr, for 1 ≤ n ≤ 100, are greater than one-million?"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import math\n",
"\n",
"def combinations_naiv(n, r):\n",
" return math.factorial(n) // (math.factorial(r) * math.factorial(n - r))\n",
"\n",
"assert(combinations_naiv(5, 3) == 10)\n",
"assert(combinations_naiv(23, 10) == 1144066)\n",
"assert(combinations_naiv(20, 20) == 1)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"count = 0\n",
"\n",
"for n in range (1, 101):\n",
" for r in range(1, n + 1):\n",
" if combinations_naiv(n, r) > 10**6:\n",
" count += 1\n",
"\n",
"s = count"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4075\n"
]
}
],
"source": [
"print(s)\n",
"assert(s == 4075)"
]
},
2018-12-24 16:57:03 +01:00
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"completion_date": "",
"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": []
},
"nbformat": 4,
"nbformat_minor": 2
}