DEV Community

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

Collapse
 
stxenhammer profile image
stxenhammer

But where did you get the 4 from & multiplying it ?

For example when I can recite something for 60 times in a min so if i recite for 100 mins i just do 60 * 100 to find out how many times I recite a line.
What was ur reasoning behind using the number 4 ? I am not clear on how I would go on solving this on my own. @

Thread Thread
 
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.