Adopted new convention from template

This commit is contained in:
David Doblas Jiménez 2022-10-02 18:37:14 +02:00
parent 0d15cfab5d
commit e74454e4f5

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python3 #!/usr/bin/env python
""" """
Created on 30 Aug 2021 Created on 30 Aug 2021
@ -11,38 +11,42 @@ https://projecteuler.net/problem=44
from itertools import combinations from itertools import combinations
from operator import add, sub from operator import add, sub
from utils import timeit from utils import timeit
def pentagonal(n): def pentagonal(n):
return int(n*(3*n-1)/2) return int(n * (3 * n - 1) / 2)
@timeit("Problem 44") @timeit("Problem 44")
def compute(): def compute():
""" """
Pentagonal numbers are generated by the formula, Pn=n(3n1)/2. Pentagonal numbers are generated by the formula, Pn=n(3n-1)/2.
The first ten pentagonal numbers are: The first ten pentagonal numbers are:
1, 5, 12, 22, 35, 51, 70, 92, 117, 145, ... 1, 5, 12, 22, 35, 51, 70, 92, 117, 145, ...
It can be seen that P4 + P7 = 22 + 70 = 92 = P8. However, their It can be seen that P4 + P7 = 22 + 70 = 92 = P8. However, their
difference, 70 22 = 48, is not pentagonal. difference, 70 - 22 = 48, is not pentagonal.
Find the pair of pentagonal numbers, Pj and Pk, for which their Find the pair of pentagonal numbers, Pj and Pk, for which their
sum and difference are pentagonal and D = |Pk Pj| is minimised. sum and difference are pentagonal and D = |Pk - Pj| is minimised.
What is the value of D? What is the value of D?
""" """
dif = 0
pentagonal_list = set(pentagonal(n) for n in range(1,2500)) ans = 0
pentagonal_list = set(pentagonal(n) for n in range(1, 2500))
pairs = combinations(pentagonal_list, 2) pairs = combinations(pentagonal_list, 2)
for p in pairs: for p in pairs:
if add(*p) in pentagonal_list and abs(sub(*p)) in pentagonal_list: if add(*p) in pentagonal_list and abs(sub(*p)) in pentagonal_list:
dif = (abs(sub(*p))) ans = abs(sub(*p))
# the first one found would be the smallest # the first one found would be the smallest
break break
return dif return ans
if __name__ == "__main__": if __name__ == "__main__":
print(f"Result for Problem 44 is {compute()}")
print(f"Result for Problem 44: {compute()}")