Refactorable Number In C++

Refactorable numbers are integers with a special property in number theory, and they are also called Smith numbers . A number is refactorable if its total digits (apart from 1) equal the total digits of all of its prime factors. From a computational and mathematical standpoint, they are interesting because of this feature.

Refactorable number: If a number is divided into its total number of factors, it can be refactored. Refactorable numbers, like 9, can be broken down into smaller numbers because they are divisible by 3 and have a total of 3 factors that is, 1, 3, 9.

No's are: 1, 2, 8, 9, 12, 18, 24, 36, 40, 56, 60, 72, 80, 84, 88, 96, 104, 108, 128, 132, 136, 152, 156, 180, 184, 204, 225, 228, 232, 240, 248, 252, 276, 288, 296, ...

Example 1:

Input: int number = 36

Output: It is a Refactorable number

Explanation: If a number can be divided by the total number of factors that are available, it can be refactored. We are given the number 36, which is refactorable because it can be divided by three and has a total of three factors, or 3(1, 3, 9,18,36).

Example 2:

Input: int number = 10

Output: It is not a Refactorable number

Explanation: If a number can be divided by the total number of factors that are available, it can be refactored. We are given the number 10, which cannot be refactored because it has a total of four factors-that is, 4(1, 2, 5, 10); additionally, since 10 is not divisible by 4, it cannot be refactored.

Example 1:

Let us take an example to illustrate the Refactorable number in C++.

Example

#include <bits/stdc++.h> 

// Function to check if a number is refactorable
bool isRefactorableNumber(int num) 
{ 
	// Initialize divisor count
	int divisorCount = 0; 
	
	// Loop through all numbers up to square root of num
	for (int i = 1; i <= sqrt(num); ++i) 
	{ 
		// Check if i is a divisor of num
		if (num % i == 0) 
		{ 
			// If divisors are equal, count only one
			if (num / i == i) 
				++divisorCount; 
			// Otherwise count both divisors
			else
				divisorCount += 2; 
		} 
	} 

	// Check if num is refactorable
	return num % divisorCount == 0; 
} 

// Driver Code 
int main() 
{ 
	int n = 20; // Change the number to check refactorability
	if (isRefactorableNumber(n)) 
		puts("Number is refactorable"); 
	else
		puts("Number is not refactorable"); 

	n = 36; // Change another number to check refactorability
	if (isRefactorableNumber(n)) 
		puts("Number is refactorable"); 
	else
		puts("Number is not refactorable"); 

	return 0; 
}

Output:

Output

Number is not refactorable
Number is refactorable

The code carries out a refactoring check on the provided numbers. To test different numbers for refactorability, you can alter the values of n in the main function.

Example 2:

Let us take another example to illustrate the Refactorable number in C++.

Example

#include <iostream>
#include <vector>
#include <cmath>

using namespace std;

// Function to compute the sum of digits of a number
int sumOfDigits(int n) {
    int sum = 0;
    while (n > 0) {
        sum += n % 10;
        n /= 10;
    }
    return sum;
}

// Function to compute prime factors of a number
vector<int> primeFactors(int n) {
    vector<int> factors;
    while (n % 2 == 0) {
        factors.push_back(2);
        n /= 2;
    }
    for (int i = 3; i <= sqrt(n); i += 2) {
        while (n % i == 0) {
            factors.push_back(i);
            n /= i;
        }
    }
    if (n > 2)
        factors.push_back(n);
    return factors;
}

// Function to check if a number is refactorable
bool isRefactorable(int n) {
    int digitSum = sumOfDigits(n);
    vector<int> factors = primeFactors(n);
    int factorSum = 0;
    for (int factor : factors) {
        factorSum += sumOfDigits(factor);
    }
    return digitSum == factorSum;
}

int main() {
    int num;
    cout << "Enter a number: ";
    cin >> num;

    if (isRefactorable(num)) {
        cout << num << " is a refactorable number." << endl;
    } else {
        cout << num << " is not a refactorable number." << endl;
    }

    return 0;
}

Output:

Output

Enter a number: 6
6 is not a refactorable number.

Conclusion:

Primitive abundant numbers, or refactorable numbers, are an interesting topic in number theory mathematics. In C++, we can check if a number is refactorable by adding up all of its proper divisors and seeing if that total equals the sum of the divisors of the sum. Making sure the number has the special ability to be expressed as the sum of its divisors is the result of this procedure. With the use of effective methods and algorithms, C++ enables the development of strong programs that search for and locate refactorable numbers inside a bound range. These applications help with computational thinking and algorithmic problem-solving in addition to helping with the understanding of numerical properties.

Input Required

This code uses input(). Please provide values below: