Home
About
Resume
Projects
Links
Blog
Download notebook
{ "cells": [ { "cell_type": "markdown", "id": "66d733b5-6c26-42bb-aa80-be0e8ee712f1", "metadata": {}, "source": [ "### Q10\n", "Find the sum of all the primes below two million." ] }, { "cell_type": "code", "execution_count": 1, "id": "0451d10b-8c81-4c74-a507-aef29ebcfd44", "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" ] }, { "cell_type": "code", "execution_count": 2, "id": "86e6abe9-bd26-4429-9a3b-fa64c128ecb4", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 177 ms, sys: 11.2 ms, total: 188 ms\n", "Wall time: 185 ms\n" ] }, { "data": { "text/plain": [ "142913828922" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "sum(prime_numbers(2000000))" ] }, { "cell_type": "code", "execution_count": null, "id": "4ecc82cf-380d-47e7-a978-c5de24585719", "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 Q9
Next Notebook:
Project Euler Q11
Loading