**Problem:**

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.

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.

**Solution:**

4782

**Code:**

The solution may include methods that will be found here: Library.java .

The solution may include methods that will be found here: Library.java .

public interface EulerSolution{

public String run();

}

/*

* Solution to Project Euler problem 25

* By Nayuki Minase

*

* http://nayuki.eigenstate.org/page/project-euler-solutions

* https://github.com/nayuki/Project-Euler-solutions

*/

import java.math.BigInteger;

public final class p025 implements EulerSolution {

public static void main(String[] args) {

System.out.println(new p025().run());

}

private static final int DIGITS = 1000;

public String run() {

BigInteger lowerthres = BigInteger.TEN.pow(DIGITS - 1);

BigInteger upperthres = BigInteger.TEN.pow(DIGITS);

BigInteger prev = BigInteger.ONE;

BigInteger cur = BigInteger.ZERO;

int i = 0;

while (true) {

// At this point, prev = fibonacci(i - 1) and cur = fibonacci(i)

if (cur.compareTo(lowerthres) >= 0)

return Integer.toString(i);

else if (cur.compareTo(upperthres) >= 0)

throw new RuntimeException("Not found");

BigInteger temp = cur.add(prev);

prev = cur;

cur = temp;

i++;

}

}

}

## No comments :

## Post a Comment