Home
About
Resume
Projects
Links
Blog
Download notebook
{ "cells": [ { "cell_type": "markdown", "id": "bb93f879-8994-4f26-867b-b5d725694c6a", "metadata": {}, "source": [ "### Q12\n", "What is the value of the first triangle number to have over five hundred divisors?" ] }, { "cell_type": "code", "execution_count": 1, "id": "50407c46-cfce-445d-bdea-517d26f2dfa4", "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 list_count(x_vector):\n", " result = {}\n", " for x in x_vector:\n", " result[x] = result.get(x,0) + 1\n", " return result\n", "\n", "def prime_factors(n):\n", " i = 2\n", " factors = []\n", " while i * i <= n:\n", " if n % i:\n", " i += 1\n", " else:\n", " n //= i\n", " factors.append(i)\n", " if n > 1:\n", " factors.append(n)\n", " return factors\n", "\n", "def num_divisors_of_triangle_num(n):\n", " if n==1:\n", " return 1\n", " factors = prime_factors(n)\n", " factors += prime_factors(n+1)\n", " factors.remove(2)\n", " return prod([i+1 for i in list_count(factors).values()])\n", "\n", "def triangle_num(n):\n", " return (n+1)*n//2\n", "\n", "def triangle_num_with_over_n_num_divisors(n):\n", " idx = 1\n", " while True:\n", " if num_divisors_of_triangle_num(idx) > n:\n", " return triangle_num(idx)\n", " idx += 1" ] }, { "cell_type": "code", "execution_count": 2, "id": "2a5c5028-cfc0-4373-8b2e-f01b9f52c05a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 58.5 ms, sys: 0 ns, total: 58.5 ms\n", "Wall time: 55.5 ms\n" ] }, { "data": { "text/plain": [ "76576500" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "triangle_num_with_over_n_num_divisors(500)" ] }, { "cell_type": "code", "execution_count": null, "id": "64372508-1687-465e-b0ac-21c758dfd044", "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 Q11
Next Notebook:
Project Euler Q13
Loading