euler/ipython/EulerProblem023.ipynb

142 lines
4.0 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Euler Problem 23\n",
"\n",
"A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number.\n",
"\n",
"A number n is called deficient if the sum of its proper divisors is less than n and it is called abundant if this sum exceeds n.\n",
"\n",
"As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.\n",
"\n",
"Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We reuse the sum of proper divisors function and use it to tell whether a number is abundant or not."
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def sum_of_proper_divisors(n):\n",
" from math import sqrt\n",
" if n == 1:\n",
" return 0\n",
" s = 1\n",
" for i in range(2, int(sqrt(n)) + 1):\n",
" if n % i == 0:\n",
" s += i\n",
" x = (n // i)\n",
" if x != i:\n",
" s += x\n",
" return s\n",
"\n",
"def is_abundant(n):\n",
" return sum_of_proper_divisors(n) > n\n",
"\n",
"assert(is_abundant(7) == False)\n",
"assert(is_abundant(12) == True)\n",
"\n",
"abundant_numbers_smaller_30000 = [n for n in range(1, 30001) if is_abundant(n)]\n",
"abundant_numbers_smaller_30000_set = set(abundant_numbers_smaller_30000)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now that we have all abundant numbers in sorted order we can write a function which tests whether a number can be written as a sum of two abundant numbers."
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def is_sum_of_two_abundants(n):\n",
" for a in abundant_numbers_smaller_30000:\n",
" if a > (n // 2):\n",
" return False\n",
" r = n - a\n",
" if r in abundant_numbers_smaller_30000_set:\n",
" return True\n",
" return False\n",
"\n",
"assert(is_sum_of_two_abundants(24) == True)\n",
"assert(is_sum_of_two_abundants(23) == False)\n",
"assert(is_sum_of_two_abundants(28123) == True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we try to brute force."
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4179871\n"
]
}
],
"source": [
"s = sum([i for i in range(1, 30000) if not is_sum_of_two_abundants(i)])\n",
"print(s)\n",
"assert(s == 4179871)"
]
}
],
"metadata": {
"completion_date": "Thu, 5 Nov 2015, 14:48",
"kernelspec": {
"display_name": "Python 3",
"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.5.4"
},
"tags": [
"perfect number",
"abundant",
"factors"
]
},
"nbformat": 4,
"nbformat_minor": 0
}