Solution to problem 61 in Python

This commit is contained in:
David Doblas Jiménez 2022-07-16 15:54:43 +02:00
parent 224ac76556
commit 04156e931e
2 changed files with 91 additions and 3 deletions

View File

@ -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

88
src/Python/Problem061.py Normal file
View File

@ -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()}")