Arithmetic Number

In number theory, an arithmetic number is an integer for which the average of its positive divisors is also an integer. Or in other words, a number N is arithmetic if the number of divisors divides the sum of divisors.
Given a positive integer n. The task is to check whether n is Arithmetic number or not.

Examples:

Input : n = 6
Output : Yes
Sum of divisor of 6 = 1 + 2 + 3 + 6 = 12.
Number of divisor of 6 = 4.
So, on dividing Sum of divisor by Number of divisor
= 12/4 = 3, which is an integer.

Input : n = 2
Output : No


Algorithm

  1. Find sum of all the factors of a number , say sum.
  2. Find count of divisors (say Count).
  3. Check if sum is divisible by count.

C++

// CPP program to check if a number is Arithmetic
// number or not
#include <bits/stdc++.h>
using namespace std;
  
// Sieve Of Eratosthenes
void SieveOfEratosthenes(int n, bool prime[],
                         bool primesquare[], int a[])
{
    for (int i = 2; i <= n; i++)
        prime[i] = true;
  
    for (int i = 0; i <= (n * n + 1); i++)
        primesquare[i] = false;
  
    // 1 is not a prime number
    prime[1] = false;
  
    for (int p = 2; p * p <= n; p++) {
        // If prime[p] is not changed, then
        // it is a prime
        if (prime[p] == true) {
            // Update all multiples of p
            for (int i = p * 2; i <= n; i += p)
                prime[i] = false;
        }
    }
  
    int j = 0;
    for (int p = 2; p <= n; p++) {
        if (prime[p]) {
            // Storing primes in an array
            a[j] = p;
  
            // Update value in primesquare[p*p],
            // if p is prime.
            primesquare[p * p] = true;
            j++;
        }
    }
}
  
// Function to count divisors
int countDivisors(int n)
{
    // If number is 1, then it will have only 1
    // as a factor. So, total factors will be 1.
    if (n == 1)
        return 1;
  
    bool prime[n + 1], primesquare[n * n + 1];
  
    int a[n]; // for storing primes upto n
  
    // Calling SieveOfEratosthenes to store prime
    // factors of n and to store square of prime
    // factors of n
    SieveOfEratosthenes(n, prime, primesquare, a);
  
    // ans will contain total number of 
    // distinct divisors
    int ans = 1;
  
    // Loop for counting factors of n
    for (int i = 0;; i++) {
  
        // a[i] is not less than cube root n
        if (a[i] * a[i] * a[i] > n)
            break;
  
        // Calculating power of a[i] in n.
        // cnt is power of prime a[i] in n.
        int cnt = 1; 
  
        // if a[i] is a factor of n
        while (n % a[i] == 0) 
        {
            n = n / a[i];
            cnt = cnt + 1; // incrementing power
        }
  
        // Calculating number of divisors
        // If n = a^p * b^q then total 
        // divisors of n
        // are (p+1)*(q+1)
        ans = ans * cnt;
    }
  
    // if a[i] is greater than cube root of n
  
    // First case
    if (prime[n])
        ans = ans * 2;
  
    // Second case
    else if (primesquare[n])
        ans = ans * 3;
  
    // Third casse
    else if (n != 1)
        ans = ans * 4;
  
    return ans; // Total divisors
}
  
// Returns sum of all factors of n.
int sumofFactors(int n)
{
    // Traversing through all prime factors.
    int res = 1;
    for (int i = 2; i <= sqrt(n); i++) {
  
        int count = 0, curr_sum = 1;
        int curr_term = 1;
        while (n % i == 0) {
            count++;
            n = n / i;
  
            curr_term *= i;
            curr_sum += curr_term;
        }
  
        res *= curr_sum;
    }
  
    // This condition is to handle
    // the case when n is a prime
    // number greater than 2.
    if (n >= 2)
        res *= (1 + n);
  
    return res;
}
  
// Check if number is Arithmetic Number
// or not.
bool checkArithmetic(int n)
{
    int count = countDivisors(n);
    int sum = sumofFactors(n);
  
    return (sum  % count == 0);
}
  
// Driven Program
int main()
{
    int n = 6;
    (checkArithmetic(n)) ? (cout << "Yes") : 
                           (cout << "No");
    return 0;
}

