{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Euler Problem 33\n", "\n", "The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly believe that 49/98 = 4/8, which is correct, is obtained by cancelling the 9s.\n", "\n", "We shall consider fractions like, 30/50 = 3/5, to be trivial examples.\n", "\n", "There are exactly four non-trivial examples of this type of fraction, less than one in value, and containing two digits in the numerator and denominator.\n", "\n", "If the product of these four fractions is given in its lowest common terms, find the value of the denominator." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We start write a function which checks if a number is curios and then brute force." ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def is_curious(n, d):\n", " assert(len(str(n)) == 2 and len(str(d)) == 2)\n", " if n == d:\n", " return False\n", " for i in range(1, 10):\n", " if str(i) in str(n) and str(i) in str(d):\n", " try:\n", " n_ = int(str(n).replace(str(i), \"\"))\n", " d_ = int(str(d).replace(str(i), \"\"))\n", " except ValueError:\n", " return False\n", " try:\n", " if n_ / d_ == n / d:\n", " return True\n", " except ZeroDivisionError:\n", " return False\n", " return False\n", "\n", "assert(is_curious(49, 98) == True)\n", "assert(is_curious(30, 50) == False)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "[(16, 64), (19, 95), (26, 65), (49, 98)]" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fs = [(n, d) for n in range(10, 100) for d in range(n, 100) if is_curious(n, d)]\n", "fs" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "387296/38729600\n" ] } ], "source": [ "n = 1\n", "d = 1\n", "for n_, d_ in fs:\n", " n *= n_\n", " d *= d_\n", "\n", "print(\"{}/{}\".format(n, d))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we can see that the solution is $100$. But actually it would be nice to calculate the GCD." ] }, { "cell_type": "code", "execution_count": 40, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "100" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def gcd_euclid(a, b):\n", " if a == b:\n", " return a\n", " elif a > b:\n", " return gcd_euclid(a - b, b)\n", " elif a < b:\n", " return gcd_euclid(a, b - a)\n", " \n", "gcd_nd = gcd_euclid(n, d)\n", "\n", "s = d // gcd_nd\n", "assert(s == 100)\n", "s" ] } ], "metadata": { "completion_date": "Mon, 12 Feb 2018, 17:29", "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.5.4" }, "tags": [ "gcd", "curious", "faction" ] }, "nbformat": 4, "nbformat_minor": 0 }