Home
About
Resume
Projects
Links
Blog
Download notebook
{ "cells": [ { "cell_type": "markdown", "id": "be7512d2-79a8-486f-a6b2-9bb7b567e7b4", "metadata": {}, "source": [ "### Q1\n", "Find the sum of all the multiples of 3 or 5 below 1000." ] }, { "cell_type": "code", "execution_count": 1, "id": "f8b15fca-8075-4f6c-85e7-741a06f3c5c0", "metadata": {}, "outputs": [], "source": [ "# Loops, O(m*n), n is range, m in number of selected multipliers\n", "def sum_multiple_n_in_range_brutal(multipliers, start, end, exclude_end = True):\n", " if exclude_end:\n", " end -= 1\n", " result = 0\n", " for n in range(start,end+1):\n", " if any([n%i==0 for i in multipliers]):\n", " result += n\n", " return result" ] }, { "cell_type": "code", "execution_count": 2, "id": "e5fa4365-1d70-4e4e-aaa4-e5b6d8c12631", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 268 µs, sys: 65 µs, total: 333 µs\n", "Wall time: 337 µs\n" ] }, { "data": { "text/plain": [ "233168" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "sum_multiple_n_in_range_brutal([3,5],1,1000)" ] }, { "cell_type": "code", "execution_count": 3, "id": "cfa7326a-6182-43fe-9d95-f3194d3c284d", "metadata": {}, "outputs": [], "source": [ "# Semi-analytical, O(2^m)\n", "def sum_n_in_range(multiplier, start, end, exclude_end = True):\n", " if exclude_end:\n", " end -= 1\n", " first_term = start + ((multiplier - start)%multiplier)\n", " last_term = end - (end%multiplier)\n", " n_terms = (last_term - first_term) // multiplier + 1\n", " return (first_term + last_term) * n_terms // 2\n", "\n", "def gcd(x, y):\n", " while(y):\n", " x, y = y, x % y\n", " return x\n", "\n", "def lcm(x, y):\n", " return (x*y)//gcd(x,y) \n", "\n", "def lcm_list(x_vector):\n", " temp_vector = x_vector.copy()\n", " temp_lcm = temp_vector.pop()\n", " while temp_vector:\n", " temp_lcm = lcm(temp_lcm,temp_vector.pop())\n", " return temp_lcm\n", "\n", "def combinations(elements):\n", " if len(elements) == 0: \n", " return [[]]\n", " comb = []\n", " for i in combinations(elements[1:]):\n", " comb += [i, i+[elements[0]]]\n", " return comb\n", "\n", "def sum_multiple_n_in_range(multipliers, start, end, exclude_end = True):\n", " result = 0\n", " for comb in combinations(multipliers)[1:]:\n", " result += sum_n_in_range(lcm_list(comb),start,end,exclude_end)*((-1)**(len(comb)-1))\n", " return result\n" ] }, { "cell_type": "code", "execution_count": 4, "id": "9f25caa5-53c9-47f3-b8b9-4824015cb38b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 13 µs, sys: 3 µs, total: 16 µs\n", "Wall time: 18.8 µs\n" ] }, { "data": { "text/plain": [ "233168" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "sum_multiple_n_in_range([3,5],1,1000)" ] }, { "cell_type": "code", "execution_count": null, "id": "9445d691-6501-4d83-8e9f-e5b7d5b15431", "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 Common Functions
Next Notebook:
Project Euler Q2
Loading