diff --git a/ipython/EulerProblem050.ipynb b/ipython/EulerProblem050.ipynb new file mode 100644 index 0000000..a971fb4 --- /dev/null +++ b/ipython/EulerProblem050.ipynb @@ -0,0 +1,167 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Consecutive prime sum (Euler Problem 50)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "[https://projecteuler.net/problem=50](https://projecteuler.net/problem=50)\n", + "\n", + "The prime 41, can be written as the sum of six consecutive primes:\n", + "\n", + "$41 = 2 + 3 + 5 + 7 + 11 + 13$\n", + "\n", + "This is the longest sum of consecutive primes that adds to a prime below one-hundred.\n", + "\n", + "The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953.\n", + "\n", + "Which prime, below one-million, can be written as the sum of the most consecutive primes?" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "def sieve_of_eratosthenes(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", + " if p * p > number:\n", + " break\n", + " return primes + prospects\n" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "def find_max_series(start_index, series_list, series_set): \n", + " series_max = series_list[-1]\n", + " total_max = 0\n", + " total = 0 \n", + " for i in range(start_index, len(series_list)):\n", + " total = total + series_list[i]\n", + " if total in series_set:\n", + " length = i - start_index + 1\n", + " total_max = total\n", + " if total > series_max:\n", + " return (length, total_max)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(6, 41)\n" + ] + } + ], + "source": [ + "n_max = 100\n", + "ps = sieve_of_eratosthenes(n_max)\n", + "ps_set = set(ps)\n", + "ps_max = max(ps)\n", + "s = max([x for x in [find_max_series(i, ps, ps_set) for i in range(0, n_max)] if x])\n", + "print(s)\n", + "assert(s[1] == 41)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(21, 953)\n" + ] + } + ], + "source": [ + "n_max = 1000\n", + "ps = sieve_of_eratosthenes(n_max)\n", + "ps_set = set(ps)\n", + "ps_max = max(ps)\n", + "s = max([x for x in [find_max_series(i, ps, ps_set) for i in range(0, n_max)] if x])\n", + "print(s)\n", + "assert(s[0] == 21)\n", + "assert(s[1] == 953)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(543, 997651)\n", + "997651\n" + ] + } + ], + "source": [ + "n_max = 1000000\n", + "ps = sieve_of_eratosthenes(n_max)\n", + "ps_set = set(ps)\n", + "ps_max = max(ps)\n", + "s = max([x for x in [find_max_series(i, ps, ps_set) for i in range(0, n_max)] if x])\n", + "print(s)\n", + "assert(s[1] == 997651)\n", + "print(s[1])" + ] + } + ], + "metadata": { + "completion_date": "Sun, 23 Dec 2018, 02:38", + "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", + "consecutive", + "primes", + "search" + ] + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/ipython/EulerProblem051.ipynb b/ipython/EulerProblem051.ipynb new file mode 100644 index 0000000..b57dc61 --- /dev/null +++ b/ipython/EulerProblem051.ipynb @@ -0,0 +1,58 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Prime digit replacements (Euler Problem 51)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "[https://projecteuler.net/problem=51](https://projecteuler.net/problem=51)\n", + "\n", + "By replacing the 1st digit of the 2-digit number x3, it turns out that six of the nine possible values: 13, 23, 43, 53, 73, and 83, are all prime.\n", + "\n", + "By replacing the 3rd and 4th digits of 56xx3 with the same digit, this 5-digit number is the first example having seven primes among the ten generated numbers, yielding the family: 56003, 56113, 56333, 56443, 56663, 56773, and 56993. Consequently 56003, being the first member of this family, is the smallest prime with this property.\n", + "\n", + "Find the smallest prime which, by replacing part of the number (not necessarily adjacent digits) with the same digit, is part of an eight prime value family." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "completion_date": "", + "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": [] + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/ipython/html/EulerProblem050.html b/ipython/html/EulerProblem050.html new file mode 100644 index 0000000..5e83d6a --- /dev/null +++ b/ipython/html/EulerProblem050.html @@ -0,0 +1,11921 @@ + + + +
+https://projecteuler.net/problem=50
+The prime 41, can be written as the sum of six consecutive primes:
+$41 = 2 + 3 + 5 + 7 + 11 + 13$
+This is the longest sum of consecutive primes that adds to a prime below one-hundred.
+The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953.
+Which prime, below one-million, can be written as the sum of the most consecutive primes?
+def sieve_of_eratosthenes(number):
+ primes = []
+ prospects = [n for n in range(2, number)]
+ while prospects:
+ p = prospects[0]
+ prospects = [x for x in prospects if x % p != 0]
+ primes.append(p)
+ if p * p > number:
+ break
+ return primes + prospects
+def find_max_series(start_index, series_list, series_set):
+ series_max = series_list[-1]
+ total_max = 0
+ total = 0
+ for i in range(start_index, len(series_list)):
+ total = total + series_list[i]
+ if total in series_set:
+ length = i - start_index + 1
+ total_max = total
+ if total > series_max:
+ return (length, total_max)
+n_max = 100
+ps = sieve_of_eratosthenes(n_max)
+ps_set = set(ps)
+ps_max = max(ps)
+s = max([x for x in [find_max_series(i, ps, ps_set) for i in range(0, n_max)] if x])
+print(s)
+assert(s[1] == 41)
+n_max = 1000
+ps = sieve_of_eratosthenes(n_max)
+ps_set = set(ps)
+ps_max = max(ps)
+s = max([x for x in [find_max_series(i, ps, ps_set) for i in range(0, n_max)] if x])
+print(s)
+assert(s[0] == 21)
+assert(s[1] == 953)
+n_max = 1000000
+ps = sieve_of_eratosthenes(n_max)
+ps_set = set(ps)
+ps_max = max(ps)
+s = max([x for x in [find_max_series(i, ps, ps_set) for i in range(0, n_max)] if x])
+print(s)
+assert(s[1] == 997651)
+print(s[1])
+https://projecteuler.net/problem=51
+By replacing the 1st digit of the 2-digit number x3, it turns out that six of the nine possible values: 13, 23, 43, 53, 73, and 83, are all prime.
+By replacing the 3rd and 4th digits of 56xx3 with the same digit, this 5-digit number is the first example having seven primes among the ten generated numbers, yielding the family: 56003, 56113, 56333, 56443, 56663, 56773, and 56993. Consequently 56003, being the first member of this family, is the smallest prime with this property.
+Find the smallest prime which, by replacing part of the number (not necessarily adjacent digits) with the same digit, is part of an eight prime value family.
+
+