×

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).


Related Topics

Java Lock

A lock is indeed a threaded synchronization technique similar to Java's synchronized blocks, however, locking can be more complex. It's not like we can completely get rid of the synchronized...

5 minutes read.

Differences and Similarities between HashSet, LinkedHashSet and TreeSet in Java

HashSet Class: The HashSet is a class that executes the Setpoint of interaction. It is utilised to store the items in a hashtable; a hashtable is an information structure which holds...

10 minutes read.

Date time API in java

Introduction: In this text, we can talk approximately Data time API in java. The java.time, java.util, java.sql, and java.text packages contain classes that represent dates and times. The following classes are...

4 minutes read.

How to find length of integer in Java

We can find the length of the integer in many ways. The length of an integer is defined as the count of the number of digits for the given integer. These...

5 minutes read.

Interface Program in Java

Interface Program in Java In the previous topic, we discussed that abstraction is possible through the interface and abstract class. An abstract class provides partial to 100% abstraction. 100 %abstraction in...

4 minutes read.

Java ArraylistRemove() Time Complexity

In this tutorial, we will learn about how we can remove time complexity in Java ArrayList. But unless we will not learn what is ArrayList, we don’t understand this process....

7 minutes read.

Java 8 filters list

A stream with the components of this stream that match the given predicate is provided by the streaming filter (Predicate predicate). This process is step-by-step. Because these operations are always...

4 minutes read.

Grepcode Java util date

What is java.util.Date Class? The date and time in Java are provided through the java.util.Date class. If you imported java.util, it could be helpful. Use the Java.util.Date class to implement this class...

4 minutes read.

How to Convert String to long in Java

How to Convert String to long in java It is used when you want to perform the mathematical operation on the String which contains long number then the conversion from String...

4 minutes read.

Tetranacci Number in Java

This article mainly describes tetranacci number identification and the Java Program for Tetranacci numbers. Tetranacci number Tetranacci numbers and Fibonacci numbers are related. The key contrast is that a Tetranacci number depends...

3 minutes read.

Java Math abs() Method

The abs() method of Math class returns the absolute value of the argument where the argument can be int, double, float, long. Syntax public static int abs(int a) public static float abs(float a) public...

2 minutes read.

Applet Life Cycle in Java

In this article, we are going to acknowledge you about what a applet is, what is its life cycle and stages in life cycle, along with the syntax and example...

4 minutes read.

Literals in Java

In this article we acknowledge ourselves about the term literals in java, types of literals and an example to each of them. Literal Literal refers to any constant value that can be...

4 minutes read.

Java vs JavaScript

Java Vs JavaScript Java and JavaScript, both play an important role in the field of Computer Technology. Most people think JavaScript is a part of Java. But it is not entirely...

4 minutes read.

Set Up the Environment in Java

The Java is regarded as the pure object-oriented programming language. The Java applications are first compiled into the byte-code and then run by using the JVM. The Java is the...

4 minutes read.

TreeSet in Java

Java TreeSet with Example Java TreeSet implements the Navigable Set interface. It stores the objects in ascending order. It contains unique elements. Access and retrieval time is fast. It does not...

8 minutes read.

JDBC Architecture

JDBC: JDBC stands for Java Database Connectivity. Sun Microsystems has a specification called JDBC. JDBC is a Java API (Application Programming Interface) that enables users to interact or communicate with...

4 minutes read.

Java String equalsIgnoreCase() method

equalsIgnoreCase() method compares two Strings based on the their content but ignore the case. Syntax public boolean equalsIgnoreCase(Objects anObject) Parameter anObject: Object to be compared with the current String without case consideration. Returns It returns true...

1 minute read.

How to Convert Decimal to Hexadecimal in Java

How to Convert Decimal to Hexadecimal in Java The hexadecimal number uses 16 values to represent a number. Numbers from 0 to 9 represented by digits and the numbers from 10...

3 minutes read.

Java String replace() method

Java String replace() method returns new String by replacing old characters with new characters or old CharSequence to new CharSequence. Syntax: public String replace(char oldChar, char newChar) public String replace(CharSequence target, CharSequence replacement) Parameters: oldChar...

2 minutes read.