Skip to content

Instantly share code, notes, and snippets.

@JnBrymn
Created August 19, 2020 01:05
Show Gist options
  • Save JnBrymn/01dda6f43d20bffc40d5154eae1b6d03 to your computer and use it in GitHub Desktop.
Save JnBrymn/01dda6f43d20bffc40d5154eae1b6d03 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from math import prod\n",
"from random import choice, choices, shuffle\n",
"\n",
"primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199] \n",
"# A function to print all prime factors of \n",
"# a given number n \n",
"def prime_factors(n):\n",
" \"\"\"returns a list of prime factors of n\"\"\"\n",
" p = 0 # in case primes seq empty\n",
" factors = []\n",
" for p in primes: \n",
" while n % p == 0:\n",
" n /= p\n",
" factors.append(int(p))\n",
" if n < p*p:\n",
" if n > 1:\n",
" factors.append(int(n))\n",
" return factors\n",
" return factors"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def nerf(a, b):\n",
" afs = prime_factors(a)\n",
" afs = (n for n in afs)\n",
" bfs = prime_factors(b)\n",
" bfs = (n for n in bfs)\n",
"\n",
" shared = []\n",
" not_shared = []\n",
" af = next(afs)\n",
" bf = next(bfs)\n",
" try:\n",
" while True:\n",
" if af<bf:\n",
" not_shared.append(af)\n",
" af = None\n",
" af = next(afs)\n",
" if af==bf:\n",
" shared.append(af)\n",
" shared.append(bf)\n",
" af = None\n",
" bf = None\n",
" af = next(afs)\n",
" bf = next(bfs)\n",
" if af>bf:\n",
" not_shared.append(bf)\n",
" bf = None\n",
" bf = next(bfs)\n",
" except StopIteration:\n",
" pass\n",
" \n",
" if af:\n",
" not_shared.append(af)\n",
" \n",
" if bf:\n",
" not_shared.append(bf)\n",
"\n",
" for af in afs:\n",
" not_shared.append(af)\n",
"\n",
" for bf in bfs:\n",
" not_shared.append(bf) \n",
" \n",
" return prod(not_shared), prod(shared)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def start_game(nth_prime):\n",
"\n",
" some_primes = primes[:nth_prime]\n",
" number = choice(some_primes)\n",
"\n",
" print(f'starting game')\n",
" print(f'goal: nerf your number with one of the choices to try and build a number whos factors are {some_primes}\\n')\n",
"\n",
" while True:\n",
" factors = prime_factors(number)\n",
" non_factors = list(set(some_primes).difference(factors))\n",
" if not(non_factors):\n",
" print(f'you win\\nfinal number = {number}')\n",
" break\n",
"\n",
" num_factors = min(len(factors),3)\n",
"\n",
" pickfrom = [\n",
" prod(choices(non_factors, k=num_factors)), \n",
" prod(choices(factors, k=num_factors-1) + choices(non_factors, k=1)),\n",
" ]\n",
" shuffle(pickfrom)\n",
" print(f'your number: {number}\\n\\ta: nerf with {pickfrom[0]}\\n\\tb: nerf with {pickfrom[1]}\\n\\tx: give up')\n",
" resp = input()\n",
" if resp == 'x':\n",
" print('give up')\n",
" break\n",
" if resp == 'a':\n",
" number = nerf(number, pickfrom[0])[0]\n",
" if resp == 'b':\n",
" number = nerf(number, pickfrom[1])[0]"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"starting game\n",
"goal: nerf your number with one of the choices to try and build a number whos factors are [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]\n",
"\n",
"your number: 19\n",
"\ta: nerf with 11\n",
"\tb: nerf with 13\n",
"\tx: give up\n",
"a\n",
"your number: 209\n",
"\ta: nerf with 247\n",
"\tb: nerf with 35\n",
"\tx: give up\n",
"b\n",
"your number: 7315\n",
"\ta: nerf with 75\n",
"\tb: nerf with 6409\n",
"\tx: give up\n",
"b\n",
"your number: 46881835\n",
"\ta: nerf with 138\n",
"\tb: nerf with 3289\n",
"\tx: give up\n",
"b\n",
"your number: 7540435\n",
"\ta: nerf with 70\n",
"\tb: nerf with 286\n",
"\tx: give up\n",
"b\n",
"your number: 2156564410\n",
"\ta: nerf with 27\n",
"\tb: nerf with 78\n",
"\tx: give up\n",
"b\n",
"your number: 248834355\n",
"\ta: nerf with 578\n",
"\tb: nerf with 338\n",
"\tx: give up\n",
"b\n",
"you win\n",
"final number = 84106011990\n"
]
}
],
"source": [
"start_game(10)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"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.8.2"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment