Check whether a number is Sublime number or not
Last Updated :
25 Aug, 2023
Given a number N, the task is to check whether the number is a Sublime number or not.
Sublime numbers are defined as positive numbers whose sum of positive divisors and count of positive divisors are both perfect numbers.
Examples:
Input: N = 12
Output: True
Explanation: 12 is a sublime number because its divisors are 1, 2, 3, 4, 6, and 12. So there are 6 factors of it and 6 is a perfect number and 1+2+3+4+6+12=28, which is also a perfect number. So 12 is a Sublime number.
Input: N = 24
Output: False
Explanation: 24 is a not sublime number because the divisors of 24 are 1, 2, 3, 4, 6, 8, 12 and 24. So there are 8 factors and which is not a perfect number and 1+2+3+4+6+8+12+24=60, Which is not a perfect number. So 24 is not a Sublime number.
Approach: We have to implement two main logic:
- Find and Count Divisors of a Number Logic
- Perfect Number Checking.
Below are the steps involved in the implementation of the code:
- 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 whether count and sum are perfect numbers or not.
- If both are perfect numbers, the given number (n) is a sublime number.
- Else, not a sublime number.
Below is the implementation of the code:
C++
#include <iostream>
using namespace std;
void checkSublime( int num)
{
int s = 0, f = 0, s1 = 0, s2 = 0, i, j;
for (i = 1; i <= num; i++) {
if (num % i == 0) {
s = s + i;
f++;
}
}
for (j = 1; j < s; j++) {
if (s % j == 0)
s1 = s1 + j;
}
for (j = 1; j < f; j++) {
if (f % j == 0)
s2 = s2 + j;
}
if (s1 == s && s2 == f)
cout << "True" << endl;
else
cout << "False" << endl;
}
int main()
{
int n = 12;
checkSublime(n);
return 0;
}
|
Java
public class SublimeNumber {
public static void checkSublime( int num)
{
int s = 0 , f = 0 , s1 = 0 , s2 = 0 , i, j;
for (i = 1 ; i <= num; i++) {
if (num % i == 0 ) {
s = s + i;
f++;
}
}
for (j = 1 ; j < s; j++) {
if (s % j == 0 )
s1 = s1 + j;
}
for (j = 1 ; j < f; j++) {
if (f % j == 0 )
s2 = s2 + j;
}
if (s1 == s && s2 == f)
System.out.println( "True" );
else
System.out.println( "False" );
}
public static void main(String[] args)
{
int n = 12 ;
checkSublime(n);
}
}
|
Python3
def checkSublime(num):
s = 0
f = 0
s1 = 0
s2 = 0
for i in range ( 1 , num + 1 ):
if num % i = = 0 :
s + = i
f + = 1
for j in range ( 1 , s):
if s % j = = 0 :
s1 + = j
for j in range ( 1 , f):
if f % j = = 0 :
s2 + = j
if s1 = = s and s2 = = f:
print ( "True" )
else :
print ( "False" )
if __name__ = = "__main__" :
n = 12
checkSublime(n)
|
C#
using System;
public class SublimeNumber
{
public static void CheckSublime( int num)
{
int s = 0, f = 0, s1 = 0, s2 = 0, i, j;
for (i = 1; i <= num; i++)
{
if (num % i == 0)
{
s = s + i;
f++;
}
}
for (j = 1; j < s; j++)
{
if (s % j == 0)
s1 = s1 + j;
}
for (j = 1; j < f; j++)
{
if (f % j == 0)
s2 = s2 + j;
}
if (s1 == s && s2 == f)
Console.WriteLine( "True" );
else
Console.WriteLine( "False" );
}
public static void Main( string [] args)
{
int n = 12;
CheckSublime(n);
}
}
|
Javascript
function checkSublime(num) {
let s = 0, f = 0, s1 = 0, s2 = 0;
for (let i = 1; i <= num; i++) {
if (num % i === 0) {
s += i;
f++;
}
}
for (let j = 1; j < s; j++) {
if (s % j === 0)
s1 += j;
}
for (let j = 1; j < f; j++) {
if (f % j === 0)
s2 += j;
}
if (s1 === s && s2 === f)
console.log( "True" );
else
console.log( "False" );
}
const n = 12;
checkSublime(n);
|
Time Complexity: O(N)
Auxiliary Space: O(1) as using constant variables
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...