Teaser 3037: Prime advent calendar
From The Sunday Times, 6th December 2020 [link] [link]
Last year I was given a mathematical Advent calendar with 24 doors arranged in four rows and six columns, and I opened one door each day, starting on December 1. Behind each door is an illustrated prime number, and the numbers increase each day. The numbers have been arranged so that once all the doors have been opened, the sum of the numbers in each row is the same, and likewise for the six columns. Given the above, the sum of all the prime numbers is as small as it can be.
On the 24th, I opened the last door to find the number 107.
In order, what numbers did I find on the 20th, 21st, 22nd and 23rd?
[teaser3037]










Jim Randell 6:52 pm on 4 December 2020 Permalink |
I think this is the first Teaser in quite a while that has taken me more than a few minutes to solve. Fortunately all the numbers we are dealing with are different, so that simplifies things a bit.
My original program [link] was shorter, but less efficient (it runs in 3.4s). The following Python 3 program is longer, but runs in only 81ms.
Run: [ @repl.it ]
from enigma import Primes, subsets, diff, intersect, div, join, peek, sprintf, printf # choose length k sets from ns, where each set sums to t def rows(ns, k, t, ss=[]): # are we done? if not ns: yield ss else: # take the first element n = ns[0] # and k-1 other elements to go with it for s in subsets(ns[1:], size=k - 1): if sum(s) == t - n: s = (n,) + s yield from rows(diff(ns, s), k, t, ss + [s]) # make a column that sums to t, by choosing an element from each row def make_col(rs, t, s=[]): if len(rs) == 1: if t in rs[0]: yield tuple(s + [t]) else: for x in (rs[0][:1] if len(s) == 0 else rs[0]): t_ = t - x if t_ > 0: yield from make_col(rs[1:], t_, s + [x]) # make columns from the rows, where each column sums to t def cols(rs, t, ss=[]): # are we done? if not rs[0]: yield ss else: # make one column for s in make_col(rs, t): # and then make the rest yield from cols(list(diff(r, [x]) for (r, x) in zip(rs, s)), t, ss + [s]) # solve the puzzle def solve(): # possible primes primes = Primes(107) # find viable sets of primes for ps in sorted(subsets(primes, size=23), key=sum): ps += (107,) # total sum = T, row sum = R, col sum = C T = sum(ps) R = div(T, 4) C = div(T, 6) if R is None or C is None: continue printf("[T={T} R={R} C={C}]") # choose rows of 6, each sums to R for rs in rows(ps, 6, R): # select columns, each sums to C for cs in cols(rs, C): yield (ps, rs, cs) # find the first solution for (ps, rs, cs) in solve(): # output solution printf("ps = {ps} -> {T}", T=sum(ps)) printf("rs = {rs} -> {R}", R=sum(rs[0])) printf("cs = {cs} -> {C}", C=sum(cs[0])) # output solution grid for r in rs: printf("{r}", r=join(sprintf("[{x:3d}]", x=peek(intersect((r, c)))) for c in cs)) # we only need the first solution breakSolution: The numbers on the 20th, 21st, 22nd, 23rd were: 73, 79, 83, 101.
One possible layout is shown below, but there are many others:
Each row sums to 270. Each column sums to 180. Altogether the numbers sum to 1080.
I let my program look for solutions with a higher sum, and it is possible to construct a calendar for every set of primes whose sum is a multiple of 24.
LikeLike
Frits 12:02 pm on 5 December 2020 Permalink |
@Jim, you can also remove 2 from primes (as column sum needs to be even (row sum, 1.5 * column sum, must be a whole number) and there is only 1 even prime in primes).
With this, your first reported T,R,C combination can be discarded as R may not be odd (sum of 6 odd numbers is even).
I also have the same first solution but it takes a long time (mainly in checking column sums).
I first tried a program with SubstitutedExpression but I had problems with that.
LikeLike
Jim Randell 5:18 pm on 5 December 2020 Permalink |
@Frits: Thanks. I realised that 2 wasn’t going to be involved in final grid. But if you exclude it at the start, and only allow even row and column sums, then the runtime of my program goes down to 58ms. (And the internal runtime is down to 14ms).
LikeLike
Frits 8:17 pm on 5 December 2020 Permalink |
@Jim,
A further improvement could be to skip checking subsets (line 45) when testing sum 1080 as the number of primes to be used is fixed.
Sum of primes from 3 to 107 is 1369 (for 27 primes)
1369 – 1080 = 289 which can only be formed by 3 primes with (89, 97 and 103 ).
The 24 remaining primes can be used to do the rows and cols logic.
LikeLike
Jim Randell 10:15 pm on 5 December 2020 Permalink |
@Frits: I’m not sure I understand. In my program the sets of primes are tackled in sum order (to ensure we find the set with the lowest sum), so only one set of primes with sum 1080 will be checked (as there is only one set that sums to 1080).
LikeLike
Frits 11:46 pm on 5 December 2020 Permalink |
You can analyze that 1080 is the first sum to check (sum has to be a multiple of 24).
So you don’t need to execute line 45 if you first have a separate check for 1080 (with the 24 primes) and you do find an answer for it.
The disadvantage is that it will make the code less concise.
LikeLike
Frits 11:51 pm on 5 December 2020 Permalink |
Meaning:
LikeLike
Frits 10:17 am on 6 December 2020 Permalink |
A little bit different and less efficient.
from itertools import combinations as comb from itertools import product as prod from enigma import group flat = lambda group: {x for g in group for x in g} # Prime numbers up to 107 Pr = [2, 3, 5, 7] Pr += [x for x in range(11, 100, 2) if all(x % p for p in Pr)] Pr += [x for x in range(101, 108, 2) if all(x % p for p in Pr)] min1 = sum(Pr[1:24]) + 107 max1 = sum(Pr[-24:]) print(" sum row column") print("min", min1, " ", round(min1 / 4, 2), " ", round(min1 / 6, 2)) print("max", max1, " ", round(max1 / 4, 2), " ", round(max1 / 6, 2)) Pr = Pr[1:-1] # exclude 2 and 107 sumPr = sum(Pr + [107]) lenPr = len(Pr + [107]) # length Pr + [107]: 27, sum(Pr + [107]) = 1369 # # sum row column # min 1068 267.0 178.0 # max 1354 338.5 225.66666666666666 # pick one value from each entry of a <k>-dimensional list <ns> def pickOneFromEach(k, ns, s=[]): if k == 0: yield s else: for n in ns[k-1]: yield from pickOneFromEach(k - 1, ns, s + [n]) # decompose <t> into <k> increasing numbers from <ns> # so that sum(<k> numbers) equals <t> def decompose(t, k, ns, s=[], used=[], m=1): if k == 1: if t in ns and not(t in s or t in used) : if not(t < m): yield s + [t] else: for (i, n) in enumerate(ns): if not(n < t): break if n in s or n in used: continue if (n < m): continue yield from decompose(t - n, k - 1, ns[i:], s + [n], used, n) # check if sums are the same for all columns def checkColSums(rs, t): correctSumList = [p for p in prod(*rs) if sum(p) == t] uniqueFirstCols = len(set(x[0] for x in correctSumList)) if uniqueFirstCols < 6: # elements are not unique return elif uniqueFirstCols == 6: groupByFirstCol = [x for x in group(correctSumList, by=(lambda d: d[0])).values()] for p in list(pickOneFromEach(6, groupByFirstCol)): if len(set(flat(p))) == 24: yield p else: for c in comb(correctSumList, 6): if len(set(flat(c))) == 24: yield c # check sums by starting with smallest for T in {x for x in range(min1, max1 + 1) if x % 24 == 0}: dif = sumPr - T rsum = T // 4 csum = T // 6 print("\nTotal sum",T, "row sum",rsum, "col sum",csum, "difference", dif) # check which primes are to be dropped c = 0 for di in decompose(dif, lenPr - 24, Pr): if c == 0: Pr2 = [x for x in Pr if x not in di] + [107] c += 1 if c > 1: # more possibilities to drop primes Pr2 = list(Pr) print(f"\nPrimes to check={Pr2}") # first make 4 lists of 6 numbers which add up to rsum for s1 in decompose(rsum - 107, 5, Pr2, [107]): s1 = s1[1:] + [s1[0]] # put 107 at the end for s1 in decompose(rsum, 6, Pr2, s1, m=s1[0]): for s1 in decompose(rsum, 6, Pr2, s1, m=s1[6]): for s1 in decompose(rsum, 6, Pr2, s1, m=s1[12]): rs = [s1[0:6], s1[6:12], s1[12:18], s1[18:24]] # check if all columns add up to csum for cs in checkColSums(rs, csum): print("\nSolution: \n") for r in zip(*cs[::-1]): # rotate matrix for x in r: print(f"{x:>3}", end = " ") print() print("\nNumbers on the 20th, 21st, 22nd and 23rd:") print(", ".join(str(x) for x in sorted(flat(cs))[-5:-1])) exit(0)LikeLike
Jim Randell 2:30 pm on 6 December 2020 Permalink |
@Frits: I don’t think you want to use a
set()at line 70. Theset()will remove duplicates, but you are not guaranteed to get the numbers out in increasing order. In this case we know that there are no duplicates, and we definitely want to consider the numbers in increasing order (so we can be sure we have found the smallest).Changing the brackets to () or [] will do, but I think there are clearer ways to write the loop.
LikeLike
Frits 7:18 pm on 6 December 2020 Permalink |
@Jim. Thanks.
I normally run python programs with PyPy and PyPy preserves the order of dictionaries and sets.
Running with Python solved the sum 1152 and not for 1080.
LikeLike
Jim Randell 11:10 am on 7 December 2020 Permalink |
@Frits: OK. I knew about the PyPy’s behaviour for
dict(), but not forset().FWIW: I try to post code that runs on as wide a variety of Pythons as possible. I currently check against CPython 2.7.18 and 3.9.0 (although sometimes I use features that only became available in Python 3).
LikeLike
GeoffR 10:43 am on 7 December 2020 Permalink |
I found a solution in MiniZinc, but the run time was very slow (just over 5 min)
% A Solution in MiniZinc include "globals.mzn"; % grid of Advent calendar doors % a b c d e f % g h i j k l % m n o p q r % s t u v w x % set of primes, excluding 2 as non viable for this puzzle set of int: primes = {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}; set of int: Digit = 3..107; % 24 prime numbers var Digit: a; var Digit: b; var Digit: c; var Digit: d; var Digit: e; var Digit: f; var Digit: g; var Digit: h; var Digit: i; var Digit: j; var Digit: k; var Digit: l; var Digit: m; var Digit: n; var Digit: o; var Digit: p; var Digit: q; var Digit: r; var Digit: s; var Digit: t; var Digit: u; var Digit: v; var Digit: w; var Digit: x; var 0..1400: psum = sum([a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x]); constraint all_different ([a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x]); % allocate 24 primes constraint a in primes /\ b in primes /\ c in primes /\ d in primes /\ e in primes /\ f in primes; constraint g in primes /\ h in primes /\ i in primes /\ j in primes /\ k in primes /\ l in primes; constraint m in primes /\ n in primes /\ o in primes /\ p in primes /\ q in primes /\ r in primes; constraint s in primes /\ t in primes /\ u in primes /\ v in primes /\ w in primes; % put highest prime in Xmas Eve Box to fix grid constraint x == 107; % row totals add to the same value constraint (a + b + c + d + e + f) == (g + h + i + j + k + l) /\ (a + b + c + d + e + f) == (m + n + o + p + q + r) /\ (a + b + c + d + e + f) == (s + t + u + v + w + x); % column totals add to the same value constraint (a + g + m + s) == (b + h + n + t) /\ (a + g + m + s) == (c + i + o + u) /\ (a + g + m + s) == (d + j + p + v) /\ (a + g + m + s) == (e + k + q + w) /\ (a + g + m + s) == (f + l + r + x); % sum of grid primes is divisible by 12 - LCM of 4 and 6 % as 4 x row sum = psum and 6 x column sum = psum constraint psum mod 12 == 0; % minimise total sum of prime numbers used solve minimize psum; % find unused primes in original max list of primes var set of int: digits_not_used = primes diff {a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x}; % output grid and grid row and column totals output [" Grid is: " ++ "\n " ++ show([a, b, c, d, e, f]) ++ "\n " ++ show([g, h, i, j, k, l]) ++ "\n " ++ show([m, n, o, p, q, r]) ++ "\n " ++ show([s, t, u, v, w, x]) ++ "\n Prime Sum overall = " ++ show(sum([a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x])) ++ "\n Row sum = " ++ show(sum([a + b + c + d + e + f])) ++ "\n Column sum = " ++ show(sum([a + g + m + s])) ++ "\n Unused primes : " ++ show(digits_not_used) ];LikeLike
Frits 1:33 pm on 8 December 2020 Permalink |
Finding a solution takes less than a second.
I used the fact that psum has to be a multiple of 24 and has a minimum/maximum of 1080/1344.
Biggest time gain seems to have come from replacing the psum definition from the sum of 24 variables to 6 times the sum of 4 variables.
% A Solution in MiniZinc include "globals.mzn"; % grid of Advent calendar doors % a b c d e f % g h i j k l % m n o p q r % s t u v w x % set of primes, excluding 2 as non viable for this puzzle set of int: primes = {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}; % psum is a multiple of 24 as column total is a multiple of 4 % (otherwise row total would be odd which is not possible) set of int: psums = {1080, 1104, 1128, 1152, 1176, 1200, 1224, 1248, 1272, 1296, 1320, 1344}; set of int: Digit = 3..107; % 24 prime numbers var Digit: a; var Digit: b; var Digit: c; var Digit: d; var Digit: e; var Digit: f; var Digit: g; var Digit: h; var Digit: i; var Digit: j; var Digit: k; var Digit: l; var Digit: m; var Digit: n; var Digit: o; var Digit: p; var Digit: q; var Digit: r; var Digit: s; var Digit: t; var Digit: u; var Digit: v; var Digit: w; var Digit: x; %var 1080..1344: psum = sum([a, b, c, d, e, f, g, h, i, j, % k, l, m, n, o, p, q, r, s, t, u, v, w, x]); var 1080..1344: psum = 6 * (a + g + m + s); constraint psum = 4 * (a + b + c + d + e + f); constraint psum in psums; constraint all_different ([a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x]); % allocate 24 primes constraint a in primes /\ b in primes /\ c in primes /\ d in primes /\ e in primes /\ f in primes; constraint g in primes /\ h in primes /\ i in primes /\ j in primes /\ k in primes /\ l in primes; constraint m in primes /\ n in primes /\ o in primes /\ p in primes /\ q in primes /\ r in primes; constraint s in primes /\ t in primes /\ u in primes /\ v in primes /\ w in primes; % put highest prime in Xmas Eve Box to fix grid constraint x == 107; % row totals add to the same value constraint (a + b + c + d + e + f) == (g + h + i + j + k + l) /\ (a + b + c + d + e + f) == (m + n + o + p + q + r) /\ (a + b + c + d + e + f) == (s + t + u + v + w + x); % column totals add to the same value constraint (a + g + m + s) == (b + h + n + t) /\ (a + g + m + s) == (c + i + o + u) /\ (a + g + m + s) == (d + j + p + v) /\ (a + g + m + s) == (e + k + q + w) /\ (a + g + m + s) == (f + l + r + x); % minimise total sum of prime numbers used solve minimize psum; % find unused primes in original max list of primes var set of int: digits_not_used = primes diff {a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x}; % output grid and grid row and column totals output [" Grid1 is: " ++ "\n " ++ show([a, b, c, d, e, f]) ++ "\n " ++ show([g, h, i, j, k, l]) ++ "\n " ++ show([m, n, o, p, q, r]) ++ "\n " ++ show([s, t, u, v, w, x]) ++ "\n Prime Sum overall = " ++ show(sum([a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x])) ++ "\n Row sum = " ++ show(sum([a + b + c + d + e + f])) ++ "\n Column sum = " ++ show(sum([a + g + m + s])) ++ "\n Unused primes : " ++ show(digits_not_used) ];LikeLike
GeoffR 9:48 pm on 8 December 2020 Permalink |
Thanks to Frits for his optimisation of my code to make it run a lot faster.
I have added an explanation of the range calculation for psum ie 1080..1344.
I also found I could tidy code further by just using psum mod24 == 0. It was not necessary to use a list of prime sums in this revised code. It ran in about 0.6 sec.
% A Solution in MiniZinc - version 3 include "globals.mzn"; % grid of Advent calendar doors % a b c d e f % g h i j k l % m n o p q r % s t u v w x % set of primes, excluding 2 as non viable for this puzzle set of int: primes = {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}; set of int: Digit = 3..107; % The minimum prime sum = sum of first 23 + 107 = 1068 % The maximum prime sum = sum of last 24 = 1354 % The prime sum (psum) = 4 * row_sum = 6 * column_sum % But, since the row and column sums are both even, psum % is a multiple of both 8 and 12 and hence also of their % lowest common multiple 24, giving 1080 <= psum <= 1344 var 1080..1344: psum; % 24 prime numbers var Digit: a; var Digit: b; var Digit: c; var Digit: d; var Digit: e; var Digit: f; var Digit: g; var Digit: h; var Digit: i; var Digit: j; var Digit: k; var Digit: l; var Digit: m; var Digit: n; var Digit: o; var Digit: p; var Digit: q; var Digit: r; var Digit: s; var Digit: t; var Digit: u; var Digit: v; var Digit: w; var Digit: x; constraint psum = 4 * (a + b + c + d + e + f) /\ psum = 6 * (a + g + m + s) /\ psum mod 24 == 0; constraint all_different ([a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x]); % allocate 24 primes constraint a in primes /\ b in primes /\ c in primes /\ d in primes /\ e in primes /\ f in primes; constraint g in primes /\ h in primes /\ i in primes /\ j in primes /\ k in primes /\ l in primes; constraint m in primes /\ n in primes /\ o in primes /\ p in primes /\ q in primes /\ r in primes; constraint s in primes /\ t in primes /\ u in primes /\ v in primes /\ w in primes; % put highest prime in Xmas Eve Box to fix grid constraint x == 107; % row totals add to the same value constraint (a + b + c + d + e + f) == (g + h + i + j + k + l) /\ (a + b + c + d + e + f) == (m + n + o + p + q + r) /\ (a + b + c + d + e + f) == (s + t + u + v + w + x); % column totals add to the same value constraint (a + g + m + s) == (b + h + n + t) /\ (a + g + m + s) == (c + i + o + u) /\ (a + g + m + s) == (d + j + p + v) /\ (a + g + m + s) == (e + k + q + w) /\ (a + g + m + s) == (f + l + r + x); % minimise total sum of prime numbers used solve minimize psum; % find unused primes in original max list of primes var set of int: digits_not_used = primes diff {a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x}; % output grid and grid row and column totals output [" Grid is: " ++ "\n " ++ show([a, b, c, d, e, f]) ++ "\n " ++ show([g, h, i, j, k, l]) ++ "\n " ++ show([m, n, o, p, q, r]) ++ "\n " ++ show([s, t, u, v, w, x]) ++ "\n Prime Sum overall = " ++ show(sum([a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x])) ++ "\n Row sum = " ++ show(sum([a + b + c + d + e + f])) ++ "\n Column sum = " ++ show(sum([a + g + m + s])) ++ "\n Unused primes : " ++ show(digits_not_used) ];LikeLike
Frits 1:48 pm on 8 December 2020 Permalink |
Another program using many nested loops.
from itertools import product as prod # Prime numbers up to 107 Pr = [2, 3, 5, 7] Pr += [x for x in range(11, 100, 2) if all(x % p for p in Pr)] Pr += [x for x in range(101, 108, 2) if all(x % p for p in Pr)] # sum has to be a multiple of 24 # (if column sum is not a multiple of 4 then the row sum will be odd) min1 = sum(Pr[1:24]) + 107 min1 = [x for x in range(min1, min1 + 24) if x % 24 == 0][0] max1 = sum(Pr[-24:]) max1 = [x for x in range(max1 - 23, max1 + 1) if x % 24 == 0][0] Pr = Pr[1:-1] # exclude 2 and 107 sumPr = sum(Pr + [107]) lenPr = len(Pr + [107]) # make sure loop variable value is not equal to previous ones def domain(v): # find already used loop values ... vals = set() # ... by accessing previously set loop variable names for s in lvd[v]: vals.add(globals()[s]) return [x for x in Pr2 if x not in vals] # decompose <t> into <k> increasing numbers from <ns> # so that sum(<k> numbers) equals <t> def decompose(t, k, ns, s=[], used=[], m=1): if k == 1: if t in ns and not(t in s or t in used) : if not(t < m): yield s + [t] else: for (i, n) in enumerate(ns): if not(n < t): break if n in s or n in used: continue if (n < m): continue yield from decompose(t - n, k - 1, ns[i:], s + [n], used, n) # pick <k> elements from list <li> so that all combined fields are different def uniqueCombis(k, li, s=[]): if k == 0: yield s else: for i in range(len(li)): if len(s + li[i]) == len(set(s + li[i])): yield from uniqueCombis(k - 1, li[i:], s + li[i]) # check if sums are the same for all columns def checkColSums(rs, t): correctSumList = [list(p) for p in prod(*rs) if sum(p) == t] for u in uniqueCombis(6, correctSumList): yield [u[0:4], u[4:8], u[8:12], u[12:16], u[16:20], u[20:]] break # set up dictionary of for-loop variables lv = ["A","B","C","D","E","F","G","H","I","J","K","L", "M","N","O","P","Q","R","S","T","U","V","W","X"] lvd = {v: lv[:i] for i, v in enumerate(lv)} # check sums by starting with smallest for T in [x for x in range(min1, max1 + 1) if x % 24 == 0]: dif = sumPr - T rsum = T // 4 csum = T // 6 print("\nTotal sum",T, "row sum",rsum, "col sum",csum, "difference", dif) # check which primes are to be dropped c = 0 for di in decompose(dif, lenPr - 24, Pr): if c == 0: Pr2 = [x for x in Pr if x not in di] c += 1 if c > 1: # more possibilities to drop primes Pr2 = list(Pr) print(f"\nPrimes to check = {Pr2}") for A in Pr2: for B in domain('B'): if B < A: continue for C in domain('C'): if C < B: continue for D in domain('D'): if D < C: continue for E in domain('E'): if E < D: continue for F in [107]: RSUM = sum([A,B,C,D,E,F]) if RSUM < min1 // 4 or RSUM > max1 // 4 or RSUM % 6 != 0: continue for G in domain('G'): for H in domain('H'): if H < G: continue for I in domain('I'): if I < H: continue for J in domain('J'): if J < H: continue for K in domain('K'): if K < J: continue L = RSUM - sum([G,H,I,J,K]) if L < K or L not in Pr2: continue if L in {A,B,C,D,E,F,G,H,I,J,K}: continue for M in domain('M'): for N in domain('N'): if N < M: continue for O in domain('O'): if O < N: continue for P in domain('P'): if P < O: continue for Q in domain('Q'): if Q < P: continue R = RSUM - sum([M,N,O,P,Q]) if R < Q or R not in Pr2: continue if R in {A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q}: continue for S in domain('S'): for T in domain('T'): if T < S: continue for U in domain('U'): if U < T: continue for V in domain('V'): if V < U: continue for W in domain('W'): if W < V: continue X = RSUM - sum([S,T,U,V,W]) if X < W or X not in Pr2: continue if X in {A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W}: continue rs = [[A,B,C,D,E,F],[G,H,I,J,K,L], [M,N,O,P,Q,R],[S,T,U,V,W,X]] CSUM = (RSUM * 2) // 3 # select columns, each sums to CSUM for cs in checkColSums(rs, CSUM): print("\nSolution: \n") for r in zip(*cs[::-1]): # rotate matrix for x in r: print(f"{x:>3}", end = " ") print() exit(0)LikeLike
GeoffR 7:41 pm on 8 December 2020 Permalink |
I get an error on the Idle and Wing IDE’s when I try to run this code:
i.e Syntax Error: too many statically nested blocks
Is there an easy fix?
LikeLike
Jim Randell 7:59 pm on 8 December 2020 Permalink |
@Geoff: There is a limit of 20 nested loops in the standard Python interpreter. But the PyPy interpreter doesn’t have this limit, so you can use it to execute code with lots of nested loops.
LikeLike