From 1a890cf71b25559821c1825d2153b3ad8f0762c4 Mon Sep 17 00:00:00 2001 From: Felix Martin Date: Mon, 12 Feb 2018 22:29:38 +0100 Subject: [PATCH] Started to try 35. --- ipython/EulerProblem035.ipynb | 169 ++++++++++++++++++++++++++++++++++ 1 file changed, 169 insertions(+) create mode 100644 ipython/EulerProblem035.ipynb diff --git a/ipython/EulerProblem035.ipynb b/ipython/EulerProblem035.ipynb new file mode 100644 index 0000000..2759c22 --- /dev/null +++ b/ipython/EulerProblem035.ipynb @@ -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 +}