×

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

List of Constants in Java

In this tutorial, we are going to deal with constants available in Java.Every programming language has its constants. Similarly, Javaalso has got constants of its own. In this tutorial, we will...

6 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 Thread class

Thread class The thread represents a part of the process. Every process can have multiple associated threads in which every thread may execute the same or different job. By default, each thread assigns...

16 minutes read.

Knapsack problem in Java

We have a collection of items in the knapsack problem. Every object has a weight and a value. These things should go in a knapsack. But there is a weight...

3 minutes read.

Set Value to Enum in Java

In this article, you will be acknowledged about Enum in java. Most importantly you will learn how to set value to Enum or how to practically customize a value to...

3 minutes read.

Memory Leak in Java

Java offers memory management right out of the box. When we use the new keyword to create an object, the JVM initialises for that object immediately. The trash collector automatically...

3 minutes read.

Read JSON file in Java

 To know about reading a JSON file in Java first we must know about the JSON file. JSON: JSON is “JavaScript Object Notation”. The JSON is the simple format for storing and...

3 minutes read.

Java Comparable Interface

We implement comparable interface when we need a new logic for determining equality. if two objects are equal, the equals() method returns true and compareTo() method returns 0. The basic...

1 minute read.

Java String indexOf() method

Java String indexOf() method returns index of a given character or substring present in a String. Methods Description int indexOf(int ch)     It returns index position for the given char value.int indexOf(int ch,...

2 minutes read.

Why we use static in Java

Many reserved keywords in Java cannot be used as variable names or identifiers. Java uses static keywords frequently because of its effective memory management system. In most cases, you must...

3 minutes read.

Java Applications

The growth in technology is increasing rapidly, so some languages are used for developing them. Java is one such famous programming language which is having numerous applications. The Java Programming...

4 minutes read.

Java Strings

String class is the most used class in Java programming language. The string is the sequence of characters, which is treated as objects in Java. Creating String objects We create String objects...

5 minutes read.

Sleep Time in Java

Sleep is amethod in java that is related to thread class and it is a concept of multithreading and is used to stop the execution of the current thread a...

4 minutes read.

Bin Packing Problem Java

Assigning some items with known weights to bins with consistent capacities is necessary for the bin packing problem. The goal is to use the smallest possible boxes while ensuring everything is put...

6 minutes read.

How to Convert Decimal to Octal in Java

How to Convert Decimal to Octal in Java There are two methods to convert Decimal to Octal: Using toOcatlString() method Using user-defined logic Using Integer.toOctalString() method The toOctalString() is astaticmethod of the Integer...

2 minutes read.

How to stop execution after a certain time in Java

We will learn how to stop a long-running execution after a set amount of time in this post. We will take a look at a few alternative approaches to this...

6 minutes read.

India Map Pattern in Java

India Map Pattern is the pattern we'll code now using Java, as demonstrated. We will use a star in Java to print our India map.The specialty is that we will only...

4 minutes read.

How Many Ways to Create Objects in Java

Introduction Java comes under the category of object-oriented programming languages. Since it is object-oriented, everything in Java is considered an object. Java is a diverse programming language that is designed to...

6 minutes read.

Java Math.multiplyExact() method

In java, we are provided with multiplyExact method in the Math module. This Math module belongs to the java.lang package. In general, this method returns the product of the provided...

2 minutes read.

Java Keywords

Java Keywords The particular words which are used in java programming language that act like a key or important words to write a code are called java keywords. Java Keywords are...

4 minutes read.