×

Hogben Numbers in Java

In this section, we will discover what the Hogben number is and develop Java programs that compute it. Java coding interviews and academic exams typically involve questions about the Hogben number program.

Hogben Number

The numbers that are recursively defined as follows are the Hogben numbers.

Where n >= 1 and H(0) = 1, H(n) = H(n-1) + 2 * (n-1)

Thus,

H(1) = H(1 - 1) + 2 * (1 - 1) = H(0) + 2 * 0 = 1 + 0 = 1

H(2) = H(2 - 1) + 2 * (2 - 1) = H(1) + 2 * 1 = 1 + 2 = 3

H(3) = H(3 - 1) + 2 * (3 - 1) = H(2) + 2 * 2 = 3 + 4 = 7

H(4) = H(4 - 1) + 2 * (4 - 1) = H(3) + 2 * 3 = 7 + 6 = 13

H(5) = H(5 - 1) + 2 * (5 - 1) = H(4) + 2 * 4 = 13 + 8 = 21

Let's examine the many methods for obtaining the Hogben numbers.

Recursive Approach

Let's look at how to find the first 10 Hogben numbers using a recursive method.

FileName: HogNum.java

public class HogNum   
{  
public int findHogNum(int n)  
{  
// handling the base case  
if(n == 1)  
{  
    return 1;   
}  
// recursively finding the nth Hogben number  
return (findHogNum(n - 1) + 2 * (n - 1));  
}  
  
// main method  
public static void main(String argvs[])  
{  
int n = 10;  
// creating an object of the class HogbenNum  
HogNum obj = new HogNum();  
System.out.println("The first " + n + " Hogben numbers are:");  
for (int j = 1; j <= n; j++)  
{  
int ans = obj.findHogNum(j);  
System.out.print(ans + " ");  
}  
}  
}  

Output

Hogben Numbers in Java

Complexity Analysis

The time complexity of the code is O(n), where n is the nth number, according to complexity analysis. The program has constant space complexity, or O (1).

Iterative Approach

Take a look at the iterative method for obtaining the first 10 Hogben numbers.

FileName: HogNum1.java

public class Hognum1  
{  
// main method  
public static void main(String argvs[])  
{  
int n = 10;  
// auxiliary array for storing the Hogben numbers  
int dp[] = new int[n + 1];  
dp[0] = 1;  
System.out.println("The first " + n + " Hogben numbers are:");  
for (int j = 1; j <= n; j++)  
{  
// computing the Hogben numbers  
dp[j] = dp[j - 1] + 2 * (j - 1);  
System.out.print(dp[j] + " ");  
}  
}  
}  

Output

Hogben Numbers in Java

Complexity Analysis

The program's time complexity is O (1). The program's space complexity is O(n), where n is the total number of Hogben numbers that need to be calculated.

The value of the most recent Hogben number computation is the single factor affecting the current Hogben number. Therefore, we are limited to using a variable to calculate the Hogben numbers rather than an array. See the code below.

FileName: HogNum2.java

public class HogNum2  
{  
// main method  
public static void main(String argvs[])  
{  
int n = 10;  
int temp = 1;  
System.out.println("The first " + n + " Hogben numbers are:");  
  
for (int j = 1; j <= n; j++)  
{  
// computing the Hogben numbers  
temp = temp + 2 * ( j - 1);  
System.out.print(temp + " ");  
}  
}  
}  

Output

Hogben Numbers in Java

Complexity Analysis

The program's time and space complexity are both O (1).

Using Mathematical Formula

The Hogben numbers are calculated using the following mathematical formula:

Where n >= 1, H(n) = n^2 - n + 1

Thus,

H(1) = 1^2 - 1 + 1 = 1 - 1 + 1 = 1

H(2) = 2^2 - 2 + 1 = 4 - 2 + 1 = 3

H(3) = 3^2 - 3 + 1 = 9 - 3 + 1 = 7

