Did problem 11 in ipython.
This commit is contained in:
@@ -5,7 +5,150 @@
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Euler Problem 10\n",
|
||||
"\n"
|
||||
"\n",
|
||||
"The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.\n",
|
||||
"\n",
|
||||
"Find the sum of all the primes below two million."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Okay, reuse prime generator from 7 and go."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"61.8695481220002\n",
|
||||
"142913828922\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import timeit\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 brute_force(): \n",
|
||||
" ps = prime_generator_function()\n",
|
||||
" s = 0\n",
|
||||
" p = next(ps)\n",
|
||||
" while p < 2000000:\n",
|
||||
" s += p\n",
|
||||
" p = next(ps)\n",
|
||||
" return s\n",
|
||||
" \n",
|
||||
"print(timeit.timeit(brute_force, number=10))\n",
|
||||
"assert(brute_force() == 142913828922)\n",
|
||||
"print(brute_force())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Okay, here it may actually be way smarter to use a sieve. We implent it because the old one was shitty. Okay, I am actually interested in the time difference. So we use the old one first and then implement the optimization."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"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",
|
||||
"def brute_force():\n",
|
||||
" return sum(get_primes_smaller(2000000))\n",
|
||||
"\n",
|
||||
"#print(timeit.timeit(brute_force, number=1))\n",
|
||||
"#print(brute_force())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"This did not even terminate. We optimize the sieve by stopping when $p^2 > number$."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"74.5315607270004\n",
|
||||
"142913828922\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"def sieve_of_eratosthenes(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",
|
||||
" if p * p > number:\n",
|
||||
" break\n",
|
||||
" return primes + prospects\n",
|
||||
"\n",
|
||||
"def brute_force():\n",
|
||||
" return sum(sieve_of_eratosthenes(2000000))\n",
|
||||
"\n",
|
||||
"assert(sieve_of_eratosthenes(10) == [2, 3, 5, 7,])\n",
|
||||
"print(timeit.timeit(brute_force, number=10))\n",
|
||||
"assert(brute_force() == 142913828922)\n",
|
||||
"print(brute_force())"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Okay, I honestly did not expect this to be slower than our generator. Gotta keep that in mind for future prime related problems."
|
||||
]
|
||||
}
|
||||
],
|
||||
@@ -29,7 +172,9 @@
|
||||
"version": "3.5.4"
|
||||
},
|
||||
"tags": [
|
||||
"primes"
|
||||
"primes",
|
||||
"brute force",
|
||||
"timeit"
|
||||
]
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
||||
Reference in New Issue
Block a user