Consider:
I am trying to find the area of an n-interesting polygon, where (n=1, A=1, n=2, A=5, n=3, A=13, n=4, A=25, and so on). So the formula for an n-interesting polygon is the area of an (n-1)-interesting polygon+(n-1)*4. When running the program, a hidden test shows that the code is wrong. What is wrong with my code?
def shapeArea(n):
if n == 0:
return 0
if n == 1:
return 1
for i in range(2, n+1):
return (shapeArea(n-1) + (n-1)*4)
I found the formula without the recursion. The test went through fine.
def shapeArea(n):
if n>=10**4 or n<1:
return False
return (n**2+(n-1)**2)
As there are already coding examples, I will explain why the formula is n * n + (n-1) * (n-1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With