Files
contests/projecteuler/045/main.py

36 lines
954 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'''
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(3n1)/2 1, 5, 12, 22, 35, ...
Hexagonal Hn=n(2n1) 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.
'''
# Observe that every other triangle number is also a hexagonal number, so the 1st, the 3rd, 5th, etc.
# So we only need to check all the uneven n triangle numbers to be pentagonal
# In addition, we can check if a number is pentagonal by checking if sqrt(1 + 24 n) % 6 == 5
import math
def t(n):
return n * (n+1) / 2
def isPentagonal(x):
return math.sqrt(1 + 24 * x) % 6 == 5
def main():
print("Hello this is Patrick")
n = 287
while not isPentagonal(t(n)):
n += 2
print(t(n))
if __name__ == "__main__":
main()