euler/ipython/EulerProblem052.ipynb

148 lines
3.4 KiB
Plaintext
Raw Normal View History

2018-12-24 03:54:15 +01:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Permuted multiples (Euler Problem 52)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"[https://projecteuler.net/problem=52](https://projecteuler.net/problem=52)\n",
"\n",
"It can be seen that the number, 125874, and its double, 251748, contain exactly the same digits, but in a different order.\n",
"\n",
"Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x, contain the same digits."
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [],
"source": [
"from functools import reduce\n",
"from operator import eq\n",
"\n",
"def get_length(n):\n",
" \"\"\" Returns the number of digits for n.\n",
" Returns wrong result for n = 0. \"\"\"\n",
" length = 0\n",
" while n:\n",
" length += 1\n",
" n //= 10\n",
" return length\n",
"\n",
"assert(get_length(100) == 3)\n",
"\n",
"def get_digits(n):\n",
" d = []\n",
" while n:\n",
" d.append(n % 10)\n",
" n //= 10\n",
" return d\n",
"\n",
"assert(get_digits(213) == [3, 1, 2])\n",
"\n",
"def get_digits_count(n):\n",
" digits_count = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]\n",
" for d in get_digits(n):\n",
" digits_count[d] += 1\n",
" return digits_count\n",
"\n",
"assert(get_digits_count(1258744) == [0, 1, 1, 0, 2, 1, 0, 1, 1, 0])"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {},
"outputs": [],
"source": [
"def check_if_multiples_contain_same_digits(n, max_multiple=2):\n",
" n_digits_count = get_digits_count(n)\n",
" n_length = get_length(n)\n",
" n_multiple = n * max_multiple\n",
" while n != n_multiple:\n",
" if n_length != get_length(n_multiple) or n_digits_count != get_digits_count(n_multiple):\n",
" return False\n",
" n_multiple -= n\n",
" return True\n",
"\n",
"assert(check_if_multiples_contain_same_digits(125874))\n",
"assert(check_if_multiples_contain_same_digits(125875) == False)"
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {},
"outputs": [],
"source": [
"for i in range(1, 10000000):\n",
" if check_if_multiples_contain_same_digits(i, 6):\n",
" s = i\n",
" break"
]
},
{
"cell_type": "code",
"execution_count": 72,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"142857\n"
]
}
],
"source": [
"print(s)\n",
"assert(s == 142857)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"completion_date": "Mon, 24 Dec 2018, 02:32",
"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": [
"digits",
"permutations"
]
},
"nbformat": 4,
"nbformat_minor": 2
}