Javatpoint Logo
Javatpoint Logo

House Numbers in Java

In this section, we are going to learn about house numbers in Java. It is a number that is comprised of a cube whose size is s + 1. On this cube, we have a square pyramid number whose side is s. The following diagram depicts a house number.

House Numbers in Java

Generally, a house number Hs is represented mathematically as:

House Numbers in Java

Implementation: Iterative

Let's see how one can implement the above-mentioned mathematical formula.

FileName: HouseNum.java

Output:

The first 10 house numbers are: 
1 9 32 78 155 271 434 652 933 1285

Time complexity: The above program uses a loop to find the value of every house number. Therefore, the time complexity of the above program is O(n2), where n is the total number of house numbers one has to compute.

Implementation: Recursive

Using recursion also, one can also find the value of the house numbers. Observe the following program.

FileName: HouseNum1.java

Output:

The first 10 house numbers are: 
1 9 32 78 155 271 434 652 933 1285

Time complexity: The above program is recursively finding the value of every house number. Therefore, the time complexity of the above program is O(n2), where n is the total number of house numbers one has to compute.

Optimized Implementation

The iteration or recursion we are using in the above two approaches to compute the value of the house numbers is the main culprit of increasing the time complexity. Therefore, we have to find a way to discard that iteration or recursion. For that, let's observe the mathematical formula again.

House Numbers in Java

The recursion or iteration is happening because the summation we are doing in the second part. If we take a closer look, we will find that the second part is nothing but the summation of the square of the first s natural numbers. Therefore,

House Numbers in Java

Let's implement the above compute mathematical formula.

FileName: HouseNume2.java

Output:

The first 10 house numbers are: 
1 9 32 78 155 271 434 652 933 1285

Time complexity: The above program is not using any recursion or iteration to find the value of every house number. Therefore, the time complexity of the above program is O(n), where n is the total number of house numbers one has to compute.

The mathematical formula still has the first part ([s + 1]3), and the second part ([s * ( s + 1) * (2 * s + 1)] / 6). However, we can solve equation A to combine all these parts.

Thus,

Hs = (s + 1)3 + ((s * (s + 1) * (2s + 1)) / 6 ............. (equation A)

Hs = (s + 1)3 + ((s * (s + 1) * (2s + 1)) / 6

Hs = (s + 1) [(s + 1)2 + (s * (2s + 1)) / 6]

Hs = (s + 1) [ s2 + 2s + 1 + ((s2 + s) * (2s + 1)) / 6]

Hs = (s + 1) [s2 + 2s + 1 + (2s2 + s) / 6]

Hs = (s + 1) [6s2 + 12s + 6 + 2s2 + s] / 6

Hs = (s + 1) [8s2 + 13s + 6] / 6 = (8s3 + 13s2 + 6s + 8s2 + 13s + 6) / 6

Hs = (8s3 + 21s2 + 19s + 6) / 6 ............. (equation B)

In equation B, all the terms have been combined and this equation can also be used to find the house numbers. The following program illustrates the same.

FileName: HouseNume3.java

Output:

The first 10 house numbers are: 
1 9 32 78 155 271 434 652 933 1285

Time complexity: The time complexity of the above program is still the same, i.e., O(n), as the program is not using any recursion or iteration to find the value of every house number. n is the total number of house numbers one has to compute.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA