euler/ipython/EulerProblem003.ipynb

204 lines
5.0 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Euler Problem 003\n",
"\n",
"The prime factors of 13195 are 5, 7, 13 and 29.\n",
"\n",
"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": 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\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": true
},
"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": 3,
"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))\n",
"print(6857) # computed the previously but remove it so that we can reexecute the complete kernel"
]
},
{
"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",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6857\n"
]
}
],
"source": [
"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_prime_factors(number):\n",
" prime_generator = prime_generator_function()\n",
" remainder = number\n",
" factors = []\n",
" for p in prime_generator:\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\n",
"\n",
"print(get_largest_prime(600851475143))\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here we go. Obviously much better than precalculation primes that we never need."
]
}
],
"metadata": {
"completion_date": "Tue, 19 Aug 2014, 23:35",
"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": [
"prime"
]
},
"nbformat": 4,
"nbformat_minor": 1
}