×

Decagonal Numbers in Java

This section explains what is a decagonal number and how to write Java programmes that compute decagonal numbers. Both academics and Java programmer interviews regularly question about the Decagonal number program.

The Decagonal Number

An example of a figurate number is a decagonal number, which is defined as follows:

D(m) = D(m - 1) + 8 * m – 7 Where m >= 1 and D(0) = 0

The Fined Decagonal Number

The decagonal number can be found using a variety of methods. The following three strategies will be covered in this section:

  • Using the Recursion
  • Using the Dynamic Programming
  • Using the Mathematical Formula

Using the Recursion

Given that we are already familiar with the recursive formula, finding the decagonal numbers by recursion is simple. The same can be seen in the program below.

DecagonalNumbersDemo.java

public class DecagonalNumbersDemo   
{  
// a procedure for calculating its nth Decagonal number
public int findTheDecagonalNumber(int n)  
{  
    // addressing the default case
    if(m == 0)  
    {  
        return 0;  
    }  
    // the decagonal numbers are calculated iteratively
    // utilising the formula for recursion
    return findTheDecagonalNumber(m - 1) + 8 * m - 7;  
}  
// It is the main method  
public static void main(String[] argvs)  
{  
// constructing a DecagonalNumbersDemo object
DecagonalNumbersDemo srj = new DecagonalNumbersDemo();  
System.out.println("Here are the first ten decagonal numbers: ");  
int range = 10; // r range that is used to calculate decagonal numbers
for(int s = 1; s <= range; s++)  
{  
    int res = srj.findTheDecagonalNumber(i);  
    System.out.print(res + " ");  
}  
}  
}  

Output:

Here are the first ten decagonal numbers: 
1 10 27 52 85 126 175 232 297 370

Analysis of Complexity: The temporal complexity of the programme equals O(n), wherein n is now the nth decagonal number, as the recursion used to get the decagonal numbers goes from n to 0 in this case. The program's space complexity is O(1) because it is not utilising any more space.

Given that we are repeatedly computing the same subproblem, the preceding program's temporal complexity can be decreased even further. In the case of computing D(3), for instance, D(3) is lowered to D(2), which is then reduced to D. (1). We can prevent this from happening by not having to recalculate D(2) and D(1). The same can be seen in the next method.

Using the dynamic programming

The obtained decagonal numbers can be stored in such an extra array for use in subsequent computations. Take note of the program below.

DecagonalNumbersDemo1.java

public class DecagonalNumbersDemo1  
{  
// It is the main method  
public static void main(String[] argvs)  
{  
// constructing a DecagonalNumbersDemo1 object
DecagonalNumbersDemo1 srj = new DecagonalNumbersDemo1();  
System.out.println("Here are the first ten decagonal numbers: ");  
int range = 10; // range that is used to calculate decagonal numbers
int dpi[] = new int[range + 1];  
for(int s = 1; s <= 10; s++)  
{  
    dpi[s] = dpi[s - 1] + 8 * i - 7;  
    System.out.print(dpi[s] + " ");  
}  
}  
}  

Output:

Here are the first ten decagonal numbers:
1 10 27 52 85 126 175 232 297 370

Analysis of Complexity: The program's temporal complexity is O (1). Where ranging is the figure upto from which the decagonal values are computed, and because the programme uses an auxiliary array, its space complexity is O(range).

If we look now at recursive formula, we can see that the decagonal number that is being computed at the moment depends only on the decagonal number that was computed just before it, not on all of the decagonal numbers that have been computed before. Consequently, we can employ a variable to store the achieved by comparing decagonal number rather than an array. Take note of the programme below.

Using the Mathematical Formula

The decagonal values are calculated using the following mathematical formula:

D(m) = m x ((4 x m) - 3). In which m >= 1

Let's see how to apply the mathematical formula.

DecagonalNumbersDemo3.java

public class DecagonalNumbersDemo3  
{  
// It is the main method  
public static void main(String[] argvs)  
{  
// constructing a DecagonalNumbersDemo3 object
DecagonalNumbersDemo3 srj = new DecagonalNumbersDemo3();  
System.out.println("Here are the first ten decagonal numbers: ");  
int range = 10; // range that is used to calculate decagonal numbers 
int dpi[] = new int[range + 1];  
for(int s = 1; s <= 10; s++)  
{  
    // mathematical formula is used  
    int numb = s * (4 * s - 3);  
    System.out.print(numb + " ");  
}  
}  
}  

Output:

Here are the first ten decagonal numbers:
1 10 27 52 85 126 175 232 297 370

Related Topics

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.

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.net.SocketException

Exception The problem occurred during the execution of the program. If an exception occurs in the program, the program gets terminated. To skip the exception occurring statements, we have to handle...

4 minutes read.

Java Framework List

The framework is the programs which are written in Java. Frameworks in Java are used to create web applications. The code which can reuse can act as a reference for...

6 minutes read.

Java Lambda foreach

Before understanding lamda foreach, one must know about foreach loop in Java. foreach loop in Java Since J2SE 5.0, there has been a for-each loop in Java, also known as an extended...

4 minutes read.

this keyword in Java

The 'this' keyword is an essential notion in Java. We'll go through the 'this' keyword in depth and provide some examples of how it's used in Java. In Java, the term...

5 minutes read.

How to Solve the Deprecated Error in Java?

Deprecated Java methods should not be used because they are deprecated (often, there are better, more modern alternatives). API update. Until now, Java has kept everything backward compatible and never...

3 minutes read.

Difference between = = and equals ( ) in java

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

6 minutes read.

Generic queue in Java

Before understanding how to implement a generic queue in java, one must know about generics and queue in java. Generics in Java Generics are parameterized types. The goal is to enable type...

6 minutes read.

Java array list remove time complexity

Java: We know that java is one of the programming languages. The main feature of java which is not in C or object oriented programming language is platform independence. Not only the...

7 minutes read.

nth Prime Number in Java

A number is considered prime if only divisible by one and itself. Prime numbers include, for example, 2, 3, 5, 7, 11, etc. In looking at it another way, a...

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

Reverse a String using Collections in Java

Generally, a String is a grouping of characters. Yet, in Java, a String is an item that addresses a succession of characters. The Java.lang.String class is utilized to make a...

5 minutes read.

Bedrock vs Java

The popularity of Minecraft, a sandbox video game, has skyrocketed. The scope, level of complexity, and variety of gameplay in this game are enormous, and user-generated content has helped to...

4 minutes read.

Check the presence of Substring in a String in java

In java, the string can be treated as class and datatype. The string contains words and numbers but should be in double-quotes. Example: ” Omsairam” Substring The part of the string is called...

2 minutes read.

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.

How to convert double to String in Java

How to Convert double to String in Java It is used when we want to convert double primitive to String type. There are two methods to convert double to String. Using String.valueOf()...

2 minutes read.

Java Byte intValue() method

The intValue()  method of Java Integer class returns an int value for this Integer.  Syntax public int intValue()  Parameters NA  Specified by This method is specified by intValue in class Number  Return Value This method returns the numeric...

1 minute read.

How to Create an Object in Java

How to Create an Object in Java An object can be defined as a run time entity that contains the blue printof the class. It means that all the member functions...

5 minutes read.

Java Beans

It is a Java class, It follows conventions they are: It must have a no-arg constructorIt must be serializable.It must provide the methods to get and set the properties Uses Of Java...

3 minutes read.