Home
About
Resume
Projects
Links
Blog
Download notebook
{ "cells": [ { "cell_type": "markdown", "id": "830b82ae-eee6-4258-83ed-d28379a8a478", "metadata": {}, "source": [ "### Q23\n", "Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers." ] }, { "cell_type": "code", "execution_count": 27, "id": "5593e1a9-8016-4907-95db-acbaa9eea4fe", "metadata": {}, "outputs": [], "source": [ "def divisors(n):\n", " divs = []\n", " for i in range(1,int((n)**(1/2))+1):\n", " if n%i == 0:\n", " divs.append(i)\n", " divs.append(n//i)\n", " return list(set(divs))\n", "\n", "def d(n):\n", " return sum(divisors(n))-n\n", "\n", "def sum_non_abundant_nums(max_range,exclude_end = False):\n", " if exclude_end:\n", " max_range -= 1\n", " abundant_nums_list = set()\n", " result = (1 + max_range) * max_range // 2\n", " for i in range(1,max_range+1):\n", " if d(i) > i:\n", " abundant_nums_list.add(i)\n", " for j in abundant_nums_list:\n", " if (i-j) in abundant_nums_list:\n", " result -= i\n", " break\n", " return result" ] }, { "cell_type": "code", "execution_count": 28, "id": "037b11a4-6710-43c4-8248-6949ff9917d9", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 224 ms, sys: 0 ns, total: 224 ms\n", "Wall time: 222 ms\n" ] }, { "data": { "text/plain": [ "4179871" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%time\n", "sum_non_abundant_nums(28123)" ] }, { "cell_type": "code", "execution_count": null, "id": "64e99ccb-244e-4ffc-b1ff-f6012c715019", "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 Q22
Next Notebook:
Project Euler Q24
Loading