Solved 47 and 48.

main
Felix Martin 2018-12-22 19:36:44 -05:00
parent 21716b5b1a
commit 477e0b8710
2 changed files with 246 additions and 0 deletions

View File

@ -0,0 +1,161 @@
{
"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
}

View File

@ -0,0 +1,85 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Self powers (Euler Problem 48)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"[https://projecteuler.net/problem=48](https://projecteuler.net/problem=48)\n",
"\n",
"The series, $1^1 + 2^2 + 3^3 + ... + 10^{10} = 10405071317$.\n",
"\n",
"Find the last ten digits of the series, $1^1 + 2^2 + 3^3 + ... + 1000^{1000}$."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Okay, this would be way harder in C/C++. In every language with long int support it is easy. See the number of people who have solved it."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"9110846700\n"
]
}
],
"source": [
"s = int(str(sum([i**i for i in range(1, 1001)]))[-10:])\n",
"assert(s == 9110846700)\n",
"print(s)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"completion_date": "Sun, 23 Dec 2018, 00:32",
"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": [
"brute force",
"self power"
]
},
"nbformat": 4,
"nbformat_minor": 2
}