Started to try 35.

main
Felix Martin 2018-02-12 22:29:38 +01:00
parent ce2544de27
commit 1a890cf71b
1 changed files with 169 additions and 0 deletions

View File

@ -0,0 +1,169 @@
{
"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": 23,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"78498\n"
]
}
],
"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": 24,
"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": 25,
"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": 26,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"s = [p for p in ps if is_circular(p)]"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, 97, 113, 131, 199, 311, 337, 373, 733, 919, 991]\n"
]
}
],
"source": [
"print(s)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"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"
}
},
"nbformat": 4,
"nbformat_minor": 0
}