-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp044.py
More file actions
executable file
·30 lines (24 loc) · 897 Bytes
/
p044.py
File metadata and controls
executable file
·30 lines (24 loc) · 897 Bytes
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
#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 is pentagonal and D = |Pk - Pj| is minimised; what is the value of D?
import logging
def main(args):
r = 5000
Pentagonal = []
for n in range(1,r):
Pentagonal.append(n*(3*n-1)//2)
sp = set(Pentagonal)
d = Pentagonal[-1]
for i in range(r-2):
a = Pentagonal[i]
for j in range(i+1, r-1):
b = Pentagonal[j]
if ((a+b in sp) and (b-a in sp)):
logging.debug((a, b))
if (d > b-a):
d = b-a
logging.info(d)