EularProject 42:单词解码出来的三角形数
Coded triangle numbers
Problem 42
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 words.txt (right click and ‘Save Link/Target As…’), a 16K text file containing nearly two-thousand common English words, how many are triangle words?
Answer:
162
Completed on Thu, 30 Jul 2015, 09:06
from math import sqrt
def IsTriangleNumber(word):
data=0
for i in word:
data+=ord(i)-ord('A')+1
k=int(sqrt(data*2))
if data*2==k*(k+1):
return 1
else:
return 0
count=0
for line in open('p042_words.txt','r'):
line=line.strip('\n').split(",")
for word in line:
word=word.strip("\"")
count+=IsTriangleNumber(word)
print(count)
EularProject 42:单词解码出来的三角形数的更多相关文章
- hdu 5312 Sequence(数学推导——三角形数)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5312 Sequence Time Limit: 2000/2000 MS (Java/Others) ...
- 三角形数且是完全平方数 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛 E.Half-consecutive Numbers
三角形数:an=n*(n+1)/2; 完全平方数:bn=c^2; 既是三角形数又是完全平方数:An=6*A(n-1)-A(n-2)+2; A[23]={ 0, 1, 8, 49, 288, 1681, ...
- Python练习题 040:Project Euler 012:有超过500个因子的三角形数
本题来自 Project Euler 第12题:https://projecteuler.net/problem=12 # Project Euler: Problem 12: Highly divi ...
- [ACM_动态规划] 数字三角形(数塔)
递归方法解决数塔问题 状态转移方程:d[i][j]=a[i][j]+max{d[i+1][j],d[i+1][j+1]} 注意:1\d[i][j]表示从i,j出发的最大总和;2\变界值设为0;3\递归 ...
- c语言统计一个文件中的单词,字符和行数
body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...
- 数字三角形/数塔问题(DP入门题)
有形如下图所示的数塔,从顶部出发,在每一结点可以选择向左走或是向右走,一起走到底层,要求找出一条路径,使路径上的值最大. 样例输入: 5 13 11 8 12 7 26 6 14 15 8 12 7 ...
- [ACM_动态规划] 数字三角形(数塔)_递推_记忆化搜索
1.直接用递归函数计算状态转移方程,效率十分低下,可以考虑用递推方法,其实就是“正着推导,逆着计算” #include<iostream> #include<algorithm> ...
- 【计算几何】【极角序】【二分】bzoj1914 [Usaco2010 OPen]Triangle Counting 数三角形
极角排序后枚举每个点,计算其与原点连线的左侧的半平面内的点与其组成的三角形数(二分/尺取),这些都不是黄金三角形. 补集转化,用平面内所有三角形的个数(C(n,3))减去这些即可. 精度很宽松,几乎不 ...
- [CQOI2014]数三角形 题解(找规律乱搞)
题面 其实这道题不用组合数!不用容斥! 只需要一个gcd和无脑找规律(滑稽 乍一看题目,如果单纯求合法三角形的话情况太多太复杂,我们可以从局部入手,最终扩展到整体. 首先考虑这样的情况: 类似地,我们 ...
随机推荐
- 洛谷 P1313 计算系数 (二项式定理)
这道题正好复习了二项式定理 所以答案就是a^n * b^m * c(n, k) 然后注意一些细节 我一开始写组合数只写一行的组合数 即c[0] = 1; c[i] = c[i-1] * (n - i ...
- WinServer-IIS-请求筛选
这个实在太多了,可以比较好的控制网站的访问 来自为知笔记(Wiz)
- 学一下HDFS,很不错(大数据技术原理及应用)
http://study.163.com/course/courseMain.htm?courseId=1002887002 里面的HDFS这一部分.
- IntegerToBinaryString
IntegerToBinaryString 方法写的非常的巧妙:佩服佩服! package com.stono.jdk; public class IntegerToBinaryString { pu ...
- IIS身份验证的配置
前4者配置:localhost applicationHost.config <location path=""> 后2者配置:web.config 要点: 这6项尽管 ...
- iOS开发—在@interface,@implementation和@property中变量的定义
一直搞不懂在OC中变量在@interface和@implementation中有什么区别,定义@property又有什么不同,查了很多资料,总结如下: //ViewController.h @inte ...
- RAC IP 地址修改
RAC 修改IP: 版本信息: REDHAT AS5 ORACLE 11G R2, 两个节点. 修改如下: 1.修改/etc/host(rac1,rac2) 192.168.3.205 rac-s ...
- 带你玩转Visual Studio——带你理解多字节编码与Unicode码
目录(?)[-] 多字节字符与宽字节字符 char与wchar_t string与wstring string 与 wstring的相关转换 字符集Charcater Set与字符编码Encoding ...
- [poj3974] Palindrome 解题报告 (hash\manacher)
题目链接:http://poj.org/problem?id=3974 题目: 多组询问,每组给出一个字符串,求该字符串最长回文串的长度 数据范围支持$O(nlog n)$ 解法一: 二分+hash ...
- 17.广度优先遍历bfs
#include <iostream> #include <boost/config.hpp> //图(矩阵实现) #include <boost/graph/adjac ...