H(4) = 4^2- 4 + 1 = 16 - 4 + 1 = 13

H(5) = 5^2 - 5 + 1 = 25 - 5 + 1 = 21

The mathematical formula described above is used in the following code.

FileName: HogNum3.java

public class HogNum3  
{  
// main method  
public static void main(String argvs[])  
{  
int n = 10;  
  
System.out.println("The first " + n + " Hogben numbers are:");  
  
for (int j = 1; j <= n; j++)  
{  
// computing the Hogben numbers  
int ans = (j * j) - j + 1;  
System.out.print(ans + " ");  
}  
}  
}  

Output

Hogben Numbers in Java

Complexity Analysis

The program's time and space complexity are both O (1).


Related Topics

Segment Tree in Java

Binary trees can address a variety of issues; however, the Segment Tree is more efficient in terms of time complexity. The segment tree in Java is represented using an array. Native...

4 minutes read.

What are Array strings in Java?

In normal programming, An array is a group and a collection of identical forms of data that are stored in a sequential memory region and may be accessed using their...

4 minutes read.

Java throws

Java throws: The Java throws keyword is used with the signature of the method to indicate that the method may raise an exception. The method that uses the Java throws...

3 minutes read.

Creating API Document Javadoc tool

The JavaDoc utility is a document generator tool written in Java that generates standard documentation in HTML format. It parses declarations and documentation in a source file collection that describes...

3 minutes read.

Convert Char array to string in java

A collection of characters is referred to as a string. A character array differs from a string in that the string is canceled by the special character "\0." A string...

4 minutes read.

User Defined Custom Exceptions in Java

In this tutorial, we will discuss user-defined custom exceptions with examples. Introduction In Java, we have proactively characterised, Exception classes, for example, ArithmeticException, NullPointerException, ArrayoutOfBound and so on. These built-in exceptions are...

3 minutes read.

How to reverse a linked list in java

The process of reversing a linked list in Java will be covered in this section. One of the most common questions in interviews is about reversing a linked list. If...

11 minutes read.

Java Boolean logicalXor() Method

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

2 minutes read.

How to take Array Input in Java

In this tutorial, we will learn about how to take array input in Java. So, before taking inputs let us know what an array is first. Array: An array is a collection...

10 minutes read.

Sleeping Barber Problem in Java

The barbershop in this issue has one barber, one barber chair, and N chairs for customers in the waiting area. We may demonstrate the issue by keeping with the original...

6 minutes read.

How to convert list to String in Java

Sometimes, we need to transform a listing of characters into a string. A string is a chain of characters, so we will make a string from an individual array without...

4 minutes read.

Why to use Enum in Java?

In this article, you will be acknowledged about the Enum in java, its uses and mainly its purpose. Enum has various functionalities. Each of them will be discussed. Enum In a computer...

3 minutes read.

Types of Statements in Java

In natural languages, statements and sentences are roughly equivalent. In general, statements are similar to valid English sentences. We will talk about a statement in Java and the different kinds...

11 minutes read.

How to enable java in chrome

The Java module is huge for the Java Runtime Environment (JRE). It permits a program to work with the Java stage to run Java applets. Essentially, each of the undertakings connect...

3 minutes read.

Java File

Java file class implements the concept of file handling. It has several methods, such as deleting, creating, reading, and updating files. This class allows java users to perform various operations...

5 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 Boolean compareTo() method

The compareTo() method of Java Boolean class compares the Boolean argument with the Boolean instance and returns integer value, zero, or negative 1, or positive 1 based on the result...

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

Special Operators in Java

An operator in Java is a special symbol. It is used to perform operations on two or more variables.  We all know basic operations in Java. There are 8 types...

5 minutes read.

Big Decimal class in Java

The fairly Big pretty Decimal class provides operations for arithmetic, rounding, comparison and format conversion in a sort of big way. It can handle generally large and very small floating-point...

6 minutes read.