Files
euler/ipython/EulerProblem047.ipynb
2018-12-22 19:36:44 -05:00

162 lines
3.6 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Distinct primes factors (Euler Problem 47)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"[https://projecteuler.net/problem=47](https://projecteuler.net/problem=47)\n",
"\n",
"The first two consecutive numbers to have two distinct prime factors are:\n",
"\n",
"14 = 2 × 7\n",
"\n",
"15 = 3 × 5\n",
"\n",
"The first three consecutive numbers to have three distinct prime factors are:\n",
"\n",
"644 = 2² × 7 × 23\n",
"\n",
"645 = 3 × 5 × 43\n",
"\n",
"646 = 2 × 17 × 19.\n",
"\n",
"Find the first four consecutive integers to have four distinct prime factors each. What is the first of these numbers?"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2, 3, 7]\n"
]
}
],
"source": [
"def sieve_of_eratosthenes(number):\n",
" primes = []\n",
" prospects = [n for n in range(2, number + 1)]\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",
"import math\n",
"\n",
"def get_prime_factors(n):\n",
" ps = sieve_of_eratosthenes(n)\n",
" fs = []\n",
" for p in ps:\n",
" if n % p == 0:\n",
" fs.append(p)\n",
" while n % p == 0:\n",
" n = n // p\n",
" return fs\n",
"\n",
"def trial_division(n):\n",
" a = [] \n",
" if n % 2 == 0:\n",
" a.append(2)\n",
" while n % 2 == 0:\n",
" n //= 2\n",
" f = 3\n",
" while f * f <= n:\n",
" if n % f == 0:\n",
" a.append(f)\n",
" while n % f == 0:\n",
" n //= f\n",
" else:\n",
" f += 2 \n",
" if n != 1:\n",
" a.append(n)\n",
" return a\n",
" \n",
"assert(get_prime_factors(14) == [2, 7])\n",
"assert(get_prime_factors(644) == [2, 7, 23])\n",
"\n",
"print(trial_division(126))"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"134043\n"
]
}
],
"source": [
"s = []\n",
"for n in range(2, 1000000):\n",
" if len(trial_division(n)) == 4:\n",
" s.append(n)\n",
" else:\n",
" s = []\n",
" if len(s) == 4:\n",
" s = s[0]\n",
" break\n",
"\n",
"print(s)\n",
"assert(s == 134043)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"completion_date": "Sun, 23 Dec 2018, 00:24",
"kernelspec": {
"display_name": "Python 3",
"language": "python3.6",
"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.6.5"
},
"tags": [
"trial division",
"prime",
"brute force"
]
},
"nbformat": 4,
"nbformat_minor": 2
}