euler/ipython/EulerProblem035.ipynb

192 lines
4.3 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Euler Problem 35\n",
"\n",
"The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime.\n",
"\n",
"There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.\n",
"\n",
"How many circular primes are there below one million?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First we get all primes into a look-up table. Then we iterate them and check whether they are circular."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def get_primes_smaller(number):\n",
" primes = []\n",
" prospects = [n for n in range(2, number)]\n",
" while prospects:\n",
" p = prospects[0]\n",
" prospects = [x for x in prospects if x % p != 0]\n",
" primes.append(p)\n",
" if p * p > number:\n",
" return primes + prospects\n",
" return primes\n",
"\n",
"ps = get_primes_smaller(1000000)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def get_combinations(xs):\n",
" if not xs:\n",
" return []\n",
" rs = []\n",
" for i in range(len(xs)):\n",
" yss = get_combinations(xs[:i] + xs[i + 1:])\n",
" if not yss:\n",
" rs.append(xs[i])\n",
" for ys in yss:\n",
" rs.append(xs[i] + ys)\n",
" return rs\n",
"\n",
"assert(get_combinations(\"ab\") == [\"ab\", \"ba\"])"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from itertools import permutations\n",
"prime_set = set(ps)\n",
"\n",
"def is_circular(p):\n",
" cs = permutations(str(p))\n",
" for c in cs:\n",
" if not int(\"\".join(c)) in prime_set:\n",
" return False\n",
" return True\n",
"\n",
"assert(is_circular(\"2\") == True)\n",
"assert(is_circular(\"11\") == True)\n",
"assert(is_circular(\"47\") == False)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"False solution 22\n"
]
}
],
"source": [
"s = len([p for p in ps if is_circular(p)])\n",
"print(\"False solution {}\".format(s))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We did not read the problem properly. Cycles are obviously not the same as permutations. If we change that we should get the solution in no time."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def cyles(xs):\n",
" if len(xs) <= 1:\n",
" return xs\n",
" return [xs[i:] + xs[:i] for i in range(len(xs))]\n",
"\n",
"prime_set = set(ps)\n",
"\n",
"def is_circular(p):\n",
" cs = cyles(str(p))\n",
" for c in cs:\n",
" if not int(\"\".join(c)) in prime_set:\n",
" return False\n",
" return True"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"55"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s = len([p for p in ps if is_circular(p)])\n",
"s"
]
}
],
"metadata": {
"completion_date": "Tue, 13 Feb 2018, 09:14",
"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": [
"circular",
"combinations"
]
},
"nbformat": 4,
"nbformat_minor": 0
}