Equidigital Numbers

A number n is called Equidigital if the number of digits in the prime factorization of n (including powers) is same as number of digits in n.

For example 16 is an Equidigital number as its prime factorization is 2^4 and its prime factorization has total two digits (2 and 4) which is same as total number of digits in 16.
As another example, 128 is not a Equidigital as its prime factorization is 2^7 and has total 2 digits (2 and 7), but number has 3 digits.

All prime numbers are Equidigital.



Given a number n, the task is to print all Equidigital numbers upto n.
Examples:

Input: n = 10 
Output: 1, 2, 3, 5, 7, 10.
Note that 4, 6, 8 and 9 are not Equidigital.

Input :  n = 20
Output :  1 2 3 5 7 10 11 13 14 15 16 17 19

Method (To print all Equidigital numbers less than or equal to n)

  1. Count all primes upto 10^6 using Sieve of Sundaram.
  2. Find number of digits in n.
  3. Find all prime factors of n and do following for every prime factor p.
    • Find number of digits in p.
    • Count highest power of p that divides n.
    • Find sum of above two.
  4. Compare two sums. If two sums are same, then return true.

Below is implementation of above idea.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ Program to find Equidigital Numbers till n
#include<bits/stdc++.h>
using namespace std;
const int MAX  = 10000;
  
// Array to store all prime less than and equal to MAX.
vector <int> primes;
  
// Utility function for sieve of sundaram
void sieveSundaram()
{
    // In general Sieve of Sundaram, produces primes smaller
    // than (2*x + 2) for a number given number x. Since
    // we want primes smaller than MAX, we reduce MAX to half
    // This array is used to separate numbers of the form
    // i+j+2ij from others where 1 <= i <= j
    bool marked[MAX/2 + 1] = {0};
  
    // Main logic of Sundaram. Mark all numbers which
    // do not generate prime number by doing 2*i+1
    for (int i=1; i<=(sqrt(MAX)-1)/2; i++)
        for (int j=(i*(i+1))<<1; j<=MAX/2; j=j+2*i+1)
            marked[j] = true;
  
    // Since 2 is a prime number
    primes.push_back(2);
  
    // Print other primes. Remaining primes are of the
    // form 2*i + 1 such that marked[i] is false.
    for (int i=1; i<=MAX/2; i++)
        if (marked[i] == false)
            primes.push_back(2*i + 1);
}
  
// Returns true if n is a Equidigital number, else
// false.
bool isEquidigital(int n)
{
    if (n == 1)
        return true;
  
    // Count digits in original number
    int original_no = n;
    int sumDigits = 0;
    while (original_no > 0)
    {
        sumDigits++;
        original_no = original_no/10;
    }
  
  
    // Count all digits in prime factors of n
    // pDigit is going to hold this value.
    int pDigit = 0 , count_exp = 0, p;
    for (int i = 0; primes[i] <= n/2; i++)
    {
        // Count powers of p in n
        while (n % primes[i] == 0)
        {
            // If primes[i] is a prime factor,
            p = primes[i];
            n = n/p;
  
            // Count the power of prime factors
            count_exp++;
        }
  
        // Add its digits to pDigit.
        while (p > 0)
        {
            pDigit++;
            p = p / 10;
        }
  
        // Add digits of power of prime factors to pDigit.
        while (count_exp > 1)
        {
            pDigit++;
            count_exp = count_exp / 10;
        }
    }
  
    // If n!=1 then one prime factor still to be
    // summed up;
    if (n != 1)
    {
        while (n > 0)
        {
            pDigit++;
            n = n/10;
        }
    }
  
    // If digits in prime factors and
    // digits in original number are same, then
    // return true. Else return false.
    return (pDigit == sumDigits);
}
  
