OFFSET
0,2
LINKS
Anatoliy A. Abramov, Squares and parabola visualization.
FORMULA
a(n) = f(n) + a(n - 1), where f(n) = Sum_{i=size(n-1)..size(n)} 2*i - 1 and size(n) = floor((1 + sqrt(1 + 8*n) / 2).
EXAMPLE
Illustration for n = 0..3:
. _____________________________
/\ ^
/ \ |
/\ d/\ |
/ \/ \ |
/\ d/\ d/\ vertical |
/ \/ \/ \ limits |
\ d/\ d/\ d/ of large |
\/ \/ \/____________ square D |
\ d/\ d/ ^ |
\/ \/ vertical | |
/\ c/\ limits | |
/ \/ \ _ _ of _ _|____________v_
\ c/\ c/ large | ^
\/ \/ square C | |
/\ b/\ | vertical |
/ \/ \ __________v_ limits |
\ b/\ b/ of large |
\/ \/ square B |
\ b/ |
\/___________________________v_
/\ limits ^
/ \ of large |
\ a/ square A |
\/______________v_
.
n=0: Large square A builds from its bottom vertex at (0, 0), has diagonal size 1*2 = 2 with lateral vertices at (-1, 1) and (1, 1), and is bounded by the parabola at (-1, 1) and (1, 1). It coincides with the 1 small square a. We have 1 small square thus far, so a(0) = 1.
n=1: Large square B builds from its bottom vertex at (0, 2), has diagonal size 2*2 = 4 with lateral vertices at (-2, 4) and (2, 4), and is bounded by the parabola at (-2, 2) and (2, 2). It contains the 4 small squares b. The total number of small squares thus far is a(1) = 1 + 4 = 5.
n=2: Large square C builds from its bottom vertex at (0, 4), has diagonal size 2*2 = 4 with lateral vertices at (-2, 6) and (2, 6), and is bounded by the parabola at (-2, sqrt(6)) and (2, sqrt(6)). It contains the 3 small squares c as well as the topmost small square b (which has already been counted). The total number of small squares thus far is a(2) = 1 + 4 + 3 = 8.
n=3: Large square D builds from its bottom vertex at (0, 6), has diagonal size 3*2 = 6 with lateral vertices at (-3, 9) and (3, 9), and is bounded by the parabola at (-3, 9) and (3, 9). It contains the 8 small squares d as well as the topmost small square c (which has already been counted). The total number of small squares thus far is a(3) = 1 + 4 + 3 + 8 = 16.
PROG
(Java)
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
IntStream.rangeClosed(0, 100).forEach(it -> System.out.printf("%d, ", sumAllVisibleSquare(it)));
}
private static int maxSquareSize(int n) {
return (int) Math.floor((1 + Math.sqrt(1 + 8 * n)) / 2);
}
private static int sumVisibleSquares(int n) {
int upSquareSize = maxSquareSize(n);
int lowSquareSize = maxSquareSize(n - 1);
return IntStream.rangeClosed(lowSquareSize, upSquareSize).map(it -> it + (it - 1)).sum();
}
private static int sumAllVisibleSquare(int n) {
return n == 0 ? 1 : sumVisibleSquares(n) + sumAllVisibleSquare(n - 1);
}
}
CROSSREFS
KEYWORD
nonn
AUTHOR
Anatoliy A. Abramov, Sep 01 2023
STATUS
approved