Skip to content

chore: add math formula for lc-1954 #3330

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,31 @@ The perimeter is 2 * 4 = 8.

## Solutions

### Find the math formula for the number of apples inside a square with a side length L:

Let r be the radius, which is half the side length of the square area, there're apples planted along its perimeter:

- 4 corners, each corner is $|r| + |r| = 2|r|$, so $8|r|$
- middle top and middle bottom (when x axis = 0), each $|0| + |r| = |r|$, so $2|r|$
- middle left and middle right (when y axis = 0), each $|0| + |r| = |r|$, so $2|r|$
- between middle-top and top-right conner([1,r], [2,r], ..., [r-1,r]):

$|r-1||r| + (1 + 2 + ... + r-1) = |r-1||r| + |r||r-1|/2$

And we have 8 of them: middle-top to top-left, middle-left to top-left, ...

So we have: $8(|r-1||r| + |r||r-1|/2)$

=> the total number of apples around the perimeter is:

$8|r| + 4|r| + 8|r-1||r| + 4|r||r-1|
= 12|r| + 8r^2 - 8|r| + 4r^2 - 4|r|
= 12r^2$

So the sum of apples inside a square with a side length L ($r = L/2$, perimeter = $L*4 = r*8$):

$$12\left( \sum_{k=1}^r k^2 \right) = 12\left(r(r+1)(2r+1)/6\right) = 2r(r+1)(2r+1)$$

<!-- solution:start -->

### Solution 1
Expand Down