Refactorable number

Given an integer n. Check whether the number is refactorable or not. A refactorable number is an integer n that is divisible by count of all it’s divisors.

Input:  n = 8
Output: yes
Explanation:
8 has 4 divisors: 1, 2, 4, 8
Since 8 is divisible by 4 therefore 8 is 
refactorable number.

Input : n = 4 
Output: no
We strongly recommend that you click here and practice it, before moving on to the solution.

This solution is pretty straightforward. The idea is to iterate from 1 to sqrt(n) and count all the divisors of a number. After that we just need to check whether the number n is divisible by it’s total count or not.

C++

// C++ program to check whether number is 
// refactorable or not
#include <bits/stdc++.h>

// Function to count all divisors
bool isRefactorableNumber(int n)
{    
    int divCount = 0;  // Initialize result

    for (int i=1; i<=sqrt(n); ++i)
    {
        if (n%i==0)
        {
            // If divisors are equal, count 
            // only one.
            if (n/i == i)
                ++divCount;

            else // Otherwise count both
                divCount+=2;
        }
    }

    return n % divCount == 0;
}

//Driver program to test above function
int main()
{
    int n = 8;
    if (isRefactorableNumber(n))
        puts("yes");
    else
        puts("no");

    n = 14;
    if (isRefactorableNumber(n))
        puts("yes");
    else
        puts("no");

    return 0;
}

Python

# Python program to check whether number is
# refactorable or not
import math

def isRefactorableNumber(n):

    # Initialize result
    divCount = 0

    for i in range(1,int(math.sqrt(n))+1):

        if n % i == 0:

            # If divisors are equal, count only one
            if n/i == i:
                divCount += 1

            else:  # Otherwise count both
                divCount += 2

    return n % divCount == 0


# Driver program to test above function
n = 8
if isRefactorableNumber(n):
    print "yes"
else:
    print "no"

n = 14
if (isRefactorableNumber(n)):
    print "yes"
else:
    print "no"
Output:
yes
no

Time complexity: O(sqrt(n))
Auxiliary space: O(1)

Facts about refactorable number

  1. There is no refactorable number which is perfect.
  2. There is no three consecutive integers can all be refactorable.
  3. Refactorable number have natural density zero.

Reference: https://en.wikipedia.org/wiki/Refactorable_number

This article is contributed by Shubham Bansal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice





Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.