Thursday, July 4, 2024
HomeData ModellingData Structure & AlgorithmCheck whether a number is Sublime number or not

Check whether a number is Sublime number or not

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++




// C++ Implementation to Check whether
// a number is Sublime Number or Not.
#include <iostream>
using namespace std;
 
// Check whether the number is
// sublime or not
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) {
 
            // Finds the divisors
            // and sum up them
            s = s + i;
 
            // Counts the number
            // of divisors
            f++;
        }
    }
 
    // Check for perfect number
    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;
}
 
// Driver code
int main()
{
    int n = 12;
 
    // Function call
    checkSublime(n);
 
    return 0;
}


Java




// java program to Check whether the number is sublime or
// not
public class SublimeNumber {
 
    // Check whether the number is sublime or not
    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) {
                // Finds the divisors and sums them up
                s = s + i;
                // Counts the number of divisors
                f++;
            }
        }
 
        // Check for perfect number
        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");
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 12;
 
        // Function call
        checkSublime(n);
    }
}
 
// This code is contributed by shivamgupta0987654321


Python3




# python3 Implementation to Check whether
# a number is Sublime Number or Not.
 
def checkSublime(num):
    s = 0
    f = 0
    s1 = 0
    s2 = 0
 
    for i in range(1, num+1):
        if num % i == 0:
            # Finds the divisors and sums them up
            s += i
            # Counts the number of divisors
            f += 1
 
    # Check for perfect number
    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")
 
 
# Driver code
if __name__ == "__main__":
    n = 12
 
    # Function call
    checkSublime(n)
 
 
# This code is contributed by shivamgupta310570


C#




// C# Implementation to Check whether
// a number is Sublime Number or Not.
using System;
 
public class SublimeNumber
{
    // Check whether the number is sublime or not
    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)
            {
                // Finds the divisors and sums them up
                s = s + i;
                // Counts the number of divisors
                f++;
            }
        }
 
        // Check for perfect number
        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");
    }
 
    // Driver code
    public static void Main(string[] args)
    {
        int n = 12;
 
        // Function call
        CheckSublime(n);
    }
}


Javascript




// Check whether the number is
// sublime or not
function checkSublime(num) {
 
    let s = 0, f = 0, s1 = 0, s2 = 0;
 
    for (let i = 1; i <= num; i++) {
        if (num % i === 0) {
 
            // Finds the divisors
            // and sum up them
            s += i;
 
            // Counts the number
            // of divisors
            f++;
        }
    }
 
    // Check for perfect number
    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");
}
 
// Driver code
    const n = 12;
 
    // Function call
    checkSublime(n);


Output

True








Time Complexity: O(N)
Auxiliary Space: O(1) as using constant variables

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Last Updated :
25 Aug, 2023
Like Article
Save Article


Previous


Next


Dominic Rubhabha Wardslaus
Dominic Rubhabha Wardslaushttps://neveropen.dev
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments