Sfoglia il codice sorgente

Added solution to problem 32

Taddeus Kroes 13 anni fa
parent
commit
c80984d8de
1 ha cambiato i file con 24 aggiunte e 0 eliminazioni
  1. 24 0
      problem32.py

+ 24 - 0
problem32.py

@@ -0,0 +1,24 @@
+#!/usr/bin/env python
+from itertools import permutations
+
+def num(digits):
+    return int(''.join(map(str, digits)))
+
+N = 9
+lens = [(mc, mp, N - mc - mp)
+        for mc in range(1, N - 1)
+        for mp in range(1, N - mc)]
+mem = {}
+s = 0
+
+for seq in permutations(range(1, N + 1)):
+    for mcl, mpl, prl in lens:
+        mc = num(seq[:mcl])
+        mp = num(seq[mcl:mcl + mpl])
+        pr = num(seq[mcl + mpl:])
+
+        if mc * mp == pr and pr not in mem:
+            mem[pr] = 1
+            s += pr
+
+print s