diff --git a/src/Python/Problems001-050/Problem044.py b/src/Python/Problems001-050/Problem044.py index 5b2b1be..60a6689 100644 --- a/src/Python/Problems001-050/Problem044.py +++ b/src/Python/Problems001-050/Problem044.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python3 +#!/usr/bin/env python """ Created on 30 Aug 2021 @@ -11,38 +11,42 @@ https://projecteuler.net/problem=44 from itertools import combinations from operator import add, sub + from utils import timeit + def pentagonal(n): - return int(n*(3*n-1)/2) + return int(n * (3 * n - 1) / 2) + @timeit("Problem 44") def compute(): """ - Pentagonal numbers are generated by the formula, Pn=n(3nāˆ’1)/2. + Pentagonal numbers are generated by the formula, Pn=n(3n-1)/2. The first ten pentagonal numbers are: 1, 5, 12, 22, 35, 51, 70, 92, 117, 145, ... 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 - 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? """ - 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) for p in pairs: 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 break - - return dif + + return ans + if __name__ == "__main__": - - print(f"Result for Problem 44: {compute()}") \ No newline at end of file + print(f"Result for Problem 44 is {compute()}")