×

Sublime Number in Java

In this tutorial, we will understand what is meant by sublime number in java.

From the point of the coding interview, it is one of the important topics.

We will we will understand the meaning of a sublime number in Java with the aid of examples, illustrations, and implementations. It is one of the popular coding interview questions.

Sublime Number

A natural number N is called a sublime number if both the sum of its divisors and the number of its divisors or factors (including itself) are perfect numbers (the number whose sum of factors is equal to the number itself). It is an OEIS sequence A081357.

There are only two sublime numbers i.e. 12 that is the smallest sublime number and the second sublime number is 76 digits long number i.e. 6,086,555,670,238,378,989,670,371,734,243,169,622,657,830,773,351,885,970,528,324,860,512,791,691,264.

Let's understand it through an example.

45.3M

769

History of Java

Sublime Number Example

Let's see how 12 is a sublime number.

The number of factors or divisors of 12 is 6 i.e. 1, 2, 3, 4, 6, and 12

The sum of divisors is: 1 + 2 + 3 + 4 + 6 + 12 = 28

Both 6 and 28 are perfect numbers. Hence, 12 is a sublime number.

Steps to Find Sublime Number

  1. Read or initialize a number n.
  2. Find the divisors of the given number (n).
  3. Count the total number of divisors of the given number (n) and store it in a variable (count).
  4. Sum up the divisors, and store the result in a variable (sum).
  5. At last, check count and sum are perfect numbers or not.
  6. If both are perfect numbers, the given number (n) is sublime.
  7. Else, not a sublime number.

Therefore, we have to implement two main logic:

1. Find and Count Divisors of a Number Logic

  1. public static void findAndCountDivisors(int num)   
  2. {  
  3. //variable to store the total number of divisors  
  4. int count = 0;  
  5. for (int i = 1; i <= num; i++)   
  6. {  
  7. //finds divisors  
  8. if (num % i == 0)   
  9. {  
  10. //if the remainder is zero, increment the result variable  
  11. count++;  
  12. }  
  13. }  

2. Perfect Number Logic

  1. public static boolean isPerfectNumber(int num)   
  2. {  
  3. //variable to store the sum  
  4. int sum = 0;  
  5. for (int i=1; i< num; i++)  
  6. {  
  7. if ( num % i == 0)  
  8. {  
  9. //calculate the sum  
  10. sum = sum + I;  
  11. }      
  12. }  
  13. //returns true if the sum is equal to the num   
  14. return (sum == num);  
  15. }  
  16. }  

Let's implement the above logic in a Java program and check whether the given number is sublime or not.

Subline Number Java Program

Now let us look at the program in java to understand the working of sublime numbers.

SublimeNumberExample.java

import java.io.*;  
public class SublimeNumberExample  
{  
public static void main (String args[]) throws IOException  
{  
int s=0, f=0, s1=0, s2=0, num, i, j;  
BufferedReader obj=new BufferedReader (new InputStreamReader(System.in));  
System.out.print("Enter the number: ");  
//reads an integer from the user  
num=Integer.parseInt(obj.readLine());  
for(i=1; i<=num; i++)  
{  
if(num % i == 0)  
{  
//finds the divisors and sum up them      
s = s + i;  
//counts the number of divisors  
f++;  
}  
}  //end of for loop  
for(j=1; j<s; j++)  
{  
if(s % j == 0)  
s1 = s1 + j;  
} //end of for loop  
for(j=1; j<f; j++)  
{  
if(f % j == 0)  
s2 = s2 + j;  
}    //end of for loop  
if(s1==s && s2==f)  
System.out.println(num+ " is a sublime number.");  
else  
System.out.println(num+ " is not a sublime number.");  
}  
}  

Output 1:

Enter the number: 12
12 is a sublime number.

Output 2:

Enter the number: 346587
346587 is not a sublime number.

Summary

In this tutorial, we tried to understand what is meant by sublime number, saw a good number of examples, and further saw its implementation in java. That is all about sublime numbers.


Related Topics

Add Time in Java

Before Java 8, java.util.Date was one of the most usually involved classes for addressing date-time values in java. Then Java 8 presented java.time.LocalDateTime and java.time.ZonedDateTime. Java 8 likewise permits us...

6 minutes read.

Java List Node

In Java, List Node is the same as the single linked list, which is the collection of nodes. So, we can say, the list nodes are grouped together to get...

8 minutes read.

Java Private keyword

A Java access modifier is a private keyword. It can be used to inner classes, methods, and variables. It is the type of access modifier that is most constrained. Privately declared...

4 minutes read.

Pangram Program in Java

If a string comprises all alphabet letters from A to Z or from a to z without regard to case, it is referred to as a pangram. Some examples of pangram...

3 minutes read.

Getting the Day from the Date in Java?

To extract the day's name from the date, we will write Java application. When dealing the date and time in Java, the following classes are used. Class for Calendars: This class is...

6 minutes read.

Java Base64 Encoding and Decoding

Introduction to Encoding and Decoding Encoding is the process of putting the sequence of characters like letters, numbers, punctuations, and other symbols into a specialised format for the efficient transmission or...

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

if-else Program in Java

if-else Program in Java The if-else program in Java controls which code snippet will execute. The if-else program is very basic and yet very important. Because it checks how well one...

19 minutes read.

Tetris Game in Java

The Tetris game is among the most well-known video games ever produced for computers. Today, we may engage in this game on a mobile device as well. Alexey Pajitnov conceptualized...

12 minutes read.

Java ResultSetMetaData

The data about another data is called Metadata. The ResultSetMetaData is used to store the data about ResultSet. The ResultSet Contains the columns, rows, names of table, datatypes etc. these...

2 minutes read.

Pig Latin Program in Java

Pig Latin Program in Java Pig Latin is a method for translating words of the English language into a different language. It is an encrypted word that is generated by using the following steps....

4 minutes read.

Java Return Keyword

The return keyword in Java is used to end a method's execution. the caller receives the return, followed by the appropriate value. The return type of the method, such as...

3 minutes read.

How to Iterate List in Java?

List is a Collection foundation interface in Java. It enables us to keep the collection of objects in order. The four classes that make up the List interface implementation are...

3 minutes read.

Bucket Sort in Java

Bucket Sort in JavaBucket sort is also called bin sort. Bucket sort first puts the elements of the array or list into different buckets. The first bucket contains elements of the smallest value. The...

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

Java Case Keyword

Case keyword: In this article we are going to learn the concept of a java case keyword.Generally, java case keyword is used with the switch statements.Case keyword is implemented in conditional...

3 minutes read.

Hourglass problem in Java

In this section, we will discuss the hourglass problem in Java.The aim is to find the largest sum of an hour glass given a 2D matrix. An hour glass is made...

2 minutes read.

Structure of Java Program

Java is well-known as an object-oriented, secure, and platform-independent programming language. The Java programming language allows us to construct a wide variety of programs. Therefore, it is essential to fully...

6 minutes read.

Java Create File

A File is a hypothetical way, which has no genuine presence. It is very much like while "using" that File that the major real activity of putting away something in...

5 minutes read.

Difference between JDK, JRE and JVM In Java

In the Java ecosystem, three primary components are often mentioned: JDK, JRE, and JVM. Here’s a breakdown of each: Java Development Kit (JDK) The Java Development Kit (JDK) is a comprehensive software...

2 minutes read.