## Longest Consecutive Sequence Of Squares

### December 11, 2015

This is similar to our previous exercise to find the longest consecutive sequence of odd numbers that sums to a given number, and is solved the same way: Initialize `lo`

, `hi`

, and `sum`

to 1. If `sum`

is less than the target, increase `hi`

and add its square to `sum`

. If `sum`

is greater than the target, increase `lo`

and subtract its square from `sum`

. If `sum`

is equal to the target, you’re done. If the square of the `hi`

value exceeds the target before you have found a solution, then there is no solution.

(define (squares n) (define (square x) (* x x)) (let loop ((lo 1) (hi 1) (sum 1)) (cond ((< n (* hi hi)) (list)) ((< sum n) (loop lo (+ hi 1) (+ sum (square (+ hi 1))))) ((< n sum) (loop (+ lo 1) hi (- sum (square lo)))) (else (list lo hi)))))

Here is the opposite function that takes the two bounds and returns the sum of the squares within those bounds; it uses the function for the sum of the squares of the first *n* integers:

(define (sum-squares lo hi) (define (sum-squares n) (* n (+ n 1) (+ n n 1) 1/6)) (- (sum-squares hi) (sum-squares (- lo 1))))

Here are some examples:

`> (squares 595)`

(6 12)

> (sum-squares 6 12)

595

And here is A034705, which shows all the numbers that can be written as the sum of consecutive squares:

> (map (lambda (xs) (sum-squares (car xs) (cadr xs))) (filter pair? (map squares (range 300))))) (1 4 5 9 13 14 16 25 29 30 36 41 49 50 54 55 61 64 77 81 85 86 90 91 100 110 113 121 126 135 139 140 144 145 149 169 174 181 190 194 196 199 203 204 221 225 230 245 255 256 265 271 280 284 285 289 294)

You can run the program at http://ideone.com/121NjK.

in Python

In Python.

A Python solution using second order differences, so no multiplication:

Is it known whether any integer can be represented as the sum of consecutive squares in more than one way?

@Ernie: Yes; 25 can be represented by the sequences (3 4) or (5). Of these, the longest is (3 4), so it is the desired sequence. The suggested solution will always find the longest sequence because it starts from 1 and increases.

@Ernie: good question. Many numbers have 2 representations, as @praxis says, 25 is the smallest, 365 is the smallest that isn’t a perfect square.

Here’s a modified program that will show us numbers with 3 or more representations:

Output so far:

No quadruple representations so far.

Another question: do numbers with a sum of consecutive squares representation become scarcer as the numbers grow? Experimentally, they seem to, so there is a possibility there is a only a finite number (apart from the perfect squares of course).

To answer my own question: of course there are an infinite number as we can generate new ones as differences of sums of squares. Still not sure about the density. Something like N^(2/3) might be about about right (there are approximately N^(1/3) sums of squares less than N, and we can choose 2 of them to take the difference).

Here is a quadruple representation: 5545037505 (480, 1210), (3570, 3612), (3613, 3654), (7442, 7451)

@Paul: Good stuff! How did you find that one? I’m guessing by generating all differences-of-sums-of-squares directly rather than checking each number for a representation.

Did some investigation of density, N^(2/3)/N seems about right, but I suspect my reasons for coming up with it in the first were bogus.

@Paul: BTW, I think you meant 554503705. Good stuff anyway.

@Matthew. Sorry for the typo. I first generate the cumulative sum of squares. Then simply loop over the start and the end indeces and fill a dictionary. The main problem is that the memory is filled up quickly. Therefore I limit the range for the dict.

Java Script