コード例 #1
0
#===============================================================================
# What is the value of the first triangle number to have over five hundred divisors?
#===============================================================================

from Common import divisors, trianglenum

i = 1
while True:
    if len(divisors(trianglenum(i))) > 500:
        break
    i += 1
print(trianglenum(i))
コード例 #2
0
#===============================================================================
# Find the next triangle number that is also pentagonal and hexagonal after 40755.
#===============================================================================

from Common import trianglenum, pentagonnum, hexagonnum

trinums = set()
pentnums = set()
hexnums = set()
for i in range(1, 100000):
    trinums.add(trianglenum(i))
    pentnums.add(pentagonnum(i))
    hexnums.add(hexagonnum(i))
for i in trinums:
    if i > 40755 and i in pentnums and i in hexnums:
        print(i)
        break
コード例 #3
0
#===============================================================================
# Using words.txt, a 16K text file containing nearly two-thousand common English words, how many are triangle words?
#===============================================================================

from Common import trianglenum

def wordValue(word):
    return sum([ord(c) - ord('A') + 1 for c in word])

trinums = []
for i in range(1, 365):     #The longest word in the file has 14 letters, so the highest possible score is 14 * 26 = 364
    trinums.append(trianglenum(i))
words = sorted(eval('[' + open('words.txt').read() + ']'))

print(sum([1 for word in words if wordValue(word) in trinums]))