Please suggest a better algorithm..
The problem is to find the no. of 3x3 matrices of perfect squares....like:
whose transpose is same as the original..
The solution I came up with is:
1.Make an array of vectors of the sq nos within the range..(such that..each array contains a vector of the no.s beginning with its(the array's) index...)
2.Iterate over the square nos in the range
--1.Put the current no. as the first row.
--2.Iterate over the set(that array of vectors!) which begins with the middle digit of the first row.
----1.Put the current no. as the 2nd row.
----2..Iterate over the set which begins with the 3rd digit of the first row.
------1.Put the no as the 3rd row.
------2.If the 3rd digit of the 2nd row == 2nd digit of the 3rd row.
----------<You have found one of such a matrix>
3.Output ..etc etc..
It works...as expected..and finds out all of the matrices..
..But the 2 nested loops feels very ..err...clumsy ...take a look..
..... I figured out(not having a good idea about the mathematical aspects of algorithms..)..that this is better than iterating over all(more than 20!) the possible matrices..
IS there a better and simpler way of doing this...??