Home
About
Resume
Projects
Links
Blog
Download notebook
{ "cells": [ { "cell_type": "markdown", "id": "a469358e-2f57-4bd6-9c8d-c59f3d77920b", "metadata": {}, "source": [ "### Q47\n", "Find the first four consecutive integers to have four distinct prime factors. What is the first of these numbers?" ] }, { "cell_type": "code", "execution_count": 1, "id": "820c6f59-d5d1-4f67-a379-b41d8bb337ac", "metadata": {}, "outputs": [], "source": [ "def prime_factors(n):\n", " i = 2\n", " factors = set()\n", " while i * i <= n:\n", " if n % i:\n", " i += 1\n", " else:\n", " n //= i\n", " factors.add(i)\n", " if n > 1:\n", " factors.add(n)\n", " return factors\n", "\n", "def distinct_prime_factors(n):\n", " if n == 1:\n", " return 2\n", " i = 2\n", " while True:\n", " for j in range(n)[::-1]:\n", " if len(prime_factors(i+j)) != n:\n", " i += 1+j\n", " break\n", " else:\n", " return i" ] }, { "cell_type": "code", "execution_count": 2, "id": "af630329-9130-41b7-9eae-dbff1852f068", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 177 ms, sys: 0 ns, total: 177 ms\n", "Wall time: 174 ms\n" ] }, { "data": { "text/plain": [ "134043" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "distinct_prime_factors(4)" ] }, { "cell_type": "code", "execution_count": null, "id": "a85727b7-2d4f-42d1-8281-d8cea5d894b2", "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 Q46
Next Notebook:
Project Euler Q48
Loading