House Numbers in JavaIn 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. Generally, a house number Hs is represented mathematically as: Implementation: IterativeLet's see how one can implement the abovementioned 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(n^{2}), where n is the total number of house numbers one has to compute. Implementation: RecursiveUsing 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(n^{2}), where n is the total number of house numbers one has to compute. Optimized ImplementationThe 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. 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, 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, H_{s} = (s + 1)^{3} + ((s * (s + 1) * (2s + 1)) / 6 ............. (equation A) H_{s} = (s + 1)^{3} + ((s * (s + 1) * (2s + 1)) / 6 H_{s} = (s + 1) [(s + 1)^{2} + (s * (2s + 1)) / 6] H_{s} = (s + 1) [ s^{2} + 2s + 1 + ((s^{2} + s) * (2s + 1)) / 6] H_{s} = (s + 1) [s^{2} + 2s + 1 + (2s^{2} + s) / 6] H_{s} = (s + 1) [6s^{2} + 12s + 6 + 2s^{2} + s] / 6 H_{s} = (s + 1) [8s^{2} + 13s + 6] / 6 = (8s^{3} + 13s^{2} + 6s + 8s^{2} + 13s + 6) / 6 H_{s} = (8s^{3} + 21s^{2} + 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.
Next TopicJava Program to Generate Binary Numbers
