From 04156e931e1b75341728ab3f9c164be0eefb5a0e Mon Sep 17 00:00:00 2001 From: daviddoji Date: Sat, 16 Jul 2022 15:54:43 +0200 Subject: [PATCH] Solution to problem 61 in Python --- .pre-commit-config.yaml | 6 +-- src/Python/Problem061.py | 88 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 src/Python/Problem061.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 3ea902f..bb9d30e 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.0.1 + rev: v4.3.0 hooks: - id: trailing-whitespace - id: end-of-file-fixer @@ -11,11 +11,11 @@ repos: hooks: - id: flake8 - repo: https://github.com/psf/black - rev: 21.12b0 + rev: 22.6.0 hooks: - id: black - repo: https://github.com/pre-commit/mirrors-mypy - rev: v0.920 + rev: v0.961 hooks: - id: mypy additional_dependencies: [pydantic] # add if use pydantic diff --git a/src/Python/Problem061.py b/src/Python/Problem061.py new file mode 100644 index 0000000..ef648ed --- /dev/null +++ b/src/Python/Problem061.py @@ -0,0 +1,88 @@ +#!/usr/bin/env python3 +""" +Created on 16 Jul 2022 + +@author: David Doblas Jiménez +@email: daviddoji@pm.me + +Solution for problem 61 of Project Euler +https://projecteuler.net/problem=61 +""" + +from utils import timeit + + +@timeit("Problem 61") +def compute(): + """ + Triangle, square, pentagonal, hexagonal, heptagonal, and octagonal numbers + are all figurate (polygonal) numbers and are generated by the following + formulae: + Triangle P3,n=n(n+1)/2 1, 3, 6, 10, 15, ... + Square P4,n=n2 1, 4, 9, 16, 25, ... + Pentagonal P5,n=n(3n-1)/2 1, 5, 12, 22, 35, ... + Hexagonal P6,n=n(2n-1) 1, 6, 15, 28, 45, ... + Heptagonal P7,n=n(5n-3)/2 1, 7, 18, 34, 55, ... + Octagonal P8,n=n(3n-2) 1, 8, 21, 40, 65, ... + + The ordered set of three 4-digit numbers: 8128, 2882, 8281, has three + interesting properties. + + The set is cyclic, in that the last two digits of each number is the + first two digits of the next number (including the last number with the + first). + Each polygonal type: triangle (P3,127=8128), square (P4,91=8281), and + pentagonal (P5,44=2882), is represented by a different number in the + set. + This is the only set of 4-digit numbers with this property. + + Find the sum of the only ordered set of six cyclic 4-digit numbers for + which each polygonal type: triangle, square, pentagonal, hexagonal, + heptagonal, and octagonal, is represented by a different number in the set. + """ + + from itertools import permutations + + P = { + "3": lambda n: n * (n + 1) // 2, + "4": lambda n: n * n, + "5": lambda n: n * (3 * n - 1) // 2, + "6": lambda n: n * (2 * n - 1), + "7": lambda n: n * (5 * n - 3) // 2, + "8": lambda n: n * (3 * n - 2), + } + + # Set of values in each figurate set + dic = { + k: [str(v(i)) for i in range(1, 200) if 10**3 <= v(i) < 10**4] + for k, v in P.items() + } + + perms = permutations(P.keys(), 6) + for perm in perms: # 720 different permutations of the six polygonals + for a in dic.get(perm[0]): + lst = [] + a1, a2 = a[:2], a[2:] + lst.append(a) + for b in filter(lambda n: n[:2] == a2, dic.get(perm[1])): + lst.append(b) + b2 = b[2:] + for c in filter(lambda n: n[:2] == b2, dic.get(perm[2])): + lst.append(c) + c2 = c[2:] + for d in filter(lambda n: n[:2] == c2, dic.get(perm[3])): + lst.append(d) + d2 = d[2:] + for e in filter(lambda n: n[:2] == d2, dic.get(perm[4])): + lst.append(e) + e2 = e[2:] + for f in filter( + lambda n: n[:2] == e2 and n[2:] == a1, dic.get(perm[5]) + ): + lst.append(f) + return sum(map(int, lst)) + + +if __name__ == "__main__": + + print(f"Result for Problem 61: {compute()}")