DEV Community

Discussion on: The challenge to make a shape area calculation in CodeSignal

 
equiman profile image
Camilo Martinez • Edited

I'm using a technic learned in a subject Numerical Methods. You need reproduce step by steep and try to find something familiar between each interaction.

Explained

A square have 4 sides, that's why you need multiply the value by four.

But each side merge a point with other in the other side (red circle), you can't count both. That's why yo need subtract 4, one for each corner.

That's how I found this formula: n * 4 - 4

On first and second iteration is difficult to see it... but next iterations can give you an idea about the solution.

Hope it helps.