Archive

Archive for July 14, 2019

Python: Pentagon Numbers



Python: Pentagon Numbers
Problem No.44 on ProjectEuler
Completed on: Thu, 11 Jul 2019, 18:37

This problem talking about the Pentagonal numbers and gives us a formula. Using that formula for a certain range of numbers, the generated sequence showing that P4 + P7 = 22 + 70 = 92, 92 is the P8, but if we subtracting (P7 – P4) = 70 – 22 = 48, 48 is not in the generated sequence of pentagonal numbers, so 48 is not pentagonal.

The task here is to find the pair of pentagonal Pj,Pk which their sum and difference are Pentagonal D = Pk – Pj is minimised.(we need to get the D).



The Code:




# P44
# Pentagon Numbers
# Solved
#Completed on Thu, 11 Jul 2019, 18:37


def pn(n):

return int(n*(3*n-1)/2)

pn_list=[]

for n in range (1000,3000) : # I start increasing the range step by step.

pn_list.append(pn(n))

we_found_it = False
for x in range (0,len(pn_list)-1) :

px= pn_list[x]

for y in range (x+1,len(pn_list)-1) :

py= pn_list[y]

if (px+py) in pn_list:

if (py-px) in pn_list:

print(‘\n We found one ‘,px,py,’D = ‘,py-px )

we_found_it = True

if we_found_it : break

print(‘Done’)






Follow me on Twitter..



Advertisements