Home
About
Resume
Projects
Links
Blog
Download notebook
{ "cells": [ { "cell_type": "markdown", "id": "bb05b256-c012-495f-9f5d-9eb92185ecf7", "metadata": {}, "source": [ "### Q7\n", "What is the 10001-st prime number?" ] }, { "cell_type": "code", "execution_count": 1, "id": "23728572-4630-4c7e-82b9-d86521f79760", "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 n_prime(n):\n", " return prime_numbers(0,n)[-1]" ] }, { "cell_type": "code", "execution_count": 2, "id": "2d886e46-0ac4-4243-92b8-6f399ab96c83", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 97.8 ms, sys: 77.7 ms, total: 175 ms\n", "Wall time: 175 ms\n" ] }, { "data": { "text/plain": [ "104743" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "n_prime(10001)" ] }, { "cell_type": "code", "execution_count": null, "id": "f952be3f-e3c3-4028-9245-62f46b2b967b", "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 Q6
Next Notebook:
Project Euler Q8
Loading