Home > Problem, Projects/Experiments, Python > Python: Triangle, Pentagonal, and Hexagonal 

Python: Triangle, Pentagonal, and Hexagonal 



Python: Triangle, Pentagonal, and Hexagonal 
Problem No.45 @ Projecteuler
Completed on: Thu, 11 Jul 2019, 21:31

Another straight-forward problem, in this task I create three functions each for Triangle, Pentagonal, and Hexagonal and we return the value of the formulas as been stated in the problem.

Using a for loop and a number range, I store the results in a list tn, pn, hn. then comparing the values in the three lists searching for same value.


The Code:


# P45
# Solved
# Completed on Thu, 11 Jul 2019, 21:31


def tn (n) :

return int(n*(n+1)/2)

def pn(n):

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

def hn (n):

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

tn_list =[]
pn_list=[]
hn_list=[]

n = 0

# Notes: I run the code for large range, but to save more time after 5000 i select +10,000 each time.

for n in range (5000,60000):

tn_list.append(tn(n))

pn_list.append(pn(n))

hn_list.append(hn(n))

print ([x for x in tn_list if x in pn_list and x in hn_list])





Follow me on Twitter..



  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s