{
 "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
}
