Solved 22 in ipython.

This commit is contained in:
2018-02-06 19:23:59 +01:00
parent 3698e639b9
commit b7e7314e99
8 changed files with 1615 additions and 11 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -24,7 +24,9 @@
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
@@ -74,7 +76,9 @@
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
@@ -294,7 +298,9 @@
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
@@ -320,7 +326,9 @@
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
@@ -341,7 +349,9 @@
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
@@ -401,16 +411,12 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
"version": "3.5.4"
},
"tags": [
"amicable",
"factors",
"combinations",
"reduce",
"prime",
"sieve of eratosthenes",
"partial",
"factors",
"timeit"
]
},

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,126 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Euler Problem 22\n",
"\n",
"Using names.txt (saved as EulerProblem022.txt in the same directory as this notebook), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.\n",
"\n",
"For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 × 53 = 49714.\n",
"\n",
"What is the total of all the name scores in the file?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Okay, this should be straight forward."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"with open('EulerProblem022.txt', 'r') as f:\n",
" names = f.read().split(',')\n",
"\n",
"def get_score_for_name(name):\n",
" return sum([ord(c) - ord('A') + 1 for c in name if not c == '\"'])\n",
"\n",
"assert(get_score_for_name('COLIN') == 53)\n",
"names.sort()\n",
"s = sum([(i + 1) * get_score_for_name(name) for i, name in enumerate(names)])\n",
"assert(s == 871198282)\n",
"print(s)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"I think nothing to explain here. The only question is what was if we hadn't the Python sort function to sort into alphabetical order, then we would have to write our own compare function and use it with what ever sorting algorithm."
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Arnold before Felix\n",
"Felix before Felixb\n",
"Felix before Felixb\n",
"Felix is Felix\n"
]
}
],
"source": [
"def compare(a, b):\n",
" try:\n",
" for i in range(len(a)):\n",
" if a[i] < b[i]:\n",
" return '{} before {}'.format(a, b)\n",
" elif a[i] > b[i]:\n",
" return '{} before {}'.format(b, a)\n",
" except IndexError:\n",
" pass\n",
" if len(a) < len(b):\n",
" return '{} before {}'.format(a, b)\n",
" elif len(a) > len(b):\n",
" return '{} before {}'.format(b, a)\n",
" else:\n",
" return '{} is {}'.format(b, a)\n",
"\n",
"print(compare('Felix', 'Arnold'))\n",
"print(compare('Felix', 'Felixb'))\n",
"print(compare('Felixb', 'Felix'))\n",
"print(compare('Felix', 'Felix'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Obviously, the algorithm would return True/False or 0/1/-1 for real sorting."
]
}
],
"metadata": {
"completion_date": "Fri, 5 Sep 2014, 15:24",
"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": [
"sorting",
"lexicographical order"
]
},
"nbformat": 4,
"nbformat_minor": 0
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,54 @@
{
"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": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"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"
]
},
"nbformat": 4,
"nbformat_minor": 0
}