show the easy way with SageMath

This commit is contained in:
filifa
2025-05-24 23:34:48 -04:00
parent 6e9f787eb5
commit c5622fc968
2 changed files with 80 additions and 15 deletions

View File

@@ -7,12 +7,48 @@
"source": [
"# [Prime Summations](https://projecteuler.net/problem=77)\n",
"\n",
"Once again, we can adapt our solution to [problem 31](https://projecteuler.net/problem=31). Here, there's the added wrinkle that we don't know how far out we need to calculate our generating function, but we can work around this by just repeatedly increasing our precision and recalculating until we find our answer."
"We can once again adapt any of our methods from [problem 31](https://projecteuler.net/problem=31), including the easiest one: just let SageMath figure it out."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "4c066047",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"71"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from itertools import count\n",
"\n",
"for n in count(1):\n",
" p = Partitions(n, parts_in=prime_range(n)).cardinality()\n",
" if p > 5000:\n",
" break\n",
" \n",
"n"
]
},
{
"cell_type": "markdown",
"id": "cd20554a",
"metadata": {},
"source": [
"Somewhat more interestingly, we could use generating functions again. This time, there's the added wrinkle that we don't know how far out we need to calculate our generating function, but we can work around this by just repeatedly increasing our precision and recalculating until we find our answer."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "1b5376e2",
"metadata": {},
"outputs": [
@@ -22,7 +58,7 @@
"1 + x^2 + x^3 + x^4 + 2*x^5 + 2*x^6 + 3*x^7 + 3*x^8 + 4*x^9 + 5*x^10 + 6*x^11 + 7*x^12 + 9*x^13 + 10*x^14 + 12*x^15 + 14*x^16 + 17*x^17 + 19*x^18 + 23*x^19 + 26*x^20 + 30*x^21 + 35*x^22 + 40*x^23 + 46*x^24 + 52*x^25 + 60*x^26 + 67*x^27 + 77*x^28 + 87*x^29 + 98*x^30 + 111*x^31 + 124*x^32 + 140*x^33 + 157*x^34 + 175*x^35 + 197*x^36 + 219*x^37 + 244*x^38 + 272*x^39 + 302*x^40 + 336*x^41 + 372*x^42 + 413*x^43 + 456*x^44 + 504*x^45 + 557*x^46 + 614*x^47 + 677*x^48 + 744*x^49 + 819*x^50 + 899*x^51 + 987*x^52 + 1083*x^53 + 1186*x^54 + 1298*x^55 + 1420*x^56 + 1552*x^57 + 1695*x^58 + 1850*x^59 + 2018*x^60 + 2198*x^61 + 2394*x^62 + 2605*x^63 + 2833*x^64 + 3079*x^65 + 3344*x^66 + 3630*x^67 + 3936*x^68 + 4268*x^69 + 4624*x^70 + 5007*x^71 + 5419*x^72 + 5861*x^73 + 6336*x^74 + 6845*x^75 + 7393*x^76 + 7979*x^77 + 8608*x^78 + 9282*x^79 + O(x^80)"
]
},
"execution_count": 1,
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
@@ -31,7 +67,7 @@
"prec = 20\n",
"while True:\n",
" R.<x> = PowerSeriesRing(ZZ, default_prec=prec)\n",
" G = 1 / prod(1 - x^p for p in primes_first_n(prec))\n",
" G = 1 / prod(1 - x^p for p in prime_range(prec))\n",
" \n",
" d = G.dict()\n",
" if any(c > 5000 for c in d.values()):\n",