欧拉计划 发表于 2016-8-23 16:44:22

题目125:找出是回文的平方数之和

Palindromic sums

The palindromic number 595 is interesting because it can be written as the sum of consecutive squares: 62 + 72 + 82 + 92 + 102 + 112 + 122.

There are exactly eleven palindromes below one-thousand that can be written as consecutive square sums, and the sum of these palindromes is 4164. Note that 1 = 02 + 12 has not been included as this problem is concerned with the squares of positive integers.

Find the sum of all the numbers less than 108 that are both palindromic and can be written as the sum of consecutive squares.

题目:

595 这个回文数很有趣,它可以被写作连续数字的平方数之和:62 + 72 + 82 + 92 + 102 + 112 + 122。

1000 以下一共有 11 个这样可以写作连续数字平方和的回文数,这 11 个数字的和是 4164。注意 1 = 02 + 12 没有被算作在内,因为该题目只考虑正整数的平方和。

求 108 以下所有1). 既是回文; 2). 又可以写成连续数字平方和,的数字之和。

迷雾少年 发表于 2016-8-31 17:36:01

这个比前面的简单

jerryxjr1220 发表于 2016-11-29 22:01:48

迷雾少年 发表于 2016-8-31 17:36
这个比前面的简单

确实比较简单
#coding:utf-8
def is_cycle(n):
    return True if str(n) == str(n)[::-1] else False

limit = 10**8
window_s = 1
window_e = int(limit ** 0.5)
lst = []
while window_s<window_e:
    s = window_s*window_s
    for i in range(window_s+1,window_e):
      s += i*i
      if s >= limit:
            break
      if is_cycle(s):
            lst.append(s)
    window_s += 1
print (sum(set(lst)))
输出:
2906969179
页: [1]
查看完整版本: 题目125:找出是回文的平方数之和