Home
About
Resume
Projects
Links
Blog
Download notebook
{ "cells": [ { "cell_type": "markdown", "id": "86e06153", "metadata": {}, "source": [ "### Q26\n", "Find the value of d < 1000 for which \\( \\frac{1}{d} \\) contains the longest recurring cycle in its decimal fraction part." ] }, { "cell_type": "code", "execution_count": 1, "id": "16dac2f4-3864-43fc-a8a0-e99049bf2d12", "metadata": {}, "outputs": [], "source": [ "def recurring_len(n):\n", " if int('9'*(n-1))%n != 0:\n", " return 0\n", " else:\n", " recurring_nums = str(int('9'*(n-1))//n)\n", " return (n-1)//recurring_nums.count(recurring_nums[:len(str(n))+1])\n", "\n", "def longest_recurring_cycle(max_range):\n", " for n in range(1,max_range)[::-1]:\n", " if recurring_len(n) == n-1:\n", " return n\n", " " ] }, { "cell_type": "code", "execution_count": 2, "id": "73650e3b-dd45-44bf-b084-8e76364a9233", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 130 µs, sys: 37 µs, total: 167 µs\n", "Wall time: 169 µs\n" ] }, { "data": { "text/plain": [ "983" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "longest_recurring_cycle(1000)" ] }, { "cell_type": "code", "execution_count": null, "id": "bb16c22a-fd2e-47a5-9815-293e6f84569a", "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 Q25
Next Notebook:
Project Euler Q27
Loading