这道题跟44还是有关系的~
44有一种说法我没有提到,那就是直接判断一个数是不是pentagonal number。
嗯……这种方法见wikipedia……具体推论方法……目测也是简单的把那个常数通过乘法变成完全平方神马的,然后开方,除法去掉。
import math
def is_triangle(n):
x = int((math.sqrt(8*n+1)-1)//2)
if x*(x+1)//2 == n:
return 1
return 0
def is_pentagonal(n):
x = int((math.sqrt(24*n+1)+1)//6)
if x*(3*x-1)//2 == n:
return 1
return 0
def main():
for i in range(144, 100000):
hexagonal = i*(2*i-1)
if is_triangle(hexagonal) and is_pentagonal(hexagonal):
print(hexagonal)
break
if __name__ == '__main__':
main()
这次的代码还是比较快的~ // 和 / 的错误也很久没犯了~好事!哈哈~
看到一个非常精彩的C++代码……忍不住copy过来了……实在是好方法……而且对于i艹和艹i 有着很深入的了解(哈哈!偷笑)
int main()
{
DWORD next_tri = 40755;
DWORD ind_tri = 285;
DWORD next_pen = 40755;
DWORD ind_pen = 165;
DWORD next_hex = 40755;
DWORD ind_hex = 143;
while(1)
{
next_tri += ++ind_tri;
if (next_tri > next_pen) next_pen += (ind_pen++*3)+1;
if (next_tri > next_hex) next_hex += (ind_hex++ *4)+1;
if(next_tri == next_pen && next_tri == next_hex)
{
cout<<"solution: "<<next_tri<<endl;
return 0;
}
}