×

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

Java String join() method

Java String join() method returns a joined String with given delimiter Syntax: public static String join(CharSequence delimeter,charSequence...elements) public static String join(CharSequence delimeter,Iterable<?extens charSequence>elements) Parameters: delimiter: char value to be added with each element elements: char value...

1 minute read.

Console Errors in Java

An unlawful motion taken through the person that reasons this system to act abnormally is amistake until this system is compiled or run; maximum programming mistakes pass unnoticed.The software is...

3 minutes read.

Java FileNotFoundException

FileNotFoundException is another exception class accessible in the java.io bundle. The exemption happens when we attempt to get to that document which isn't accessible in the framework. It is a checked...

5 minutes read.

Concurrent Modification Exception In Java

When an object is attempted to be updated concurrently when it is not allowed, the ConcurrentModificationException arises. This error typically occurs while using Java Collection classes. When another thread is iterating...

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

Compare time in java

Introduction: This article discusses how to compare time in java. Maximum of the time we need to examine the date and datetime items. Date comparisons are vital if you want to...

3 minutes read.

Hybrid Inheritance in Java

The most crucial OOPs concept in Java is inheritance, which enables the transfer of a class's properties to another class. It describes the Is-A relationship generally. We can create a...

3 minutes read.

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.

Enterprise Java Beans

One of the many Java APIs for the common development of corporate software is Enterprise Java Beans (EJB). An EJB, a server-side software component, contains the business logic of an...

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

Maximum length of string in java

In java, String can act as a data type and a class. The string can be defined as the collection of characters that are enclosed with double quotes(“ “). The...

3 minutes read.

Switch Case Program in Java

Switch Case Program in Java The switch case program in Java controls which code snippet gets executed on the basis of the value of the expression mentioned in the switch statement....

22 minutes read.

JDK | Java Development Kit

Java Development Kit (JDK) The Java Development Kit is a software development environment used to create Java applications. The JDK includes JRE (Java Runtime Environment), an interpreter (java), a compiler (javac),...

3 minutes read.

Java Get File Size

There are three ways to get the file size in Java. Using Java InputOutput Using NewInputOutput Library Files.size() Method FileChannel.size() Method Using Apache Commons InputOutput Using Java InputOutput: The Java IO package offers the classes that deal...

5 minutes read.

How to Convert double to int in Java

The double is a larger data type than int. When we assign a larger type value to a variable of smaller type, then we need to perform the explicit conversion....

2 minutes read.

What is new in Java 17?

Java 17 LTS is the most recent long-term support release for the Java SE platform. Under the Oracle No-Fee Terms and Conditions License, which stands for long-term support, JDK 17...

6 minutes read.

Java Developer

Who is a Java Developer? A Java developer is a skilled programmer who works on commercial applications, software, and webpages.  Java developers can work in two different areas: Operating system development:...

3 minutes read.

Print Pencil Shape Pattern in Java

Another pattern made from asterisk symbols that use loops and other logical concepts is the pencil pattern. It is usually requested to draw a pattern using a program. To write the...

6 minutes read.

Java program to print matrix in Z form

In this article, you will be acknowledged about what is a matrix along with an example. Also, most importantly, you will learn how to print matrix in Z form. What is...

5 minutes read.

Difference Between in Java and C++

FeatureC++JavaDefinitionC++ is a general programming language created by Bjarne Stroustrup as an extension of c language    Java is class-based, object-based, and designed to have as few implementation dependencies as...

4 minutes read.