Count digits in given number N which divide N - GeeksforGeeks

26 Mar, 2021
Given a number N which may be 10^5 digits long, the task is to count all the digits in N which divide N. Divisibility by 0 is not allowed. If any digit in N which is repeated divides N, then all repetitions of that digit should be counted i. e., N = 122324, here 2 divides N and it appears 3 times. So count for digit 2 will be 3.Examples:  
Input  : N = "35"
Output : 1
There are two digits in N and 1 of them
(5)divides it.

Input  : N = "122324"
Output : 5
N is divisible by 1, 2 and 4
 
How to check divisibility of a digit for large N stored as string? The idea is to use distributive property of mod operation. (x+y)%a = ((x%a) + (y%a)) % a. 
// This function returns true if digit divides N, 
// else false
bool divisible(string N, int digit)
{
    int ans = 0;
    for (int i = 0; i < N.length(); i++)
    {     
        // (N[i]-'0') gives the digit value and
        // form the number       
        ans  = (ans*10 + (N[i]-'0'));

        // We use distributive property of mod here. 
        ans %= digit;
    }
    return (ans == 0);
}
A simple solution for this problem is to read number in string form and one by one check divisibility by each digit which appears in N. Time complexity for this approach will be O(N2).An efficient solution for this problem is to use an extra array divide[] of size 10. Since we have only 10 digits so run a loop from 1 to 9 and check divisibility of N with each digit from 1 to 9. If any digit divides N then mark true in divide[] array at digit as index. Now traverse the number string and increment result if divide[i] is true for current digit i.  
C++
Java
Python3
C#
PHP
Javascript
// C++ program to find number of digits in N that// divide N.#include<bits/stdc++.h>using namespace std; // Utility function to check divisibility by digitbool divisible(string N, int digit){    int ans = 0;    for (int i = 0; i < N.length(); i++)    {        // (N[i]-'0') gives the digit value and        // form the number        ans  = (ans*10 + (N[i]-'0'));        ans %= digit;    }    return (ans == 0);} // Function to count digits which appears in N and// divide N// divide[10]  --> array which tells that particular//                 digit divides N or not// count[10]   --> counts frequency of digits which//                 divide Nint allDigits(string N){    // We initialize all digits of N as not divisible    // by N.    bool divide[10] = {false};    divide[1] = true;  // 1 divides all numbers     // start checking divisibility of N by digits 2 to 9    for (int digit=2; digit<=9; digit++)    {        // if digit divides N then mark it as true        if (divisible(N, digit))            divide[digit] = true;    }     // Now traverse the number string to find and increment    // result whenever a digit divides N.    int result = 0;    for (int i=0; i<N.length(); i++)    {        if (divide[N[i]-'0'] == true)            result++;    }     return result;} // Driver program to run the caseint main(){    string N = "122324";    cout << allDigits(N);    return 0;}
// Java program to find number of digits in N that// divide N.import java.util.*; class solution{ // Utility function to check divisibility by digitstatic boolean divisible(String N, int digit){    int ans = 0;    for (int i = 0; i < N.length(); i++)    {        // (N[i]-'0') gives the digit value and        // form the number        ans = (ans*10 + (N.charAt(i)-'0'));        ans %= digit;    }    return (ans == 0);} // Function to count digits which appears in N and// divide N// divide[10] --> array which tells that particular//                 digit divides N or not// count[10] --> counts frequency of digits which//                 divide Nstatic int allDigits(String N){    // We initialize all digits of N as not divisible    // by N.    Boolean[] divide = new Boolean[10];    Arrays.fill(divide, Boolean.FALSE);    divide[1] = true; // 1 divides all numbers     // start checking divisibility of N by digits 2 to 9    for (int digit=2; digit<=9; digit++)    {        // if digit divides N then mark it as true        if (divisible(N, digit))            divide[digit] = true;    }     // Now traverse the number string to find and increment    // result whenever a digit divides N.    int result = 0;    for (int i=0; i<N.length(); i++)    {        if (divide[N.charAt(i)-'0'] == true)            result++;    }     return result;} // Driver program to run the casepublic static void main(String args[]){    String N = "122324";    System.out.println(allDigits(N));} }// This code is contributed by Surendra_Gangwar
# Python3 program to find number of# digits in N that divide N. # Utility function to check# divisibility by digitdef divisible(N, digit):      ans = 0;    for i in range(len(N)):        # (N[i]-'0') gives the digit        # value and form the number        ans = (ans * 10 + (ord(N[i]) - ord('0')));        ans %= digit;    return (ans == 0); # Function to count digits which# appears in N and divide N# divide[10] --> array which tells# that particular digit divides N or not# count[10] --> counts frequency of#                 digits which divide Ndef allDigits(N):      # We initialize all digits of N    # as not divisible by N.    divide =[False]*10;    divide[1] = True; # 1 divides all numbers     # start checking divisibility of    # N by digits 2 to 9    for digit in range(2,10):        # if digit divides N then        # mark it as true        if (divisible(N, digit)):            divide[digit] = True;     # Now traverse the number string to    # find and increment result whenever    # a digit divides N.    result = 0;    for i in range(len(N)):              if (divide[(ord(N[i]) - ord('0'))] == True):            result+=1;     return result; # Driver CodeN = "122324";print(allDigits(N)); # This code is contributed by mits
// C# program to find number of digits// in N that divide N.using System; class GFG {     // Utility function to// check divisibility by digitstatic bool divisible(string N, int digit){    int ans = 0;    for (int i = 0; i < N.Length; i++)    {                 // (N[i]-'0') gives the digit value and        // form the number        ans = (ans * 10 + (N[i] - '0'));        ans %= digit;    }    return (ans == 0);} // Function to count digits which// appears in N and divide N// divide[10] --> array which// tells that particular// digit divides N or not// count[10] --> counts// frequency of digits which// divide Nstatic int allDigits(string N){         // We initialize all digits    // of N as not divisible by N    bool[] divide = new bool[10];         for (int i = 0; i < divide.Length; i++)    {        divide[i] = false;    }         // 1 divides all numbers    divide[1] = true;     // start checking divisibility    // of N by digits 2 to 9    for (int digit = 2; digit <= 9; digit++)    {                 // if digit divides N        // then mark it as true        if (divisible(N, digit))            divide[digit] = true;    }     // Now traverse the number    // string to find and increment    // result whenever a digit divides N.    int result = 0;         for (int i = 0; i < N.Length; i++)    {        if (divide[N[i] - '0'] == true)            result++;    }     return result;} // Driver Codepublic static void Main(){    string N = "122324";    Console.Write(allDigits(N));}} // This code is contributed// by Akanksha Rai(Abby_akku)
<?php// PHP program to find number of// digits in N that divide N. // Utility function to check// divisibility by digitfunction divisible($N, $digit){    $ans = 0;    for ($i = 0; $i < strlen($N); $i++)    {        // (N[i]-'0') gives the digit        // value and form the number        $ans = ($ans * 10 + (int)($N[$i] - '0'));        $ans %= $digit;    }    return ($ans == 0);} // Function to count digits which// appears in N and divide N// divide[10] --> array which tells// that particular digit divides N or not// count[10] --> counts frequency of//                 digits which divide Nfunction allDigits($N){    // We initialize all digits of N    // as not divisible by N.    $divide = array_fill(0, 10, false);    $divide[1] = true; // 1 divides all numbers     // start checking divisibility of    // N by digits 2 to 9    for ($digit = 2; $digit <= 9; $digit++)    {        // if digit divides N then        // mark it as true        if (divisible($N, $digit))            $divide[$digit] = true;    }     // Now traverse the number string to    // find and increment result whenever    // a digit divides N.    $result = 0;    for ($i = 0; $i < strlen($N); $i++)    {        if ($divide[(int)($N[$i] - '0')] == true)            $result++;    }     return $result;} // Driver Code$N = "122324";echo allDigits($N); // This code is contributed by mits?>
<script> // JavaScript program to find number of digits// in N that divide N. // Utility function to// check divisibility by digitfunction divisible(N, digit){    let ans = 0;    for (let i = 0; i < N.length; i++)    {                   // (N[i]-'0') gives the digit value and        // form the number        ans = (ans * 10 + (N[i] - '0'));        ans %= digit;    }    return (ans == 0);}   // Function to count digits which// appears in N and divide N// divide[10] --> array which// tells that particular// digit divides N or not// count[10] --> counts// frequency of digits which// divide Nfunction allDigits(N){           // We initialize all digits    // of N as not divisible by N    let divide = [];           for (let i = 0; i < divide.length; i++)    {        divide[i] = false;    }           // 1 divides all numbers    divide[1] = true;       // start checking divisibility    // of N by digits 2 to 9    for (let digit = 2; digit <= 9; digit++)    {                   // if digit divides N        // then mark it as true        if (divisible(N, digit))            divide[digit] = true;    }       // Now traverse the number    // string to find and increment    // result whenever a digit divides N.    let result = 0;           for (let i = 0; i < N.length; i++)    {        if (divide[N[i] - '0'] == true)            result++;    }       return result;}     // Driver Code    let N = "122324";    document.write(allDigits(N));         // This code is contributed by chinmoy1997pal.</script>
Output : 
5
Time Complexity: O(n) Auxiliary space: O(1)This article is contributed by Shashank Mishra ( Gullu ). If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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. 
Mithun Kumar
Akanksha_Rai
SURENDRA_GANGWAR
ManasChhabra2
chinmoy1997pal
number-digits
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Modulo Operator (%) in C/C++ with Examples
Merge two sorted arrays
Prime Numbers
Program to find sum of elements in a given array
Operators in C / C++
Program for factorial of a number
Euclidean algorithms (Basic and Extended)
Program for Decimal to Binary Conversion
Algorithm to solve Rubik's Cube
Write a program to calculate pow(x,n)