A certain street has between 50 and 500 houses in a row, numbered 1, 2, 3, 4, … consecutively. There is a certain house on the street such that the sum of all the house numbers to the left side of it is equal to the sum of all the house numbers to its right. Find the number of this house.
— source
If there are k houses and we are at house n, we can relate k and n using the triangular numbers as follows:
(n-1)n/2 = k(k+1)/2 - n(n+1)/2
...
1 = (2k+1)^2 - 8n^2
This can be re-written as the following Pell equation:
1 = K^2 - 8N^2
Integer pairs (K, N) can thus be found by partially evaluating the continued fraction of sqrt(8).
This gist contains Factor code to do so.
IN: scratchpad 10 houses .
{
{ 1 1 }
{ 8 6 }
{ 49 35 }
{ 288 204 }
{ 1681 1189 }
{ 9800 6930 }
{ 57121 40391 }
{ 332928 235416 }
{ 1940449 1372105 }
{ 11309768 7997214 }
}
IN: scratchpad : triangle ( n -- n ) dup 1 + * 2/ ;
IN: scratchpad 11309768 triangle 7997214 triangle - 7997213 triangle = .
t