PHP

<?php
// PHP program to check if 
// a number is Arithmetic 
// number or not
  
// Sieve Of Eratosthenes
function SieveOfEratosthenes($n, &$prime,
                             &$primesquare, &$a)
{
    for ($i = 2; $i <= $n; $i++)
        $prime[$i] = true;
  
    for ($i = 0; 
         $i <= ($n * $n + 1); $i++)
        $primesquare[$i] = false;
  
    // 1 is not a
    // prime number
    $prime[1] = false;
  
    for ($p = 2; $p * $p <= $n; $p++) 
    {
        // If prime[p] is 
        // not changed, then
        // it is a prime
        if ($prime[$p] == true) 
        {
            // Update all multiples of p
            for ($i = $p * 2;   
                 $i <= $n; $i += $p)
                $prime[$i] = false;
        }
    }
  
    $j = 0;
    for ($p = 2; $p <= $n; $p++)
    {
        if ($prime[$p]) 
        {
            // Storing primes in an array
            $a[$j] = $p;
  
            // Update value in
            // primesquare[p*p],
            // if p is prime.
            $primesquare[$p * $p] = true;
            $j++;
        }
    }
}
  
// Function to count divisors
function countDivisors($n)
{
    // If number is 1, then it
    // will have only 1 as a 
    // factor. So, total factors 
    // will be 1.
    if ($n == 1)
        return 1;
  
    $prime = array_fill(0, ($n + 1), false);
    $primesquare = array_fill(0, ($n *
                              $n + 1), false);
  
    // for storing primes upto n
    $a = array_fill(0, $n, 0); 
  
    // Calling SieveOfEratosthenes 
    // to store prime factors of 
    // n and to store square of 
    // prime factors of n
    SieveOfEratosthenes($n, $prime
                        $primesquare, $a);
  
    // ans will contain 
    // total number of 
    // distinct divisors
    $ans = 1;
  
    // Loop for counting
    // factors of n
    for ($i = 0;; $i++) 
    {
  
        // a[i] is not less 
        // than cube root n
        if ($a[$i] * $a[$i] * $a[$i] > $n)
            break;
  
        // Calculating power of 
        // a[i] in n. cnt is power 
        // of prime a[i] in n.
        $cnt = 1; 
  
        // if a[i] is a factor of n
        while ($n % $a[$i] == 0) 
        {
            $n = (int)($n / $a[$i]);
              
            // incrementing power
            $cnt = $cnt + 1; 
        }
  
        // Calculating number of 
        // divisors. If n = a^p * b^q 
        // then total divisors 
        // of n are (p+1)*(q+1)
        $ans = $ans * $cnt;
    }
  
    // if a[i] is greater 
    // than cube root of n
  
    // First case
    if ($prime[$n])
        $ans = $ans * 2;
  
    // Second case
    else if ($primesquare[$n])
        $ans = $ans * 3;
  
    // Third casse
    else if ($n != 1)
        $ans = $ans * 4;
  
    return $ans; // Total divisors
}
  
// Returns sum of
// all factors of n.
function sumofFactors($n)
{
    // Traversing through 
    // all prime factors.
    $res = 1;
    for ($i = 2; 
         $i <= sqrt($n); $i++) 
    {
        $count = 0; 
        $curr_sum = 1;
        $curr_term = 1;
        while ($n % $i == 0) 
        {
            $count++;
            $n = (int)($n / $i);
  
            $curr_term *= $i;
            $curr_sum += $curr_term;
        }
  
        $res *= $curr_sum;
    }
  
    // This condition is to handle
    // the case when n is a prime
    // number greater than 2.
    if ($n >= 2)
        $res *= (1 + $n);
  
    return $res;
}
  
// Check if number is 
// Arithmetic Number or not.
function checkArithmetic($n)
{
    $count = countDivisors($n);
    $sum = sumofFactors($n);
  
    return ($sum % $count == 0);
}
  
// Driver code
$n = 6;
echo (checkArithmetic($n)) ? 
                     "Yes" : "No";
  
// This code is contributed
// by mits
?>

Output:

Yes


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.


Improved By : Mithun Kumar




Recommended Posts:



3.5 Average Difficulty : 3.5/5.0
Based on 2 vote(s)






User Actions