Implemented solutions 5-7 in ipython.
This commit is contained in:
312
ipython/EulerProblem005.html
Normal file
312
ipython/EulerProblem005.html
Normal file
File diff suppressed because one or more lines are too long
130
ipython/EulerProblem005.ipynb
Normal file
130
ipython/EulerProblem005.ipynb
Normal file
@@ -0,0 +1,130 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Euler Problem\n",
|
||||||
|
"\n",
|
||||||
|
"2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.\n",
|
||||||
|
"\n",
|
||||||
|
"What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?\n",
|
||||||
|
"\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"My easiest guess is to multiply all prime numbers till the number."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"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"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"210\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"from operator import mul\n",
|
||||||
|
"from functools import reduce\n",
|
||||||
|
"\n",
|
||||||
|
"def get_number_which_is_divisible_by_all_numbers_from_one_to(n):\n",
|
||||||
|
" ps= get_primes_smaller(n + 1)\n",
|
||||||
|
" return reduce(mul, ps, 1)\n",
|
||||||
|
"\n",
|
||||||
|
"print(get_number_which_is_divisible_by_all_numbers_from_one_to(10))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"That obviously didn't work. The reason is that the same prime can occur multiple times in the factorization of a divisor. For example $2^{3} = 8$. We can always brute force of course. We do a smart brute force and only check multiples from the product of primes because this factor must be part of the solution."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"232792560\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"def is_divisible_by_numbers_smaller_or_equal(number, maximum_number):\n",
|
||||||
|
" for n in range(2, maximum_number + 1):\n",
|
||||||
|
" if number % n != 0:\n",
|
||||||
|
" return False\n",
|
||||||
|
" return True\n",
|
||||||
|
"\n",
|
||||||
|
"def get_number_which_is_divisible_by_all_numbers_from_one_to(n):\n",
|
||||||
|
" ps = get_primes_smaller(n + 1)\n",
|
||||||
|
" factor = reduce(mul, ps, 1)\n",
|
||||||
|
" multiples_of_factor = factor\n",
|
||||||
|
" while True:\n",
|
||||||
|
" if is_divisible_by_numbers_smaller_or_equal(multiples_of_factor, n):\n",
|
||||||
|
" return multiples_of_factor\n",
|
||||||
|
" multiples_of_factor += factor\n",
|
||||||
|
"\n",
|
||||||
|
"assert(get_number_which_is_divisible_by_all_numbers_from_one_to(10) == 2520)\n",
|
||||||
|
"print(get_number_which_is_divisible_by_all_numbers_from_one_to(20))"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"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"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
||||||
265
ipython/EulerProblem006.html
Normal file
265
ipython/EulerProblem006.html
Normal file
File diff suppressed because one or more lines are too long
94
ipython/EulerProblem006.ipynb
Normal file
94
ipython/EulerProblem006.ipynb
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Euler Problem 6\n",
|
||||||
|
"\n",
|
||||||
|
"The sum of the squares of the first ten natural numbers is,\n",
|
||||||
|
"\n",
|
||||||
|
"$1^2 + 2^2 + ... + 10^2 = 385$\n",
|
||||||
|
"\n",
|
||||||
|
"The square of the sum of the first ten natural numbers is,\n",
|
||||||
|
"\n",
|
||||||
|
"$(1 + 2 + ... + 10)^2 = 55^2 = 3025$\n",
|
||||||
|
"\n",
|
||||||
|
"Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is $3025 − 385 = 2640$.\n",
|
||||||
|
"\n",
|
||||||
|
"Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Okay, this is as straightforward as it can get."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"25164150\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"s = sum([x for x in range(1, 101)])**2 - sum([x**2 for x in range(1, 101)])\n",
|
||||||
|
"assert(s == 25164150)\n",
|
||||||
|
"print(s)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"General solution."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"def diff_between_sum_of_squares_and_square_sum(n):\n",
|
||||||
|
" return sum([x for x in range(1, n + 1)])**2 - sum([x**2 for x in range(1, n + 1)])\n",
|
||||||
|
"\n",
|
||||||
|
"assert(diff_between_sum_of_squares_and_square_sum(100) == 25164150)\n",
|
||||||
|
"assert(diff_between_sum_of_squares_and_square_sum(10) == 2640)"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"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"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
||||||
257
ipython/EulerProblem007.html
Normal file
257
ipython/EulerProblem007.html
Normal file
File diff suppressed because one or more lines are too long
87
ipython/EulerProblem007.ipynb
Normal file
87
ipython/EulerProblem007.ipynb
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Euler Problem 7\n",
|
||||||
|
"\n",
|
||||||
|
"By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.\n",
|
||||||
|
"\n",
|
||||||
|
"What is the 10 001st prime number?"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"We reuse our prime functions and use a trick from [stackoverflow](https://stackoverflow.com/questions/2300756/get-the-nth-item-of-a-generator-in-python) to get the nth element."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 7,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"104743\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"import itertools\n",
|
||||||
|
"\n",
|
||||||
|
"def is_prime(n, smaller_primes):\n",
|
||||||
|
" for s in smaller_primes:\n",
|
||||||
|
" if n % s == 0:\n",
|
||||||
|
" return False\n",
|
||||||
|
" if s * s > n:\n",
|
||||||
|
" return True\n",
|
||||||
|
" return True\n",
|
||||||
|
"\n",
|
||||||
|
"def prime_generator_function():\n",
|
||||||
|
" primes = [2, 3, 5, 7]\n",
|
||||||
|
" for p in primes:\n",
|
||||||
|
" yield p\n",
|
||||||
|
" while True:\n",
|
||||||
|
" p += 2\n",
|
||||||
|
" if is_prime(p, primes):\n",
|
||||||
|
" primes.append(p)\n",
|
||||||
|
" yield p\n",
|
||||||
|
"\n",
|
||||||
|
"def get_nth_prime(n):\n",
|
||||||
|
" ps = prime_generator_function()\n",
|
||||||
|
" return next(itertools.islice(ps, n - 1, n))\n",
|
||||||
|
"\n",
|
||||||
|
"assert(get_nth_prime(6) == 13)\n",
|
||||||
|
"print(get_nth_prime(10001))"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"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"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user