|
Question:
Perfect Squares
There are many examples of the sum of the squares of two consecutive
integers being a perfect square. For example, 3^2 + 4^2 = 5^2 and
20^2 + 21^2 = 29^2. Find an example of another k (k > 2) such that the
sum of the squares of k consecutive integers is a perfect square.
Answer:
Consider the general case for a solution to be of the form:
n^2 + (n+1)^2 + (n+2)^2 +... = k^2
If we expand each of these squares we get:
n^2 + n^2+2*n+1 + n^2+4*n+4 + ... = k^2
And we can collect them into the form:
a*n^2 + b*n + c = k^2 and
k = sqrt(a*n^2 + b*n + c), noting that k can never be negative
Now all this takes is a simple plugging in of numbers, simple being a =
relative term. As you will see by the solution, doing this by hand is =
not feasible. However, using a computer it is all too easy. So let's =
write the code, in c++:
/*code begins. Pardon my spacing, VC++ copies spaces funny, so I edited
them*/
#include
#include
int main()
{
int a = 3; /*a,b,c,n are all as listed in the equation*/
int b = 6;
int c = 5;
int n = 1;
int i = 3; /*the counter for the number of consecutive
integers loop*/
int hold = 0; /*the variable to hold the potential perfect
square result*/
=20
for(i=3;i<100;i++) /*cycles from 3 to 100 consecutive
integers*/
{
for(n=1;n<1000;n++) /*attempts values of n from 1 to
1000*/
{
hold = (int)pow(a*n*n+b*n+c,.5); /*sets hold to
the truncated square root of the equation*/
if(hold*hold ==3D a*n*n+b*n+c) /*checks to
see if the square of the truncated result is equal to the value for k^2.
If so, print out k, n and the number of=20
consecutive integers to add, otherwise it checks the next one*/
cout<
-- Stefan Dorsett
|