{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def cycle_length(d):\n", " n = 1\n", " numerators = []\n", "\n", " while True:\n", " n *= 10\n", " try:\n", " cycle_start = numerators.index(n)\n", " return len(numerators[cycle_start:])\n", " except ValueError:\n", " numerators.append(n)\n", " n = n % d\n", " if n == 0:\n", " return 0\n", "\n", "longest_length = 0\n", "longest_d = None\n", "\n", "for d in range(2, 1000):\n", " l = cycle_length(d)\n", " if l > longest_length:\n", " longest_length = l\n", " longest_d = d" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.7.4" }, "title": "Reciprocal cycles" }, "nbformat": 4, "nbformat_minor": 2 }