Skip to content

Instantly share code, notes, and snippets.

@alaingilbert
Created August 22, 2012 01:01
Show Gist options
  • Save alaingilbert/3421059 to your computer and use it in GitHub Desktop.
Save alaingilbert/3421059 to your computer and use it in GitHub Desktop.
How many squares question...
# http://www.quora.com/Puzzle-and-Trick-Questions/How-many-squares-are-in-this-picture
def get_square_count(length):
squares = 0
nb_nodes = length ** 2
for i in range(nb_nodes):
tmp = i + 1
while tmp % 5 != 0 and tmp + ((tmp - i) * length) < nb_nodes:
tmp += 1
squares += 1
return squares
print 'Response: %s' % (get_square_count(5) + 2 * get_square_count(3))
# Response: 40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment