×

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

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.

Hashtable in Java

Hashtable in Java The Hashtable class implements the Map interface and extends the Dictionary class. It implements a hash table which shows the key-value relation, i.e., it maps the keys to the values....

9 minutes read.

Java Class Keyword

We know that java is object-oriented programming language which contains the essential key concepts such as classes and objects etc to have clear idea about the object-oriented programming.Java is mainly...

3 minutes read.

Java String Methods

Java String Methods Java String class is the most important class of the java.lang package. It is used to handle the String related operations. It contains a lot of built-in Java...

2 minutes read.

Java float vs double

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

7 minutes read.

Moran Numbers in Java

In this article, we will be acknowledged about the moran numbers in Java, how they are formed, what are the approaches to achieve the moran numbers. Moran Number A moran numbers are...

3 minutes read.

Java Integer toBinaryString() method

The toBinaryString() method of Java Integer class returns a string representing the specified int argument as an unsigned integer in base 2. Syntax public static String toBinaryString (int  i)  Parameters The parameter ‘i’ represents...

1 minute read.

What’s new in Java 12

On March 19th, 2019, the Java 12th edition was released. After releasing this edition, they have decided to release every new edition every six months. This version is the advanced...

5 minutes read.

Java Virtual Machine (JVM)

JVM is a virtual runtime environment to execute Java byte codes. The JVM doesn’t understand the keywords we used to write code. That is why it is converted into bytecode. It controls the...

3 minutes read.

Java Swing Time Picker

Prerequisites In this tutorial, we will learn the time picker in java swing. Before learn time picker, we should learn about java swing. Java Swing Introduction The Java Foundation Classes include Java Swing....

5 minutes read.

Java calculate age

In this section, we will create a Java program that calculates age from the given date of birth or current date. In order to get the date of birth from the current...

6 minutes read.

Java Implements Keyword

To understand about the keyword implements we need to learn about the concept of the interfaces and inheritance in java programming languages. So let us learn about the interface and...

3 minutes read.

Design of JDBC

Java applications may interface using database systems from many vendors using the Java Database Connectivity (JDBC) Application Software Interface (API) from Sun Microsystem. To connect spreadsheets, JDBC and database drivers...

3 minutes read.

Java RandomAccessfile

Writing and reading to random access files are done using this class. An array of many bytes is how a random access file operates. By changing the implied file pointer...

3 minutes read.

Traverse through HashMap in Java

In this tutorial, we will discuss traversing through HashMap in Java Introduction: HashMap<Key, Value> is a part of the java collection implemented from the java 1.2 version. HashMap is written as HashMap<K,...

4 minutes read.

Lazy Propagation in Segment Tree in Java

The topic of segment trees in Java is continued by the topic of sluggish propagation in segment trees. It is suggested that readers first read through the section tree topic....

4 minutes read.

Facts about null in Java

Nearly all programming languages have a relationship with null. Hardly any programmers are unconcerned by null. The null has a java.lang.NullPointerException association in Java. Given that it is a class...

4 minutes read.

ConcurrentSkipListSet in Java

ConcurrentSkipListSet is a class that is a part of collection framework in Java.It has been available since Java version 1.6. ConcurrentSkipListSet  is a scalable concurrent NavigableSet implementation based on a ConcurrentSkipListMap.The...

16 minutes read.

Java String valueOf() method

Java String valueOf() method converts different types of values into String. Such as : int to String, long to String, boolean to String, character to String, float to String, double to...

2 minutes read.

Magic Square in Java

A square with numbers is referred to as a magic square. The numbers in a magic square of order n are arranged to ensure that the sum of all of...

4 minutes read.