Created
February 5, 2023 17:50
-
-
Save bbeck/d471c47296b7274075ca12a31b8d177f to your computer and use it in GitHub Desktop.
Prime Combinatons
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def bfs(root, get_children, is_goal):\n", | |
" queue = [root]\n", | |
" parents = {}\n", | |
" seen = set(queue)\n", | |
" \n", | |
" while len(queue) > 0:\n", | |
" current, queue = queue[0], queue[1:]\n", | |
" \n", | |
" if is_goal(current):\n", | |
" return path(current, parents)\n", | |
" \n", | |
" for child in get_children(current):\n", | |
" if child not in seen:\n", | |
" seen.add(child)\n", | |
" parents[child] = current\n", | |
" queue.append(child)\n", | |
"\n", | |
" return None\n", | |
"\n", | |
"def path(node, parents):\n", | |
" p = []\n", | |
" \n", | |
" while node is not None:\n", | |
" p.append(node)\n", | |
" node = parents.get(node)\n", | |
" \n", | |
" return p[::-1]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"# Precompute all primes <= 99999\n", | |
"primes = set()\n", | |
"for n in range(2, 100000):\n", | |
" if not any(n % p == 0 for p in primes):\n", | |
" primes.add(n)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"def get_child_combinations(n):\n", | |
" for i in range(5):\n", | |
" d = (n//(10**i)) % 10\n", | |
" \n", | |
" if d-1 >= 0:\n", | |
" yield n - (10**i)\n", | |
" else:\n", | |
" yield n + 9*(10**i)\n", | |
" \n", | |
" if d+1 < 10:\n", | |
" yield n + (10**i)\n", | |
" else:\n", | |
" yield n - 9*(10**i)\n", | |
" \n", | |
"def get_prime_child_combinations(n):\n", | |
" return (c for c in get_child_combinations(n) if c in primes)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'00001 -> 00002 -> 00003 -> 00013 -> 00023 -> 90023 -> 99023 -> 99923 -> 99823 -> 99833 -> 99733 -> 09733 -> 00733 -> 10733 -> 10723'" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"p = bfs(1, get_prime_child_combinations, lambda n: n == 10723)\n", | |
"\" -> \".join(\"%05d\" % c for c in p)" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3 (ipykernel)", | |
"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.7.12" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment