mirror of
https://github.com/jlengrand/project_euler.git
synced 2026-03-10 00:31:21 +00:00
Eclipse integration for simpler use with Windows. Still have to perform some good work on prime numbers! Signed-off-by: Julien Lengrand-Lambert <julien@lengrand.fr>
75 lines
1.9 KiB
Python
75 lines
1.9 KiB
Python
'''
|
|
Created on 7 feb. 2012
|
|
|
|
@author: Julien Lengrand-Lambert
|
|
DESCRIPTION : Solves problem 45 of Project Euler
|
|
Triangle, pentagonal, and hexagonal numbers are generated by the following formulae:
|
|
|
|
Triangle Tn=n(n+1)/2 1, 3, 6, 10, 15, ...
|
|
Pentagonal Pn=n(3(n-1))/2 1, 5, 12, 22, 35, ...
|
|
Hexagonal Hn=n(2(n-1)) 1, 6, 15, 28, 45, ...
|
|
It can be verified that T285 = P165 = H143 = 40755.
|
|
|
|
Find the next triangle number that is also pentagonal and hexagonal.
|
|
'''
|
|
|
|
def tn(value):
|
|
"""
|
|
Returns the triangle number of the given value
|
|
"""
|
|
return value * (value + 1) / 2
|
|
|
|
def pn(value):
|
|
"""
|
|
Returns the Pentagonal number of the given value
|
|
"""
|
|
return value * ((3 * value) - 1) / 2
|
|
|
|
def hn(value):
|
|
"""
|
|
Returns the Hexagonal number of the given value
|
|
"""
|
|
return value * ((2 * value) - 1)
|
|
|
|
def tripenhex(value):
|
|
"""
|
|
Returns True of the triangle, pentagonal and hexagonal number of value are the same
|
|
"""
|
|
return (tn(value) == pn(value) == hn(value))
|
|
|
|
def big_while():
|
|
"""
|
|
Returns the value for which tm = pn = ho
|
|
"""
|
|
cpt_t = 2
|
|
cpt_p = 2
|
|
cpt_h = 2
|
|
val_t = tn(cpt_t)
|
|
val_p = pn(cpt_p)
|
|
val_h = hn(cpt_h)
|
|
|
|
res = []
|
|
while len(res) < 2: # to get the two first values
|
|
while not (val_t == val_p == val_h):
|
|
|
|
cpt_t += 1
|
|
val_t = tn(cpt_t)
|
|
|
|
# updating val_p
|
|
while val_p < val_t:
|
|
cpt_p += 1
|
|
val_p = pn(cpt_p)
|
|
|
|
# updating val_h
|
|
while val_h < val_t:
|
|
cpt_h += 1
|
|
val_h = hn(cpt_h)
|
|
res.append(val_t)
|
|
cpt_t += 1
|
|
val_t = tn(cpt_t)
|
|
|
|
return res[1] # outputs the second value
|
|
|
|
if __name__ == '__main__':
|
|
|
|
print "Answer : %d " % (big_while()) |