×

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

Group by in Java 8

By using this groupingBy() method, developers can directly able to perform the "GROUP BY" operation. The thing is, now, the Java 8 programming language allows the programmers to do this...

3 minutes read.

Singleton Design Pattern in Java

In the singleton pattern, a class that only has one instance and offers a universal point of access is taken into consideration. It can also be defined in another way, a...

4 minutes read.

Union in Java

Sets.union() method in Java returns an immutable representation of both the union of two sets. Every element that is present in either backup set is included in the set that...

2 minutes read.

Java Xml Parser

In this article, we acknowledge you about what is a XML parser in Java, how is it going to work and what is the purpose of it. Also, the article discusses...

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

Convert JSON to Map in Java

JACKSON and GSON libraries are two extremely capable JSON-related libraries offered by Java. To efficiently work with the returned JSON data, we frequently need to convert JSON responses into a...

6 minutes read.

Manachers Algorithm in Java

Here, we'll go over the four scenarios once more in an effort to approach them differently and use the same strategy. The values of (centerRightPosition - currentRightPosition) and LPS length at...

3 minutes read.

Pyramid Program in Java

Pyramid Program in Java In the previous section, we have discussed about the number pattern programs in Java. The logic for the number pattern and pyramid pattern is the same except...

2 minutes read.

Java Integer numberOfTrailingZeros() method

The numberOfTrailingZeros()  method of Java Integer class returns the total number of zero bits following the lowest-order one-bit in the 2’s complement binary representation of the specified int value. Syntax public static...

1 minute read.

Java OCR

In this article, you will be acknowledged about what is a tesseract OCR, how it works, what are its used and advantages and disadvantages. Also, you will be able to...

4 minutes read.

Lambda expressions in Java

A brief introduction to Lambda expression in java In this topic, we will discuss the lambda expression in java. A lambda expression in Java is an enhanced version of an anonymous...

13 minutes read.

What is interpreter in Java?

The programming language Java is platform-neutral. Therefore, can use Java on any platform that supports the Java processor. The Piece of software transforms the Java bytecode contained in the class...

5 minutes read.

Trim Method in String Java

What is a String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java programming language. “String” is a...

3 minutes read.

Heart Pattern in Java

Heart Pattern is yet another intricate pattern program, however, due to its complexity, interviewers hardly ever inquire about it. Method for Printing the Heart Number Pattern Put the value of the total row...

2 minutes read.

Java Open File

Java Desktop class give an open() strategy to open a filr. It has a place with a java.awt package. Work area execution is stage subordinate, so it is important to...

5 minutes read.

Java Boolean logicalAnd() Method

The logicalAnd() method of Java Boolean class returns the result of implementing logicalAND operation on the specified Boolean operands. Syntax:public static boolean logicalAnd (boolean a, boolean b) Parameters:The parameters ‘a’ and ‘b’...

2 minutes read.

How to create a mirror image of a 2D array in Java

Problem Statement We have provided a list of m x n. here m indicates rows, and n indicates columns). Printing the fresh matrices should result in a mirror reflection of an...

2 minutes read.

How to Read CSV Files in Java?

Comma-Separated Values is the acronym for this format. It is a straightforward file format storing textual tabular data in a database or spreadsheet. The CSV files can be imported into...

3 minutes read.

Java Coding Software

Desktop and web apps are created using Java, an object-oriented programming language. Java code may be executed on any platform, making it platform-independent. A text editor, tool, or piece of...

9 minutes read.

Basic Terms in Multithreading

To understand the terms of multithreading, we must have knowledge about concurrency, processes, and threads. Concurrency The concurrency stands for performing multiple tasks at the same time. In the process communication, the operating system permits the process...

8 minutes read.