Admirable Numbers In C++ - C++ Programming Tutorial
C++ Course / Miscellaneous / Admirable Numbers In C++

Admirable Numbers In C++

BLUF: Mastering Admirable Numbers In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Admirable Numbers In C++

C++ is renowned for its efficiency. Learn how Admirable Numbers In C++ enables low-level control and high-performance computing in the tutorial below.

It is deemed commendable when an integer possesses a unique relationship with its divisors. If the sum of all proper divisors of a number (excluding the number itself) equals another number M, and M is a divisor of N, then the number is classified as Admirable. The connection between divisor sums and divisibility is crucial. To identify exceptional numbers in C++, one must compute the divisors, aggregate them, and verify that the specified divisibility criteria are met. This not only enhances one's programming proficiency in crafting loops and conditional statements but also aids in optimizing mathematical algorithms for computational efficiency. Furthermore, exploring this topic offers an intriguing pathway to delve into fundamental principles of number theory, encompassing modular arithmetic and divisor functions.

Mathematical condition:

Sum of proper divisors of N=M, and M∣N.

Examine the value of N as 12. The appropriate divisors of 12 include 1, 2, 3, 4, and 6, with a collective sum of 16. While it doesn't happen in this specific case, the number 12 would be considered commendable if 16 were divisible by 12.

Input: N = 12

Output: Yes

Explanation:

12’s proper divisors are 1, 2, 3, 4, 6, and 12

sigma(N) = 1 + 2 + 3 + 4 + 6 + 12 = 28

sigma(N) – 2D’ = 2N

28 – 22 = 212

24 == 24

In the realm of computational mathematics and properties related to divisors, remarkable numbers hold significance. In C++, one can attain this by effectively calculating divisors and their totals, as well as confirming divisibility conditions. Implementing this concept aids in grasping conditional validations, modular calculations, and divisor techniques. Remarkable numbers serve as a captivating subject for mathematical coding assignments due to these characteristics.

Algorithm:

Calculate the sum of divisors of N:

  • Initialize a variable sumDivisors = 0.
  • For each integer
  • i from 1 to root N:
  • If N%i==0:
  • Add i to sumDivisors.
  • If i≠N/i to sumDivisors.
  • Add N and 1 to sumDivisors at the end.

Check for the condition σ(N)−2D=2N:

  • For each divisor D of N from 1 to root N:
  • If N%D==0, check if σ(N)−2D==2N.
  • If true, return "Yes" and terminate.

Return "No" if there is no suitable divisor that meets the criteria.

Pseudocode:

Example

function calculateDivisorSum(N):
    sumDivisors = 0
    for i = 1 to sqrt(N):
        if N % i == 0:
            sumDivisors += i
            if i != N / i:
                sumDivisors += N / i
    sumDivisors += 1 + N   // Add 1 and N to sum
    return sumDivisors

function isAdmirable(N):
    sigmaN = calculateDivisorSum(N)
    for i = 1 to sqrt(N):
        if N % i == 0:
            if sigmaN - 2 * i == 2 * N:
                return "Yes"
    return "No"

Example:

Let's consider an example to explain the concept of Admirable Numbers in C++.

Example

#include <iostream>
#include <cmath>
using namespace std;

// Function to calculate the sum of all divisors of a given number
int calculateDivisorSum(int number) {
    int sum = 0;
    // Find all divisors of the number
    for (int i = 2; i <= sqrt(number); i++) {
        // Check if 'i' is a divisor of 'number'
        if (number % i == 0) {
            // If both divisors are the same, add once; otherwise, add both
            if (i == (number / i))
                sum += i;
            else
                sum += (i + number / i);
        }
    }
    // Include 1 and the number itself in the sum
    return (sum + number + 1);
}

// Function to verify the admirable number condition
bool verifyAdmirableCondition(int number) {
    int sigmaNumber = calculateDivisorSum(number);
    // Check all divisors of 'number'
    for (int i = 2; i <= sqrt(number); i++) {
        // If 'i' is a divisor of 'number'
        if (number % i == 0) {
            // If divisors are equal, check once; otherwise, check both
            if (i == (number / i)) {
                if (sigmaNumber - 2 * i == 2 * number)
                    return true;
            } else {
                if (sigmaNumber - 2 * i == 2 * number)
                    return true;
                if (sigmaNumber - 2 * (number / i) == 2 * number)
                    return true;
            }
        }
    }
    // Check 1, since it is also a divisor
    if (sigmaNumber - 2 * 1 == 2 * number)
        return true;

    return false;
}

// Function to determine if a number is admirable
bool isAdmirableNumber(int number) {
    return verifyAdmirableCondition(number);
}

// Driver code
int main() {
    int numberToCheck = 18; // Change this value to test other numbers
    if (isAdmirableNumber(numberToCheck))
        cout << numberToCheck << " is an admirable number." << endl;
    else
        cout << numberToCheck << " is not an admirable number." << endl;
    return 0;
}

Output:

Output

If the number is 12
12 is an admirable number.
If the number is 18
18 is not an admirable number.
If the number is 28
28 is not an admirable number.

Conclusion:

In the field of number theory, the notion of admirable numbers is quite fascinating. These numbers emerge when a number satisfies a specific condition related to the sum of its divisors. To determine the admirability of a number, we initially compute the total sum of all its divisors and then search for an appropriate divisor that, when subtracted twice from the sum, results in twice the original number. Exploring the properties of divisor functions becomes more manageable through this concept, offering a valuable exercise in both logical reasoning and algorithmic development. The C++ function demonstrated above efficiently examines admirable numbers, leveraging fundamental concepts like divisor summation and mathematical criteria. This serves as a compelling illustration of how number theory principles can be effectively utilized in practical coding scenarios.

Input Required

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

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience