Skip to content

Commit b05a495

Browse files
committed
chore: add math formula for lc-1954
1 parent 8aa6828 commit b05a495

File tree

1 file changed

+26
-0
lines changed
  • solution/1900-1999/1954.Minimum Garden Perimeter to Collect Enough Apples

1 file changed

+26
-0
lines changed

solution/1900-1999/1954.Minimum Garden Perimeter to Collect Enough Apples/README_EN.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,32 @@ The perimeter is 2 * 4 = 8.
6868

6969
## Solutions
7070

71+
### Find the math formula for the number of apples inside a square with a side length L:
72+
73+
Let r be the radius, which is half the side length of the square area, there're apples planted along its perimeter:
74+
- 4 corners, each corner is $|r| + |r| = 2|r|$, so $8|r|$
75+
- middle top and middle bottom (when x axis = 0), each $|0| + |r| = |r|$, so $2|r|$
76+
- middle left and middle right (when y axis = 0), each $|0| + |r| = |r|$, so $2|r|$
77+
- between middle-top and top-right conner([1,r], [2,r], ..., [r-1,r]):
78+
79+
$|r-1||r| + (1 + 2 + ... + r-1) = |r-1||r| + |r||r-1|/2$
80+
81+
And we have 8 of them: middle-top to top-left, middle-left to top-left, ...
82+
83+
So we have: $8(|r-1||r| + |r||r-1|/2)$
84+
85+
86+
=> the total number of apples around the perimeter is:
87+
88+
$8|r| + 4|r| + 8|r-1||r| + 4|r||r-1|
89+
= 12|r| + 8r^2 - 8|r| + 4r^2 - 4|r|
90+
= 12r^2$
91+
92+
So the sum of apples inside a square with a side length L ($r = L/2$, perimeter = $L*4 = r*8$):
93+
94+
$$12\left( \sum_{k=1}^r k^2 \right) = 12\left(r(r+1)(2r+1)/6\right) = 2r(r+1)(2r+1)$$
95+
96+
7197
<!-- solution:start -->
7298

7399
### Solution 1

0 commit comments

Comments
 (0)