Eularproject 42: Number of triangles decoded by the word

Source: Internet
Author: User

Coded Triangle Numbers
Problem 42
The nth term of the sequence of triangle numbers are given by, TN =? n (n+1); So the first ten triangle numbers is:

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

By converting 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 + one + + = = T10. If the word value is a triangle number then we shall call the word a triangle word.
Using Words.txt (Right click and ' Save link/target as ... '), a 16K text file containing nearly two-thousand common 中文版 wo RDS, how many is triangle words?
Answer:
162
Completed on Thu, 2015, 09:06

 fromMathImportsqrt def istrianglenumber(word):Data=0     forIinchWord:data+=ord (i)-ord (' A ')+1K=int (sqrt (data*2))ifdata*2==k* (k +1):return 1    Else:return 0Count=0 forLineinchOpen' P042_words.txt ',' R '): Line=line.strip (' \ n '). Split (",") forWordinchLine:word=word.strip ("\"") Count+=istrianglenumber (Word) print (count)

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Eularproject 42: Number of triangles decoded by word

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.