Centered Square Numbers in Java

In this section, we will learn what is a centered square number and also create Java programs to check if the given number is a centered square number or not. The centered square number program is frequently asked in Java coding interviews and academics.

Centered Square Numbers

Centered Square numbers are the figurate numbers that are recursively defined as:

Thus,

For n = 1

Q(1) = Q(1 - 1) + 4 x (1 - 1) => Q(1) = Q(0) + 4 x 0 = 1 + 0 = 1

For n = 2

Q(2) = Q(2 - 1) + 4 x (2 - 1) => Q(2) = Q(1) + 4 x 1 = 1 + 4 = 5

For n = 3

Q(3) = Q(3 - 1) + 4 x (3 - 1) => Q(3) = Q(2) + 4 x 2 = 5 + 8 = 13

For n = 4

Q(4) = Q(4 - 1) + 4 x (4 - 1) => Q(4) = Q(3) + 4 x 3 = 13 + 12 = 25

For n = 5

Q(5) = Q(5 - 1) + 4 x (5 - 1) => Q(5) = Q(4) + 4 x 4 = 25 + 16 = 41

Recursive Approach

In this approach, we are going to write a method that will call itself (recursion) to compute the centered square number. The base case for terminating the recursion is Q(1) = 1. Observe its implementation mentioned below.

FileName: CenteredSquareNumber.java

Output:

```The first 10 Centered Square Number are:
1 5 13 25 41 61 85 113 145 181
```

Complexity Analysis: The time complexity of the program O(d), where d is the dth position of the centered square number that has to be found. The program is not consuming any extra space. Hence, the space complexity is O(1).

Approach: Using Iteration

The space complexity of the above program can be reduced further. We will use an array to store the result of the previously computed centered square numbers. Thus, it is not required to compute the previously computed solution again and again. Observe its implementation mentioned below.

FileName: CenteredSquareNumber1.java

Output:

```The first 10 Centered Square Number are:
1 5 13 25 41 61 85 113 145 181
```

Complexity Analysis: The time complexity of the program is O(1). The program uses an array to store the result. Therefore, the space complexity of the program is O(n), where n is the total number centered square numbers that have to be computed.

We can still do optimization in terms of space complexity. If we observe the above recursive formula, we find that the nth centered square number is only dependent on the (n - 1)th centered square number. Thus, there is no need to store all of the previously computed centered square numbers. Simply storing the last computed centered square number will do the job. The following program shows the same.

FileName: CenteredSquareNumber2.java

Output:

```The first 10 Centered Square Number are:
1 5 13 25 41 61 85 113 145 181
```

Complexity Analysis: The time complexity of the program is O(1). The program is not using an array to store the result. Therefore, the space complexity of the program is also O(1).

Approach: Using Mathematical Formula

The mathematical formula for computing the centered square numbers is

Q(n) = n2 + (n - 1)2, where n >= 1

Thus,

For n = 1

Q(1) = 12 + (1 - 1)2 => Q(1) = 1 + 02 = 1 + 0 = 1

For n = 2

Q(2) = 22 + (2 - 1)2 => Q(2) = 4 + 12 = 4 + 1 = 5

For n = 3

Q(3) = 32 + (3 - 1)2 => Q(3) = 9 + 22 = 9 + 4 = 13

For n = 4

Q(4) = 42 + (4 - 1)2 => Q(4) = 16 + 32 = 16 + 9 = 25

For n = 5

Q(5) = 52 + (5 - 1)2 => Q(5) = 25 + 42 = 25 + 16 = 41

The implementation of the mentioned mathematical formula is quite straight forward.

FileName: CenteredSquareNumber3.java

Output:

```The first 10 Centered Square Number are:
1 5 13 25 41 61 85 113 145 181
```

Complexity Analysis: The time as well as space complexity of the program is the same as the previous program.

Check for the Centered Square Numbers

Till now, we have always been computing the centered square numbers. Now, we will see how to validate whether the given number is a centered square number or not.

Approach

The approach is to compute the centered square number till the input number or greater than the input number (inputNum in our case). We can do the same with the help of the triangular numbers. Observe the following steps.

Step 1: First, we need to compute the triangular number.

Step 2: Then, multiply the computed triangular number with the number 4.

Step 3: After that add 1 to the result. The number we get is the centered square number.

Step 4: If the computed number found in the previous step matches with the inputNum, then inputNum is the centered square number. Else, compute the next greater triangular number and repeat from step 2. If the computed centered becomes greater than the inputNum, then it means inputNum is not a centered square number.

Note: Triangular Number is T(n) = (n x (n + 1)) / 2, where n >= 1

FileName: CenteredSquareNumber4.java

Output:

```4 is not a centered square number.
5 is a centered square number.
8 is not a centered square number.
13 is a centered square number.
15 is not a centered square number.
25 is a centered square number.
40 is not a centered square number.
41 is a centered square number.
```

Complexity Analysis: The time complexity of the program is O(N), where N is the value of the centered square number that has to be validated. The space complexity of the program is constant, i.e., O(1).