euler/ipython/EulerProblem014.ipynb

163 lines
4.2 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Euler Problem 14\n",
"\n",
"The following iterative sequence is defined for the set of positive integers:\n",
"\n",
"n → n/2 (n is even)\n",
"n → 3n + 1 (n is odd)\n",
"\n",
"Using the rule above and starting with 13, we generate the following sequence:\n",
"\n",
"$13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1$\n",
"\n",
"It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1.\n",
"\n",
"Which starting number, under one million, produces the longest chain?\n",
"\n",
"NOTE: Once the chain starts the terms are allowed to go above one million."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"I would go for a recursive function with a cache here. This should give us good performance with acceptable memory footprint."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from functools import lru_cache\n",
"from collections import deque\n",
"\n",
"@lru_cache(maxsize=11000000)\n",
"def get_collatz_sequence(n):\n",
" if n == 1:\n",
" return deque([1])\n",
" ds = get_collatz_sequence(n // 2) if n % 2 == 0 else get_collatz_sequence(3 * n + 1)\n",
" ds.appendleft(n)\n",
" return ds\n",
"\n",
"assert(list(get_collatz_sequence(13)) == [13, 40, 20, 10, 5, 16, 8, 4, 2, 1])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Isn't this a beautiful solution? We need a deque to appendleft. Of course, we could also use a regular list and reverse the result, but it is nicer to get the correct solution out right away. Now we simply force the solution."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Okay, we need a loop.\n"
]
}
],
"source": [
"try:\n",
" s = max([(len(get_collatz_sequence(i)), i) for i in range(1000000)])\n",
" print(s)\n",
" assert(s == 837799)\n",
"except RecursionError:\n",
" print(\"Okay, we need a loop.\")\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"11.4885111964837\n",
"837799\n"
]
}
],
"source": [
"cache = {}\n",
"\n",
"def get_collatz_sequence(n):\n",
" global cache\n",
" orig_n = n\n",
" cs = []\n",
" while n != 1:\n",
" if n in cache:\n",
" cs = cs + cache[n]\n",
" cache[orig_n] = cs\n",
" return cs\n",
" cs.append(n)\n",
" n = n // 2 if n % 2 == 0 else 3 * n + 1\n",
" cs.append(n)\n",
" for i in range(len(cs)):\n",
" cache[cs[i]] = cs[i:]\n",
" return cs\n",
"\n",
"assert(list(get_collatz_sequence(13)) == [13, 40, 20, 10, 5, 16, 8, 4, 2, 1])\n",
"\n",
"def brute_force():\n",
" return max([(len(get_collatz_sequence(i)), i) for i in range(1, 1000000)])[1]\n",
"\n",
"s = brute_force()\n",
"assert(s == 837799)\n",
"\n",
"import timeit\n",
"print(timeit.timeit(brute_force, number=10))\n",
"print(brute_force())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here we go. Takes some time. We could optimize by caching only the length and not the complete list."
]
}
],
"metadata": {
"completion_date": "Sun, 31 Aug 2014, 19:45",
"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.6.3"
},
"tags": [
"collatz",
"cache",
"deque"
]
},
"nbformat": 4,
"nbformat_minor": 2
}