Home
About
Resume
Projects
Links
Blog
Download notebook
{ "cells": [ { "cell_type": "markdown", "id": "8752103c-0595-4348-9363-bb8143ff324b", "metadata": {}, "source": [ "### Q8\n", "Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?" ] }, { "cell_type": "code", "execution_count": 1, "id": "b19919cc-37fc-4329-a2a1-107d3a604fec", "metadata": {}, "outputs": [], "source": [ "num_string = \"7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450\"" ] }, { "cell_type": "code", "execution_count": 2, "id": "1a17ea37-3fd0-4fd5-994a-3e2c0e142ee5", "metadata": {}, "outputs": [], "source": [ "def prod(x_vector):\n", " result = 1\n", " for i in x_vector:\n", " result *= i\n", " return result\n", "\n", "def adjacent_product(num_list, len_adjacent):\n", " base_prod = prod([int(c) for c in num_list[:len_adjacent]])\n", " result = [base_prod]\n", " for i in range(1,len(num_list)-len_adjacent+1):\n", " result.append(result[i-1]*int(num_list[i+len_adjacent-1])//int(num_list[i-1]))\n", " return result\n", "\n", "def largest_adjacent_product(num_string, len_adjacent):\n", " splited_strings = [i for i in num_string.split(\"0\") if len(i)>=len_adjacent]\n", " if len(splited_strings) == 0:\n", " return 0\n", " return max([max(adjacent_product(splited_string,len_adjacent)) for splited_string in splited_strings]) " ] }, { "cell_type": "code", "execution_count": 3, "id": "0c407333-096d-49de-9d71-bdcc79a5e8b0", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 243 µs, sys: 48 µs, total: 291 µs\n", "Wall time: 294 µs\n" ] }, { "data": { "text/plain": [ "23514624000" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "largest_adjacent_product(num_string,13)" ] }, { "cell_type": "code", "execution_count": null, "id": "57c4f634-7837-4f22-97a7-3ab9657805a7", "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 Q7
Next Notebook:
Project Euler Q9
Loading