150 lines
3.5 KiB
Plaintext
150 lines
3.5 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": 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",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": true
|
|
},
|
|
"outputs": [],
|
|
"source": []
|
|
}
|
|
],
|
|
"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
|
|
}
|