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
- Read or initialize a number n.
- Find the divisors of the given number (n).
- Count the total number of divisors of the given number (n) and store it in a variable (count).
- Sum up the divisors, and store the result in a variable (sum).
- At last, check count and sum are perfect numbers or not.
- If both are perfect numbers, the given number (n) is sublime.
- Else, not a sublime number.
Therefore, we have to implement two main logic:
1. Find and Count Divisors of a Number Logic
- public static void findAndCountDivisors(int num)
- {
- //variable to store the total number of divisors
- int count = 0;
- for (int i = 1; i <= num; i++)
- {
- //finds divisors
- if (num % i == 0)
- {
- //if the remainder is zero, increment the result variable
- count++;
- }
- }
2. Perfect Number Logic
- public static boolean isPerfectNumber(int num)
- {
- //variable to store the sum
- int sum = 0;
- for (int i=1; i< num; i++)
- {
- if ( num % i == 0)
- {
- //calculate the sum
- sum = sum + I;
- }
- }
- //returns true if the sum is equal to the num
- return (sum == num);
- }
- }
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.