// Driver code
int main()
{
    // Finding all prime numbers before limit. These
    // numbers are used to find prime factors.
    sieveSundaram();
  
    cout << "Printing first few Equidigital Numbers"
         " using isEquidigital()\n";
    for (int i=1; i<20; i++)
        if (isEquidigital(i))
            cout << i << " ";
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find Equidigital Numbers till n
  
import java.util.Vector;
import static java.lang.Math.sqrt;
  
class GFG 
{
    static final int MAX = 10000;
    static Vector<Integer> primes = new Vector<Integer>(MAX+1);
      
    // Utility function for sieve of sundaram
    static void sieveSundaram()
    {
        // In general Sieve of Sundaram, produces primes smaller
        // than (2*x + 2) for a number given number x. Since
        // we want primes smaller than MAX, we reduce MAX to half
        // This array is used to separate numbers of the form
        // i+j+2ij from others where 1 <= i <= j
        boolean marked[] = new boolean[MAX/2 + 1];
       
        // Main logic of Sundaram. Mark all numbers which
        // do not generate prime number by doing 2*i+1
        for (int i=1; i<=(sqrt(MAX)-1)/2; i++)
            for (int j=(i*(i+1))<<1; j<=MAX/2; j=j+2*i+1)
                marked[j] = true;
       
        // Since 2 is a prime number
        primes.add(2);
       
        // Print other primes. Remaining primes are of the
        // form 2*i + 1 such that marked[i] is false.
        for (int i=1; i<=MAX/2; i++)
            if (marked[i] == false)
                primes.add(2*i + 1);
    }
       
    // Returns true if n is a Equidigital number, else
    // false.
    static boolean isEquidigital(int n)
    {
        if (n == 1)
            return true;
       
        // Count digits in original number
        int original_no = n;
        int sumDigits = 0;
        while (original_no > 0)
        {
            sumDigits++;
            original_no = original_no/10;
        }
       
       
        // Count all digits in prime factors of n
        // pDigit is going to hold this value.
        int pDigit = 0 , count_exp = 0, p = 0;
        for (int i = 0; primes.elementAt(i) <= n/2; i++)
        {
            // Count powers of p in n
            while (n % primes.elementAt(i) == 0)
            {
                // If primes[i] is a prime factor,
                p = primes.elementAt(i);
                n = n/p;
       
                // Count the power of prime factors
                count_exp++;
            }
       
            // Add its digits to pDigit.
            while (p > 0)
            {
                pDigit++;
                p = p / 10;
            }
       
            // Add digits of power of prime factors to pDigit.
            while (count_exp > 1)
            {
                pDigit++;
                count_exp = count_exp / 10;
            }
        }
       
        // If n!=1 then one prime factor still to be
        // summed up;
        if (n != 1)
        {
            while (n > 0)
            {
                pDigit++;
                n = n/10;
            }
        }
       
        // If digits in prime factors and
        // digits in original number are same, then
        // return true. Else return false.
        return (pDigit == sumDigits);
    }
      
    // Driver method
    public static void main (String[] args)
    {
       // Finding all prime numbers before limit. These
       // numbers are used to find prime factors.
       sieveSundaram();
       
       System.out.println("Printing first few Equidigital Numbers" +
                           " using isEquidigital()");
         
       for (int i=1; i<20; i++)
           if (isEquidigital(i))
               System.out.print(i + " ");
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 Program to find Equidigital 
# Numbers till n 
import math
MAX = 10000
  
# Array to store all prime less 
# than and equal to MAX. 
primes = []; 
  
# Utility function for sieve of sundaram 
def sieveSundaram(): 
  
    # In general Sieve of Sundaram, produces 
    # primes smaller than (2*x + 2) for a number 
    # given number x. Since we want primes smaller 
    # than MAX, we reduce MAX to half. This array 
    # is used to separate numbers of the form 
    # i+j+2ij from others where 1 <= i <= j 
    marked = [False] * int(MAX / 2 + 1); 
  
    # Main logic of Sundaram. Mark all numbers which 
    # do not generate prime number by doing 2*i+1 
    for i in range(1, int((math.sqrt(MAX) - 1) / 2) + 1): 
        for j in range((i * (i + 1)) << 1
                    int(MAX / 2) + 1, 2 * i + 1): 
            marked[j] = True
  
    # Since 2 is a prime number 
    primes.append(2); 
  
    # Print other primes. Remaining primes 
    # are of the form 2*i + 1 such that 
    # marked[i] is false. 
    for i in range(1, int(MAX / 2) + 1): 
        if (marked[i] == False): 
            primes.append(2 * i + 1); 
  
# Returns true if n is a Equidigital 
# number, else false. 
def isEquidigital(n):
  
    if (n == 1): 
        return True
  
    # Count digits in original number 
    original_no = n; 
    sumDigits = 0
    while (original_no > 0):
        sumDigits += 1
        original_no = int(original_no / 10);
  
    # Count all digits in prime factors of n 
    # pDigit is going to hold this value. 
    pDigit = 0
    count_exp = 0
    p = 0;
    i = 0;
    while (primes[i] <= int(n / 2)): 
          
        # Count powers of p in n 
        while (n % primes[i] == 0):
              
            # If primes[i] is a prime factor, 
            p = primes[i]; 
            n = int(n / p); 
  
            # Count the power of prime factors 
            count_exp += 1;
  
        # Add its digits to pDigit. 
        while (p > 0): 
            pDigit += 1
            p = int(p / 10); 
  
        # Add digits of power of prime 
        # factors to pDigit. 
        while (count_exp > 1): 
            pDigit += 1
            count_exp = int(count_exp / 10);
        i += 1;
  
    # If n!=1 then one prime factor 
    # still to be summed up; 
    if (n != 1): 
        while (n > 0):
            pDigit += 1
            n = int(n / 10); 
  
    # If digits in prime factors and 
    # digits in original number are same, 
    # then return true. Else return false. 
    return (pDigit == sumDigits); 
  
# Driver code 
  
# Finding all prime numbers before 
# limit. These numbers are used to
# find prime factors. 
sieveSundaram(); 
  
print("Printing first few Equidigital"
      "Numbers using isEquidigital()"); 
for i in range(1, 20): 
    if (isEquidigital(i)): 
        print(i, end = " "); 
          
# This code is contributed by mits

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find Equidigital Numbers till n
using System; 
using System.Collections;
  
public class GFG 
{
    static int MAX = 10000;
    static ArrayList primes = new ArrayList(MAX+1);
      
    // Utility function for sieve of sundaram
    static void sieveSundaram()
    {
        // In general Sieve of Sundaram, produces primes smaller
        // than (2*x + 2) for a number given number x. Since
        // we want primes smaller than MAX, we reduce MAX to half
        // This array is used to separate numbers of the form
        // i+j+2ij from others where 1 <= i <= j
        bool[] marked = new bool[MAX/2 + 1];
      
        // Main logic of Sundaram. Mark all numbers which
        // do not generate prime number by doing 2*i+1
        for (int i=1; i<=(Math.Sqrt(MAX)-1)/2; i++)
            for (int j=(i*(i+1))<<1; j<=MAX/2; j=j+2*i+1)
                marked[j] = true;
      
        // Since 2 is a prime number
        primes.Add(2);
      
        // Print other primes. Remaining primes are of the
        // form 2*i + 1 such that marked[i] is false.
        for (int i=1; i<=MAX/2; i++)
            if (marked[i] == false)
                primes.Add(2*i + 1);
    }
      
    // Returns true if n is a Equidigital number, else
    // false.
    static bool isEquidigital(int n)
    {
        if (n == 1)
            return true;
      
        // Count digits in original number
        int original_no = n;
        int sumDigits = 0;
        while (original_no > 0)
        {
            sumDigits++;
            original_no = original_no/10;
        }
      
      
        // Count all digits in prime factors of n
        // pDigit is going to hold this value.
        int pDigit = 0 , count_exp = 0, p = 0;
        for (int i = 0; (int)primes[i] <= n/2; i++)
        {
            // Count powers of p in n
            while (n % (int)primes[i] == 0)
            {
                // If primes[i] is a prime factor,
                p = (int)primes[i];
                n = n/p;
      
                // Count the power of prime factors
                count_exp++;
            }
      
            // Add its digits to pDigit.
            while (p > 0)
            {
                pDigit++;
                p = p / 10;
            }
      
            // Add digits of power of prime factors to pDigit.
            while (count_exp > 1)
            {
                pDigit++;
                count_exp = count_exp / 10;
            }
        }
      
        // If n!=1 then one prime factor still to be
        // summed up;
        if (n != 1)
        {
            while (n > 0)
            {
                pDigit++;
                n = n/10;
            }
        }
      
        // If digits in prime factors and
        // digits in original number are same, then
        // return true. Else return false.
        return (pDigit == sumDigits);
    }
      
    // Driver method
    public static void Main()
    {
    // Finding all prime numbers before limit. These
    // numbers are used to find prime factors.
    sieveSundaram();
      
    Console.WriteLine("Printing first few Equidigital Numbers using isEquidigital()");
          
    for (int i=1; i<20; i++)
        if (isEquidigital(i))
            Console.Write(i + " ");
    }
}
// This Code is contributed by mits

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP Program to find Equidigital Numbers till n
$MAX = 10000;
  
// Array to store all prime less than and equal to MAX.
$primes=array();
  
// Utility function for sieve of sundaram
function sieveSundaram()
{
    global $primes,$MAX;
    // In general Sieve of Sundaram, produces primes smaller
    // than (2*x + 2) for a number given number x. Since
    // we want primes smaller than MAX, we reduce MAX to half
    // This array is used to separate numbers of the form
    // i+j+2ij from others where 1 <= i <= j
    $marked=array_fill(0,($MAX/2 + 1),false);
  
    // Main logic of Sundaram. Mark all numbers which
    // do not generate prime number by doing 2*i+1
    for ($i=1; $i<=((int)sqrt($MAX)-1)/2; $i++)
        for ($j=($i*($i+1))<<1; $j<=(int)($MAX/2); $j=$j+2*$i+1)
            $marked[$j] = true;
  
    // Since 2 is a prime number
    array_push($primes,2);
  
    // Print other primes. Remaining primes are of the
    // form 2*i + 1 such that marked[i] is false.
    for ($i=1; $i<=(int)($MAX/2); $i++)
        if ($marked[$i] == false)
            array_push($primes,2*$i + 1);
}
  
// Returns true if n is a Equidigital number, else
// false.
function isEquidigital($n)
{
    global $primes,$MAX;
    if ($n == 1)
        return true;
  
    // Count digits in original number
    $original_no = $n;
    $sumDigits = 0;
    while ($original_no > 0)
    {
        $sumDigits++;
        $original_no = (int)($original_no/10);
    }
  
  
    // Count all digits in prime factors of n
    // pDigit is going to hold this value.
    $pDigit = 0;
    $count_exp = 0;
    $p=0;
    for ($i = 0; $primes[$i] <= (int)($n/2); $i++)
    {
        // Count powers of p in n
        while ($n % $primes[$i] == 0)
        {
            // If primes[i] is a prime factor,
            $p = $primes[$i];
            $n = (int)($n/$p);
  
            // Count the power of prime factors
            $count_exp++;
        }
  
        // Add its digits to pDigit.
        while ($p > 0)
        {
            $pDigit++;
            $p =(int)($p / 10);
        }
  
        // Add digits of power of prime factors to pDigit.
        while ($count_exp > 1)
        {
            $pDigit++;
            $count_exp = (int)($count_exp / 10);
        }
    }
  
    // If n!=1 then one prime factor still to be
    // summed up;
    if ($n != 1)
    {
        while ($n > 0)
        {
            $pDigit++;
            $n = (int)($n/10);
        }
    }
  
    // If digits in prime factors and
    // digits in original number are same, then
    // return true. Else return false.
    return ($pDigit == $sumDigits);
}
  
// Driver code
  
    // Finding all prime numbers before limit. These
    // numbers are used to find prime factors.
    sieveSundaram();
  
    echo "Printing first few Equidigital Numbers using isEquidigital()\n";
    for ($i=1; $i<20; $i++)
        if (isEquidigital($i))
            echo $i." ";
              
              
// This code is contributed by mits
?>

chevron_right



Output:

Printing first few Equidigital Numbers using isEquidigital()
1 2 3 5 7 10 11 13 14 15 16 17 19

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

This article is contributed by Sahil Chhabra(KILLER). 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.



My Personal Notes arrow_drop_up

Improved By : Mithun Kumar