Solved 3 and fixed bug in publish script.
This commit is contained in:
334
ipython/EulerProblem003.html
Normal file
334
ipython/EulerProblem003.html
Normal file
File diff suppressed because one or more lines are too long
@@ -11,6 +11,110 @@
|
|||||||
"What is the largest prime factor of the number 600851475143?"
|
"What is the largest prime factor of the number 600851475143?"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"We start by writing a function which calculates all primes till a certain value using the Sieve of Eratosthenes."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"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",
|
||||||
|
"assert(get_primes_smaller(0) == [])\n",
|
||||||
|
"assert(get_primes_smaller(10) == [2, 3, 5, 7,])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Now we create a function which does prime factorization. It is very important that we only test primes smaller than the squre root. Otherwise the complexity becomes too big."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import math\n",
|
||||||
|
"\n",
|
||||||
|
"def get_prime_factors(number):\n",
|
||||||
|
" possible_factors = get_primes_smaller(math.ceil(math.sqrt(number)))\n",
|
||||||
|
" remainder = number\n",
|
||||||
|
" factors = []\n",
|
||||||
|
" for p in possible_factors:\n",
|
||||||
|
" while remainder % p == 0:\n",
|
||||||
|
" remainder /= p\n",
|
||||||
|
" factors.append(p)\n",
|
||||||
|
" if remainder == 1:\n",
|
||||||
|
" break\n",
|
||||||
|
" if remainder != 1:\n",
|
||||||
|
" factors.append(remainder)\n",
|
||||||
|
" return factors\n",
|
||||||
|
"\n",
|
||||||
|
"assert(get_prime_factors(7) == [7])\n",
|
||||||
|
"assert(get_prime_factors(12) == [2, 2, 3]) \n",
|
||||||
|
"assert(get_prime_factors(88) == [2, 2, 2, 11]) \n",
|
||||||
|
"assert(get_prime_factors(13195) == [5, 7, 13, 29])\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Now we can go ahead an brute force the solution."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 5,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"6857\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"def get_largest_prime(number):\n",
|
||||||
|
" return get_prime_factors(number)[-1]\n",
|
||||||
|
"\n",
|
||||||
|
"assert(get_largest_prime(13195) == 29)\n",
|
||||||
|
"print(get_largest_prime(600851475143))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"Okay, actually we can brute force, but it is really slow. A better solution is to write a prime number generator which calculates the next number on demand."
|
||||||
|
]
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"execution_count": null,
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ def render_solutions(solutions):
|
|||||||
|
|
||||||
def convert_solutions_to_html(solutions):
|
def convert_solutions_to_html(solutions):
|
||||||
for s in solutions:
|
for s in solutions:
|
||||||
if getmtime(s.html) < getmtime(s.ipynb):
|
if not os.path.isfile(s.html) or getmtime(s.html) < getmtime(s.ipynb):
|
||||||
args = ["ipython", "nbconvert", s.ipynb]
|
args = ["ipython", "nbconvert", s.ipynb]
|
||||||
subprocess.call(args)
|
subprocess.call(args)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user