add problem 90
This commit is contained in:
parent
9bdbcb91f0
commit
57dc69699c
|
@ -0,0 +1,142 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "24453968",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# [Cube Digit Pairs](https://projecteuler.net/problem=90)\n",
|
||||
"\n",
|
||||
"There's only ${10 \\choose 6} = 210$ distinct cubes, making this problem easy to brute force. We first make a generator for those cubes."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"id": "06b0661f",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from itertools import combinations\n",
|
||||
"\n",
|
||||
"def cubes():\n",
|
||||
" yield from combinations(range(0, 10), 6)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "87ba05f8",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"We also write a simple function for handling the fact that we can flip 6s and 9s."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"id": "15dab5dd",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def extended_set(s):\n",
|
||||
" s = set(s)\n",
|
||||
" if 6 in s:\n",
|
||||
" s.add(9)\n",
|
||||
" elif 9 in s:\n",
|
||||
" s.add(6)\n",
|
||||
" \n",
|
||||
" return s"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "42f875d0",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Another simple function tests if we can display all the square numbers with a given pair of cubes."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"id": "7396b4e8",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def all_squares_displayable(s, t):\n",
|
||||
" es = extended_set(s)\n",
|
||||
" et = extended_set(t)\n",
|
||||
" \n",
|
||||
" square_digits = ((0, 1), (0, 4), (0, 9), (1, 6), (2, 5), (3, 6), (4, 9), (6, 4), (8, 1))\n",
|
||||
" for (m, n) in square_digits:\n",
|
||||
" if not ((m in es and n in et) or (n in es and m in et)):\n",
|
||||
" return False\n",
|
||||
" \n",
|
||||
" return True"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "9efa8055",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Then we just check all the cube pairs."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"id": "2d11c86f",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"arrangements = set()\n",
|
||||
"for s, t in combinations(cubes(), 2):\n",
|
||||
" if all_squares_displayable(s, t):\n",
|
||||
" arrangements.add((s, t))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"id": "8ce1fab4",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"1217"
|
||||
]
|
||||
},
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"len(arrangements)"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "SageMath 9.5",
|
||||
"language": "sage",
|
||||
"name": "sagemath"
|
||||
},
|
||||
"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.11.2"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 5
|
||||
}
|
Loading…
Reference in New Issue