Chinaunix首页 | 论坛 | 博客
  • 博客访问: 86645
  • 博文数量: 47
  • 博客积分: 1410
  • 博客等级: 上尉
  • 技术积分: 625
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-11 12:11
文章分类

全部博文(47)

文章存档

2008年(47)

我的朋友

分类:

2008-11-14 17:44:19

Problem 42

25 April 2003

The nth term of the sequence of triangle numbers is given by, tn = ½n(n+1); so the first ten triangle numbers are:

1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...

By converting each letter in a word to a number corresponding to its alphabetical position and adding these values we form a word value. For example, the word value for SKY is 19 + 11 + 25 = 55 = t10. If the word value is a triangle number then we shall call the word a triangle word.

Using (right click and 'Save Link/Target As...'), a 16K text file containing nearly two-thousand common English words, how many are triangle words?

def fun42():
    fp     = open("42.txt")
    words  = fp.readline().split(",")
    words  = [i[1:-1].upper() for i in words]
    result = 0
    for word in words:
        value = 0
        for i in word:
            value += (ord(i) - 64)
        value *= 2
        t      = int(math.sqrt(value))
        if t*(t+1) == value:
           result += 1
    fp.close()
    return result

answer is 162
time:0.0159997940063
阅读(453) | 评论(0) | 转发(0) |
0

上一篇:Problem 41

下一篇:Problem 43

给主人留下些什么吧!~~