Project Euler

Problem #125

The palindromic number 595 is interesting because it can be written as the sum of consecutive squares: 6^(2) + 7^(2) + 8^(2) + 9^(2) + 10^(2) + 11^(2) + 12^(2).

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 = 0^(2) + 1^(2) has not been included as this problem is concerned with the squares of positive integers.

Find the sum of all the numbers less than 10^(8) that are both palindromic and can be written as the sum of consecutive squares.

Ruby: Running time = 1.24s
+#Enumerable

+#isPalindrome?

def p125
  limit=10**8
  ans=Set.new
  low=1
  until (low**2+(low+1)**2)>=limit
    high=low+1
    s=low**2+high**2
    until s>=limit
      ans.add(s) if isPalindrome?(s)
      s+=(high+=1)**2
    end
    low+=1
  end
  puts ans.sum
end