Tryed to solve 21 in ipython.
This commit is contained in:
87
ipython/EulerProblem020.ipynb
Normal file
87
ipython/EulerProblem020.ipynb
Normal file
@@ -0,0 +1,87 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Euler Problem 20\n",
|
||||
"\n",
|
||||
"n! means n × (n − 1) × ... × 3 × 2 × 1\n",
|
||||
"\n",
|
||||
"For example, 10! = 10 × 9 × ... × 3 × 2 × 1 = 3628800,\n",
|
||||
"and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.\n",
|
||||
"\n",
|
||||
"Find the sum of the digits in the number 100!"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Write our own factorial implementation and get the solution."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def factorial(n):\n",
|
||||
" assert(n > 0)\n",
|
||||
" if n == 1:\n",
|
||||
" return 1\n",
|
||||
" else:\n",
|
||||
" return n * factorial(n - 1)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"648\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"f100 = factorial(100)\n",
|
||||
"print(sum(map(int, str(f100))))\n",
|
||||
"assert(sum(map(int, str(f100))) == 648)"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"completion_date": "Fri, 5 Sep 2014, 10:42",
|
||||
"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": [
|
||||
"factorial"
|
||||
]
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
}
|
||||
219
ipython/EulerProblem021.ipynb
Normal file
219
ipython/EulerProblem021.ipynb
Normal file
@@ -0,0 +1,219 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Euler Problem 21\n",
|
||||
"\n",
|
||||
"Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n).\n",
|
||||
"If d(a) = b and d(b) = a, where a ≠ b, then a and b are an amicable pair and each of a and b are called amicable numbers.\n",
|
||||
"\n",
|
||||
"For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.\n",
|
||||
"\n",
|
||||
"Evaluate the sum of all the amicable numbers under 10000."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Reuse factorization function from problem 12."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def get_primes_smaller(number):\n",
|
||||
" primes = []\n",
|
||||
" prospects = [n for n in range(2, number)]\n",
|
||||
" while prospects:\n",
|
||||
" p = prospects[0]\n",
|
||||
" prospects = [x for x in prospects if x % p != 0]\n",
|
||||
" primes.append(p)\n",
|
||||
" return primes\n",
|
||||
"\n",
|
||||
"primes_smaller_10000 = get_primes_smaller(10000)\n",
|
||||
"\n",
|
||||
"def get_prime_factors(number):\n",
|
||||
" remainder = number\n",
|
||||
" factors = []\n",
|
||||
" for p in primes_smaller_10000:\n",
|
||||
" while remainder % p == 0:\n",
|
||||
" remainder /= p\n",
|
||||
" factors.append(p)\n",
|
||||
" if remainder == 1 or p * p > number:\n",
|
||||
" break\n",
|
||||
" if remainder != 1:\n",
|
||||
" factors.append(remainder)\n",
|
||||
" return factors"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"If we have a list of all factors we group them. To get all potential divisors for each group we may use each factor 0 to len(factor_group). We then combine the potential divisors of all factor groups. "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def product(xs):\n",
|
||||
" from operator import mul\n",
|
||||
" from functools import reduce\n",
|
||||
" return reduce(mul, xs, 1)\n",
|
||||
"\n",
|
||||
"def group(xs):\n",
|
||||
" from functools import reduce\n",
|
||||
" def f(xss, x):\n",
|
||||
" if xss and x in xss[-1]:\n",
|
||||
" xss[-1].append(x)\n",
|
||||
" else:\n",
|
||||
" xss.append([x])\n",
|
||||
" return xss\n",
|
||||
" return reduce(f, xs, [])\n",
|
||||
"\n",
|
||||
"def combinations(xss):\n",
|
||||
" from functools import reduce\n",
|
||||
" def f(xs, ys):\n",
|
||||
" return [x + [y]\n",
|
||||
" for y in ys\n",
|
||||
" for x in xs\n",
|
||||
" ] \n",
|
||||
" return reduce(f, list(xss), [[]])\n",
|
||||
"\n",
|
||||
"def get_divisors_for_factor_group(factor_group):\n",
|
||||
" factor = factor_group[0]\n",
|
||||
" return [factor**i for i in range(len(factor_group) + 1)]\n",
|
||||
"\n",
|
||||
"def get_proper_divisors(number):\n",
|
||||
" factors = get_prime_factors(number)\n",
|
||||
" factor_groups = map(get_divisors_for_factor_group, group(factors))\n",
|
||||
" factor_combinations = combinations(factor_groups)\n",
|
||||
" divisors = map(product, factor_combinations)\n",
|
||||
" factors = list(divisors)\n",
|
||||
" factors.sort()\n",
|
||||
" return factors[:-1] # last value is not a proper divisor\n",
|
||||
"\n",
|
||||
"assert(get_proper_divisors(220) == [1, 2, 4, 5, 10, 11, 20, 22, 44, 55, 110])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"We can finally start to get the solution."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def sum_of_proper_divisors(number):\n",
|
||||
" return sum(get_proper_divisors(number))\n",
|
||||
"\n",
|
||||
"assert(sum_of_proper_divisors(220) == 284)\n",
|
||||
"assert(sum_of_proper_divisors(284) == 220)\n",
|
||||
"\n",
|
||||
"def is_amicable(number):\n",
|
||||
" s = sum_of_proper_divisors(number)\n",
|
||||
" return number == sum_of_proper_divisors(s)\n",
|
||||
"\n",
|
||||
"assert(is_amicable(220))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"ename": "KeyboardInterrupt",
|
||||
"evalue": "",
|
||||
"output_type": "error",
|
||||
"traceback": [
|
||||
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
|
||||
"\u001b[1;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
|
||||
"\u001b[1;32m<ipython-input-4-afe32366db14>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mamicable_till_10000\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m10000\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mis_amicable\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
|
||||
"\u001b[1;32m<ipython-input-4-afe32366db14>\u001b[0m in \u001b[0;36m<listcomp>\u001b[1;34m(.0)\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mamicable_till_10000\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m10000\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mis_amicable\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
|
||||
"\u001b[1;32m<ipython-input-3-4ae1320a291d>\u001b[0m in \u001b[0;36mis_amicable\u001b[1;34m(number)\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mis_amicable\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnumber\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 8\u001b[0m \u001b[0ms\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0msum_of_proper_divisors\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnumber\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 9\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mnumber\u001b[0m \u001b[1;33m==\u001b[0m \u001b[0msum_of_proper_divisors\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ms\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 10\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 11\u001b[0m \u001b[1;32massert\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mis_amicable\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m220\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[1;32m<ipython-input-3-4ae1320a291d>\u001b[0m in \u001b[0;36msum_of_proper_divisors\u001b[1;34m(number)\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0msum_of_proper_divisors\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnumber\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0msum\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mget_proper_divisors\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnumber\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 3\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;32massert\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msum_of_proper_divisors\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m220\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;36m284\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;32massert\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msum_of_proper_divisors\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m284\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;36m220\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[1;32m<ipython-input-2-741d0e28361b>\u001b[0m in \u001b[0;36mget_proper_divisors\u001b[1;34m(number)\u001b[0m\n\u001b[0;32m 28\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 29\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mget_proper_divisors\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnumber\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 30\u001b[1;33m \u001b[0mfactors\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mget_prime_factors\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnumber\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 31\u001b[0m \u001b[0mfactor_groups\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mmap\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mget_divisors_for_factor_group\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mgroup\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfactors\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 32\u001b[0m \u001b[0mfactor_combinations\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcombinations\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfactor_groups\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[1;32m<ipython-input-1-87689c2c868f>\u001b[0m in \u001b[0;36mget_prime_factors\u001b[1;34m(number)\u001b[0m\n\u001b[0;32m 15\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mp\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mprimes_smaller_10000\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 16\u001b[0m \u001b[1;32mwhile\u001b[0m \u001b[0mremainder\u001b[0m \u001b[1;33m%\u001b[0m \u001b[0mp\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 17\u001b[1;33m \u001b[0mremainder\u001b[0m \u001b[1;33m/=\u001b[0m \u001b[0mp\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 18\u001b[0m \u001b[0mfactors\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mp\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 19\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mremainder\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;36m1\u001b[0m \u001b[1;32mor\u001b[0m \u001b[0mp\u001b[0m \u001b[1;33m*\u001b[0m \u001b[0mp\u001b[0m \u001b[1;33m>\u001b[0m \u001b[0mnumber\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[1;31mKeyboardInterrupt\u001b[0m: "
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"amicable_till_10000 = [i for i in range(1, 10000) if is_amicable(i)]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"for a in amicable_till_10000:\n",
|
||||
" print(a)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"completion_date": "Fri, 5 Sep 2014, 14:39",
|
||||
"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": [
|
||||
"amicable",
|
||||
"factors",
|
||||
"combinations",
|
||||
"reduce",
|
||||
"prime"
|
||||
]
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 0
|
||||
}
|
||||
Reference in New Issue
Block a user