Solution to problem 61 in Python
This commit is contained in:
parent
224ac76556
commit
04156e931e
@ -1,6 +1,6 @@
|
|||||||
repos:
|
repos:
|
||||||
- repo: https://github.com/pre-commit/pre-commit-hooks
|
- repo: https://github.com/pre-commit/pre-commit-hooks
|
||||||
rev: v4.0.1
|
rev: v4.3.0
|
||||||
hooks:
|
hooks:
|
||||||
- id: trailing-whitespace
|
- id: trailing-whitespace
|
||||||
- id: end-of-file-fixer
|
- id: end-of-file-fixer
|
||||||
@ -11,11 +11,11 @@ repos:
|
|||||||
hooks:
|
hooks:
|
||||||
- id: flake8
|
- id: flake8
|
||||||
- repo: https://github.com/psf/black
|
- repo: https://github.com/psf/black
|
||||||
rev: 21.12b0
|
rev: 22.6.0
|
||||||
hooks:
|
hooks:
|
||||||
- id: black
|
- id: black
|
||||||
- repo: https://github.com/pre-commit/mirrors-mypy
|
- repo: https://github.com/pre-commit/mirrors-mypy
|
||||||
rev: v0.920
|
rev: v0.961
|
||||||
hooks:
|
hooks:
|
||||||
- id: mypy
|
- id: mypy
|
||||||
additional_dependencies: [pydantic] # add if use pydantic
|
additional_dependencies: [pydantic] # add if use pydantic
|
||||||
|
88
src/Python/Problem061.py
Normal file
88
src/Python/Problem061.py
Normal 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()}")
|
Loading…
x
Reference in New Issue
Block a user