Home
About
Resume
Projects
Links
Blog
Download notebook
{ "cells": [ { "cell_type": "markdown", "id": "a469358e-2f57-4bd6-9c8d-c59f3d77920b", "metadata": {}, "source": [ "### Q58\n", "If one complete new layer is wrapped around the spiral above, a square spiral with side length 9 will be formed. If this process is continued, what is the side length of the square spiral for which the ratio of primes along both diagonals first falls below 10%?" ] }, { "cell_type": "code", "execution_count": 1, "id": "bd7ddc2f-5964-49c3-a927-774399b0b0a9", "metadata": {}, "outputs": [], "source": [ "def is_prime(n):\n", " for i in range(2,int(n**0.5)+1):\n", " if (n%i) == 0:\n", " return False\n", " return True\n", "\n", "def spiral_primes_ratio_under_r(r):\n", " n = 0\n", " last_num = 1\n", " num_count = 1\n", " prime_count = 0\n", " while n < 1 or prime_count / num_count > r:\n", " n += 1\n", " for o in range(4):\n", " last_num += 2*n\n", " num_count += 1\n", " prime_count += 1 if is_prime(last_num) else 0\n", " return n" ] }, { "cell_type": "code", "execution_count": 2, "id": "3e78fa42-856e-4722-a279-245d9b8516a5", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 3.12 s, sys: 14 ms, total: 3.13 s\n", "Wall time: 3.13 s\n" ] }, { "data": { "text/plain": [ "13120" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "spiral_primes_ratio_under_r(0.1)" ] }, { "cell_type": "code", "execution_count": null, "id": "8a62cf42-e5e5-45bd-b96e-ea90ead6e378", "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 Q57
Next Notebook:
Project Euler Q59
Loading