My first question: how would one solve such problems (in general)? What should be the general technique?
My second question: When to be sure that we have counted all the squares in such problems?
Puzzling Stack Exchange is a question and answer site for those who create, solve, and study puzzles. It only takes a minute to sign up.
Sign up to join this communityYou need to find some systematic technique. The technique will depend on the problem. Usually there is a regularity to the problem that you can exploit. In this case, I would note that the two squares that are not part of the main $4 \times 4$ do not interact with the lattice, so we can count them separately. For the lattice, work by square size.
You have four choices along each edge for the upper left corner of a $1 \times 1$ square for
$16$ in total.
Similarly there are
$9\ 2 \times 2$ squares, $4\ 3 \times 3$ squares and $1\ 4 \times 4$ for a total of $30$.
You might even know that in an $n \times n$ chessboard there are $\sum_{i=1}^ni^2=\frac 16n(n+1)(2n+1)$ squares.
Each of the stray squares gives us five, for a total
$40$.
Systematic technique is definitely required. I came across this particular puzzle a few years ago on facebook and got into arguments with some people about the answer, with them arguing that there were
41, 42
or even more squares. Eventually I got bored, produced this animation
which shows 40 different squares, and challenged them to show me one that I'd not highlighted.
The general case would be similar - find the recurring pattern, and count them methodically.
As to knowing if you've "found them all", that's just down to being rigorous and methodical in your search. This can get difficult if the puzzle is particularly complex or large, but if the answer is in the realms of "easily countable" as it is in this case then this is easily verified manually.
One potential strategy is as follows:
Call this example an square of dimentions 8x8
By this strategy I get:
40 squares
Of course I agree that being systematic is helpful for this sort of puzzle, but I take the original questioner to have been asking also for help with how to be systematic: how to make sure you haven't somehow missed a square. Here is one suggestion, which applies best to cases (like this one) where all the squares have to have the same orientation because of a grid of lines already present in the image.
A square is then determined by its top left corner and its size. So, when you find a square, draw a line on the diagram running from the top left corner to (let's say) somewhere a little down from the top right corner. Now, when you think you have found them all, you can go back and (e.g.) at each available vertex -- each point that could be the top left corner of a square -- check that you have drawn all possible such lines.
Another strategy would be as follows:
This gives us (16+9+4+1)=30 squares.
Now add in the smaller squares:
This gives us 10 more squares. Add those answers and we get 40.
I believe the 98% get it wrong because of the fact that the question itself is a square. They count the 40 squares created by the green lines, but they miss the pink square. @ClickRick, I believe those that say 42 are correct: the pink area makes 41, the embossed rim would make 42. However, if you wanted to go further, you could note that the background is two different colors; pink and purple, slightly different tones. I count at least 13 purple squares in the background, and at least 2 pink ones. The problem is that the rules are not clearly defined; 40 is correct from a basic standpoint, looking at only the green lines. However, since 98 percent get it wrong, and it is a puzzle after all, I must believe that the squares that exist outside of the line are meant to be counted. My guess is that a large portion of the 98 percent probably say 40. In your simulation, there are only 40. But I believe that there is some reason for the pink background, rather than a plain white one - to complicate things, creating additional inconspicuous squares.
If I have interpreted the question properly, you are asking:
After counting the obvious $40$ squares, how do I convince myself that there are no others?
We can (hopefully) ignore the 'trick' aspects such as the surrounding box - the puzzle might have been hosted on a page containing lots of squares as decoration, and questions that expect this as an answer are misleading.
So we are left with 'how do I know that is it?' My solution is to stare at a puzzle until I become convinced that this is the case. This might take hours, or only minutes. Remove all the squares you have accounted for - what is left? Where might any other squares be? Eventually you can make yourself believe that $40$ is the actual answer, and your search is complete.
Answer to question 1 and 2: Use brute force, that means test all possible combinations.
You will need a definition to check if a point exists and if a line exists.
In Pseudocode that looks something like this:
function get_square_count()
counter = 0
for each a in corners:
for each b in corners:
//same height level and b right of a
if a.x=b.x and a.y<b.y:
if is_square_top_line(a,b):
counter++
return counter
function is_square_top_line(a,b)
// use square property to add height
calculate virtual points c,d
check if c and d exist
check if all lines exists a,b b,c c,d, d,a