From ac22f6c944dfeb7a2e8cbf896e79bb498ba06e23 Mon Sep 17 00:00:00 2001 From: filifa Date: Sat, 19 Apr 2025 23:02:53 -0400 Subject: [PATCH] add problem 33 --- notebooks/problem0033.ipynb | 128 ++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 notebooks/problem0033.ipynb diff --git a/notebooks/problem0033.ipynb b/notebooks/problem0033.ipynb new file mode 100644 index 0000000..459b9da --- /dev/null +++ b/notebooks/problem0033.ipynb @@ -0,0 +1,128 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "eb246b50", + "metadata": {}, + "source": [ + "# [Digit Cancelling Fractions](https://projecteuler.net/problem=33)\n", + "\n", + "The search term for this concept is an [anomalous cancellation](https://en.wikipedia.org/wiki/Anomalous_cancellation). We can write a function that checks if an anomalous cancellation can happen by storing the digits of the numerator and denominator into four variables total, then checking four separate cases for if a digit can be \"cancelled.\"" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "d4e94963", + "metadata": {}, + "outputs": [], + "source": [ + "def digits(n):\n", + " return (n // 10, n % 10)\n", + "\n", + "\n", + "def can_cancel_digits(n, d):\n", + " x, y = digits(n)\n", + " z, w = digits(d)\n", + "\n", + " f = QQ(n/d)\n", + "\n", + " if x == z and w != 0 and y/w == f:\n", + " return True\n", + " elif x == w and y/z == f:\n", + " return True\n", + " elif y == z and w != 0 and x/w == f:\n", + " return True\n", + " elif y == w and w != 0 and x/z == f:\n", + " return True\n", + "\n", + " return False" + ] + }, + { + "cell_type": "markdown", + "id": "a68309e9", + "metadata": {}, + "source": [ + "We're only dealing with two-digit numerators and denominators, so this is easy to brute force." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "756614f7", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{(16, 64), (19, 95), (26, 65), (49, 98)}" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "fractions = set()\n", + "for n in range(10, 100):\n", + " for d in range(n + 1, 100):\n", + " if can_cancel_digits(n, d):\n", + " fractions.add((n, d))\n", + "\n", + "fractions" + ] + }, + { + "cell_type": "markdown", + "id": "6d3fb43b", + "metadata": {}, + "source": [ + "There's only four such fractions." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "90f47a7f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "100" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "prod(QQ(n/d) for (n, d) in fractions).denominator()" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "SageMath 9.5", + "language": "sage", + "name": "sagemath" + }, + "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.11.2" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}