-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathsolution_1.py
More file actions
49 lines (35 loc) · 1.21 KB
/
solution_1.py
File metadata and controls
49 lines (35 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python
# coding=utf-8
#
# Python Script
#
# Copyleft © Manoel Vilela
#
#
"""
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.
Find the pair of pentagonal numbers, Pj and Pk,
for which their sum and difference are pentagonal and D = |Pk − Pj|
is minimised; what is the value of D?"""
from itertools import combinations
from operator import add, sub
def pentagonal(n):
return (n * (3 * n - 1)) // 2
def pentagonal_list(n):
return [pentagonal(x) for x in range(1, n)]
def solution():
# lol, if i don't use 'set', the time explode!
# the most curious is the list already uniq!
# p = pentagonal_list(3000); len(p) == len(set(p)) => True!!!
# My guess is combinations had some optimizations for set
# set type doesn't ordered too...
pentagonals = set(pentagonal_list(3000))
for c in combinations(pentagonals, 2):
if add(*c) in pentagonals and abs(sub(*c)) in pentagonals:
return abs(sub(*c))
if __name__ == '__main__':
print(solution())