From 0773e06401ce7ef55d605b609278d7d699816d89 Mon Sep 17 00:00:00 2001 From: filifa Date: Sun, 20 Jul 2025 18:07:38 -0400 Subject: [PATCH] simplify code a bit --- notebooks/problem0049.ipynb | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/notebooks/problem0049.ipynb b/notebooks/problem0049.ipynb index 5ebff28..4b1af56 100644 --- a/notebooks/problem0049.ipynb +++ b/notebooks/problem0049.ipynb @@ -37,13 +37,14 @@ "metadata": {}, "outputs": [], "source": [ + "from itertools import combinations\n", + "\n", "progressions = set()\n", - "for (i, p) in enumerate(sorted(primes)):\n", - " for q in primes[i+1:]:\n", - " d = q - p\n", - " r = q + d\n", - " if r < 10000 and is_prime(r):\n", - " progressions.add((p, q, r))" + "for (p, q) in combinations(primes, 2):\n", + " d = q - p\n", + " r = q + d\n", + " if r < 10000 and is_prime(r):\n", + " progressions.add((p, q, r))" ] }, { @@ -101,13 +102,11 @@ } ], "source": [ - "from collections import Counter\n", - "\n", "for (p, q, r) in progressions:\n", " if p == 1487 and q == 4817 and r == 8147:\n", " continue\n", " \n", - " if Counter(p.digits()) == Counter(q.digits()) == Counter(r.digits()):\n", + " if sorted(p.digits()) == sorted(q.digits()) == sorted(r.digits()):\n", " break\n", " \n", "p, q, r"