Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Im not able to get area from n polygon in java

If you can explain to me, not only solve it, it will really be incredible.

FIRST: It's an exercise that my teacher gave me, it really does not have any value in my grades, but I'm trying to solve it, and I can't when I do the tests says

Input: n: 3 Output: 10 Expected Output: 13 Console Output: Empty

Here is the question:

Below we will define an n-interesting polygon. Your task is to find the area of a polygon for a given n.

A 1-interesting polygon is just a square with a side of length 1. An n-interesting polygon is obtained by taking the n - 1-interesting polygon and appending 1-interesting polygons to its rim, side by side. You can see the 1-, 2-, 3- and 4-interesting polygons in the picture below.enter image description here

like image 462
Csanchez Avatar asked Dec 14 '25 10:12

Csanchez


1 Answers

You can do it in the simplest way. I set down calculations and found that for:

  • n = 1 => 1
  • n = 2 => 5
  • n = 3 => 13
  • n = 4 => 25
  • n = 5 => 41
  • n = 6 => 61 So when I did a calculation of the square of each number:
  • n = 1 => 1 -> 1*1 = 1
  • n = 2 => 5 -> 2*2 = 4, 5-4 = 1
  • n = 3 => 13 -> 3*3 = 9, 13-9=4
  • n = 4 => 25 -> 4*4 = 16, 25-16=9
  • n = 5 => 41 -> 5*5 = 25, 41-25=16
  • n = 6 => 61 -> 6*6 = 36, 61-36=25

Then I figured out formula : n^2 + (n-1)*(n-1)

like image 185
Rijad Muhic Avatar answered Dec 16 '25 00:12

Rijad Muhic