Open In App
Related Articles

Refactorable number

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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.
Example : 
 

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


 


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)
    // Initialize result
    int divCount = 0; 
      
    for (int i = 1; i <= sqrt(n); ++i)
    {
        if (n % i==0)
        {
            // If divisors are equal, count 
            // only one.
            if (n / i == i)
                ++divCount;
  
            // Otherwise count both
            else 
                divCount += 2;
        }
    }
  
    return n % divCount == 0;
}
  
//Driver Code
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;
}

                    

Java

// Java program to check whether number is 
// refactorable or not
  
class GFG
{
    // Function to count all divisors
    static boolean isRefactorableNumber(int n)
    {
        // Initialize result
        int divCount = 0
          
        for (int i = 1; i <= Math.sqrt(n); ++i)
        {
            if (n % i==0)
            {
                // If divisors are equal, count 
                // only one.
                if (n / i == i)
                    ++divCount;
                      
                // Otherwise count both
                else 
                    divCount += 2;
            }
        }
        return n % divCount == 0;
    }
      
    public static void main (String[] args)
    {
        int n = 8;
        if (isRefactorableNumber(n))
            System.out.println("yes");
        else
            System.out.println("no");
              
        n = 14;
        if (isRefactorableNumber(n))
            System.out.println("yes");
        else
            System.out.println("no");
    }
}
  
// This code is contributed by Saket Kumar

                    

Python3

# 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 Code
n = 8
if isRefactorableNumber(n):
    print ("yes")
else:
    print ("no")
  
n = 14
if (isRefactorableNumber(n)):
    print ("yes")
else:
    print ("no")

                    

C#

// C# program to check whether number is 
// refactorable or not
using System;
  
class GFG
{
    // Function to count all divisors
    static bool isRefactorableNumber(int n)
    {
          
        // Initialize result
        int divCount = 0; 
          
        for (int i = 1; i <= Math.Sqrt(n); ++i)
        {
            if (n % i==0)
            {
                // If divisors are equal, count 
                // only one.
                if (n / i == i)
                    ++divCount;
                  
                // Otherwise count both
                else 
                    divCount += 2;
            }
        }
        return n % divCount == 0;
    }
      
    // Driver code
    public static void Main ()
    {
        int n = 8;
        if (isRefactorableNumber(n))
            Console.WriteLine("yes");
        else
            Console.Write("no");
              
        n = 14;
        if (isRefactorableNumber(n))
            Console.Write("yes");
        else
            Console.Write("no");
    }
}
  
// This code is contributed by nitin mittal.

                    

PHP

<?php
// PHP program to check
// whether number is 
// refactorable or not
  
// Function to count all divisors
function isRefactorableNumber($n)
      
    // Initialize result
    $divCount = 0; 
      
    for ($i = 1; $i <= sqrt($n); ++$i)
    {
        if ($n % $i==0)
        {
              
            // If divisors are equal,
            // count only one.
            if ($n / $i == $i)
                ++$divCount;
  
            // Otherwise count both
            else
                $divCount += 2;
        }
    }
  
    return $n % $divCount == 0;
}
  
    // Driver Code
    $n = 8;
    if (isRefactorableNumber($n))
        echo "yes";
    else
        echo "no";
        echo"\n";
    $n = 14;
    if (isRefactorableNumber($n))
        echo "yes";
    else
        echo "no";
  
// This code is contributed by Ajit
?>

                    

Javascript

<script>
  
// JavaScript program for the above approach
   
    // Function to count all divisors 
    function isRefactorableNumber(n) 
    
        // Initialize result 
        let divCount = 0;  
            
        for (let i = 1; i <= Math.sqrt(n); ++i) 
        
            if (n % i==0) 
            
                // If divisors are equal, count  
                // only one. 
                if (n / i == i) 
                    ++divCount; 
                        
                // Otherwise count both 
                else 
                    divCount += 2; 
            
        
        return n % divCount == 0; 
    
  
// Driver Code
    let n = 8; 
    if (isRefactorableNumber(n)) 
        document.write("yes" + "<br />"); 
    else
        document.write("no" + "<br />"); 
    
    n = 14; 
    if (isRefactorableNumber(n)) 
        document.write("yes"); 
    else
        document.write("no");
          
        // This code is contributed by splevel62.
</script>

                    
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


 


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 geeks!


Last Updated : 13 Sep, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials