Home
About
Resume
Projects
Links
Blog
Download notebook
{ "cells": [ { "cell_type": "markdown", "id": "d4c14bb2-b866-437b-9188-aa1cfddce346", "metadata": {}, "source": [ "### Q5\n", "What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?" ] }, { "cell_type": "code", "execution_count": 1, "id": "1d81038a-dc41-4cb3-a345-e0a4a021c6c6", "metadata": {}, "outputs": [], "source": [ "def gcd(x, y):\n", " return gcd(y, x%y) if y else x\n", "\n", "def lcm(x, y):\n", " return (x*y)//gcd(x,y) \n", "\n", "def lcm_vector(x_vector):\n", " result = x_vector[0]\n", " for x in x_vector[1:]:\n", " result = lcm(result,x)\n", " return result\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "8b206d2f-4d16-4388-b2ce-44de9682eb7a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 12 µs, sys: 1 µs, total: 13 µs\n", "Wall time: 14.5 µs\n" ] }, { "data": { "text/plain": [ "232792560" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "lcm_vector(list(range(1,20)))" ] }, { "cell_type": "code", "execution_count": 3, "id": "1bff3bad-e9ee-404e-97aa-c984bc0867b9", "metadata": {}, "outputs": [], "source": [ "def lcm_from_one_to_n(n):\n", " result = 1\n", " L = []\n", " for i in range(1,n+1):\n", " for j in L:\n", " if i%j == 0:\n", " i //= j\n", " L.append(i)\n", " result *= i\n", " return result" ] }, { "cell_type": "code", "execution_count": 4, "id": "8b1efd31-7521-4e4e-8013-b48c6164648d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 13 µs, sys: 1 µs, total: 14 µs\n", "Wall time: 15.7 µs\n" ] }, { "data": { "text/plain": [ "232792560" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "lcm_from_one_to_n(20)" ] }, { "cell_type": "code", "execution_count": null, "id": "b789ba2c-ae57-4690-91f4-c18eb6ecc202", "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 Q4
Next Notebook:
Project Euler Q6
Loading