Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Given a rectangle(or even a square of N x N) H x W, find all the possible rectangles (excluding squares) that can fit into H x W (or N x N).

My tutor spoon fed me this expression but left it up to us to figure out why and how it works.


for H = 1 to Height
   for W = 1 to Width
      totalRect += (H x W) - Math.min(H,W)
   next
next


The expression is absolutely beautiful in its simplicity and it works perfectly. But I cant figure out why or even how it works ( I can't derive it). Anyone out there not of a lesser mind that can help me figure this out?

Thanks
Posted
Updated 30-Apr-10 0:09am
v4

CyberSamuraiii wrote:
but left it up to us to figure out why and how it works.


So, figure out why and how it works. If you can't analyze existing code, you'll absolutely suck as a programmer.
 
Share this answer
 
CyberSamuraiii wrote:
The expression is absolutely beautiful in its simplicity and it works perfectly.

I bet my 20 cents it does't work as it stands. :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900