Home
About
Resume
Projects
Links
Blog
Download notebook
{ "cells": [ { "cell_type": "markdown", "id": "a469358e-2f57-4bd6-9c8d-c59f3d77920b", "metadata": {}, "source": [ "### Q35\n", "How many circular primes are there below one million?" ] }, { "cell_type": "code", "execution_count": 17, "id": "255771bb-8696-4c1c-aeb2-f56c3bc62237", "metadata": {}, "outputs": [], "source": [ "def prime_numbers(n, max_len = False):\n", " result = []\n", " if max_len:\n", " n = max_len**2//5000 + max_len*20\n", " sieve = [True] * (n+1)\n", " for p in range(2, n+1):\n", " if (sieve[p]):\n", " result.append(p)\n", " if max_len and len(result) >= max_len:\n", " return result\n", " for i in range(p, n+1, p):\n", " sieve[i] = False\n", " return result\n", "\n", "def circular_primes_under_n(n):\n", " primes_list = set(prime_numbers(n))\n", " drop_prime = set()\n", " for p in primes_list:\n", " str_p = str(p)\n", " if len(str_p) >=2 and any(str(i) in str_p for i in [2,4,5,6,8,0]):\n", " drop_prime.add(p)\n", " for p in primes_list:\n", " if p in drop_prime:\n", " continue\n", " str_p = str(p)\n", " if int(str_p[1:]+str_p[:1]) not in primes_list :\n", " for i in range(0,len(str_p)):\n", " drop_prime.add(int(str_p[i:]+str_p[:i]))\n", " return primes_list.difference(drop_prime)" ] }, { "cell_type": "code", "execution_count": 19, "id": "63599c30-aa22-42ed-9680-d7cd9c0f1f39", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 169 ms, sys: 0 ns, total: 169 ms\n", "Wall time: 166 ms\n" ] }, { "data": { "text/plain": [ "55" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "len(circular_primes_under_n(1000000))" ] }, { "cell_type": "code", "execution_count": null, "id": "3a6349fa-0f4f-47b0-873f-ef7fc06ac49e", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.8.10" } }, "nbformat": 4, "nbformat_minor": 5 }
Previous Notebook:
Project Euler Q34
Next Notebook:
Project Euler Q36
Loading