House Numbers in Java
In this section, we will discuss about house number in Java. It is a sum of cubes, each of which has a dimension of h + 1. There is a squared pyramid number with the side h on this cube. A home number is shown in the illustration below.
Usually, the mathematical representation of a house number Hs seems to be as follows:
Construction: Iterative
Let's look at how to put the mathematical equation given above into practice.
HouseNoDemo.java
public class HouseNoDemo
{
// a method to find the value of the house number
// located at the position h
public int findHouseNo(int h)
{
// for h == 0
// 1 is constantly in the home.
if(h == 0)
{
return 1;
}
int numb = 0;
// There are two sections to the mathematical formula.
// One is calculating the answer for (h + 1) ^ 3. A different is determining the answer of 1 ^ 2 + 2 ^ 2 + ... + h ^2
// here we are computing the first part
numb = (h + 1) * (h + 1) * (h + 1);
// Here, we're calculating the second component.
for(int k = 1; k <= h; k++)
{
numb = numb + (k * k);
}
return numb;
}
// It is the main method
public static void main(String argvs[])
{
// constructing a house number object
HouseNo sr = new HouseNo();
System.out.println("These are the first "+ 10 +" house numbers: ");
for(int k = 0; k < 10; k++)
{
System.out.print(sr.findHouseNo(k) + " ");
}
}
}
Output:
These are the first "+ 10 +" house numbers:
1 9 32 78 155 271 434 652 933 1285
Time complexity: To determine the value of each house number, the aforementioned software use a loop. Because there are so many house numbers to calculate, n, the temporal complexity of the aforementioned programme is O(n2).
Recursive implementation
One may also determine the significance of the house numbers using recursion. Take note of the program below.
HouseNoDemo2.java
public class HouseNoDemo2
{
// a method for estimating the value of a house number
// at position h or u which iteratively calculates
public int findHouseNum(int h, int u)
{
// There are two sections to the mathematical formula.
// One is calculating the answer for (h + 1) ^ 3. A different is determining
// the answer of 1 ^ 2 + 2 ^ 2 + ... + h ^2
// for h == 0
// 1 is constantly in the home.
if(h == 0)
{
// Here, we're calculating the first component.
return (u + 1) * (u + 1) * (u + 1);
}
Calculating the value of / 1 * 1 + 2 * 2 +... + h * h in a recursive manner.
return (h * h) + findHouseNo(h - 1, u);
}
// It is the main method
public static void main(String argvs[])
{
// constructing a HouseNoDemo2 class object
HouseNoDemo2 sr = new HouseNoDemo2();
System.out.println("These are the first "+ 10 +" house numbers: ");
for(int s = 0; s < 10; s++)
{
System.out.print(obj.findHouseNo(s, s) + " ");
}
}
}
Output:
These are the first "+ 10 +" house numbers:
1 9 32 78 155 271 434 652 933 1285
Time complexity: The aforementioned program iteratively calculates the value of each home number. Because there are so many home numbers to calculate, n, the temporal complexity of the aforementioned program is O(n2).
Optimized Application
The primary factor raising the temporal complexity is the repetition or recursion we utilize in the two ways described above to determine the value of a house numbers. As a result, we must figure out how to stop that recursion or iteration. Let's look at the math formula once again to see how that works.
The summing we are performing in the second portion is what causes the recursion or loop. If we examine the second portion more closely, we will see that it is nothing more than the square root of the first section's natural numbers.
Let's put the mathematical formula into practice.
HouseNoDemo3.java
public class HouseNoDemo3
{
// a technique that calculates the
// value of a house number at position h
public int findHouseNo(int h)
{
// There are two sections to the mathematical formula.
// One is calculating the answer for (h + 1) ^ 3.
// A different is determining the answer of (h * (h + 1) * (2 * h + 1)) / 6
// Here, we're calculating the first component.
int numb = (h + 1) * (h + 1) * (h + 1);
// Here, we're calculating the second component.
numb = numb + (h * (h + 1) * (2 * h + 1)) / 6;
return numb;
}
// It is the main method
public static void main(String argvs[])
{
// constructing a HouseNoDemo3 class object
HouseNoDemo3 sr = new HouseNoDemo3();
System.out.println("The first " + 10 + " house numbers are: ");
for(int s = 0; s < 10; s++)
{
System.out.print(obj.findHouseNo(s) + " ");
}
}
}
Output:
These are the first "+ 10 +" house numbers:
1 9 32 78 155 271 434 652 933 1285
Time complexity: The software mentioned above does not utilise recur or iteration to determine the value of each house number. Because there are so many home numbers to calculate, n, the temporal complexity of the above algorithm is O(n).