Find all the possible numbers in a range that can be evenly divided by its digits

Given two integers n and m which represent a range where n is the lower bound and m is the upper bound respectively. The task is to find all the possible numbers lie between n and m that can be evenly divided by its digits.

Examples:

Input: n = 1, m = 15
Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15]
Explanation:
The numbers in the array can be divided evenly by its digits except
2 numbers 10 and 13 which lie between 1 and 15.
10 can be broken into its digits 1 and 0.
10 % 1 == 0 but 10 % 0 != 0.
13 can be broken into its digits 1 and 3.
13 % 1 == 0 but 13 % 3 != 0.

Input: n = 21, m = 25
Output: [22, 24]
Explanation:
The numbers in the array can be divided evenly by its digits except
there are 3 numbers 21, 23 and 25 lie between 21 and 25.
21 can be broken into its digits 2 and 1.
21 % 2 != 0 and 21 % 1 != 0
23 can be broken into its digits 2 and 3.
23 % 2 != 0 and 23 % 3 != 0.
25 can be broken into its digits 2 and 5.
25 % 2 != 0 but 25 % 5 == 0.

Approach:



The main idea is to iterate each and every number from left to right. For each number convert it into the string followed by a character array and check whether it contains 0 if it contains 0 ignore it. Otherwise, for each number check, whether the number is evenly divided by its digits or not. If the number is evenly divided by its digits then add into the list else discard it. Finally, return the list.

Below is the implementation of the above approach:

Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find all the possible numbers
// that can be evenly divided by its digits
  
import java.util.*;
class GFG {
  
    // Function to check each and every number
    // from left to right. If the number is 
    // divisible by its digits
    // then the number is added into the list
    static List<Integer> selfDividingNumber(int left, 
                                            int right)
    {
  
        List<Integer> list = new ArrayList<Integer>();
        for (int i = left; i <= right; i++) {
            if (isDivisible(i)) {
                list.add(i);
            }
        }
        return list;
    }
  
    // Function to check whether the number
    // is evenly divisible by its digits or not.
    static boolean isDivisible(int num)
    {
  
        // Iterate each number convert number
        // into string and then to character array.
        for (char c : String.valueOf(num).toCharArray()) {
            if (c == '0' || num % (c - '0') > 0) {
                return false;
            }
        }
        return true;
    }
  
    // Driver Code
    public static void main(String args[])
    {
  
        // initialise range
        int n1 = 1, m1 = 15;
  
        System.out.print(selfDividingNumber(n1, m1));
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find all the possible numbers 
# that can be evenly divided by its digits 
  
# Function to check each and every number 
# from left to right. If the number is 
# divisible by its digits 
# then the number is added into the list 
def selfDividingNumber(left, right) :
    array_list = [];
      
    for i in range(left, right + 1) :
        if (isDivisible(i)) :
            array_list.append(i);
              
    return array_list;
      
# Function to check whether the number
# is evenly divisible by its digits or not.
def isDivisible(num) :
      
    # Iterate each number convert number
    # into string and then to character array.
    for c in list(str(num)) :
        if (c == '0' or num % (ord(c) - ord('0')) > 0):
            return False;
              
    return True;
      
# Driver Code 
if __name__ == "__main__" :
      
    # Initialise range 
    n1 = 1; m1 = 15;
    print(selfDividingNumber(n1, m1)); 
  
# This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find all the 
// possible numbers that can
// be evenly divided by its digits
using System;
using System.Collections.Generic;
  
public class GFG {
  
// Function to check each and every number
// from left to right. If the number is 
// divisible by its digits then the number
// is added into the list
static List<int> selfDividingNumber(int left, 
                                    int right)
{
    List<int> list = new List<int>();
    for(int i = left; i <= right; i++)
    {
       if (isDivisible(i))
       {
           list.Add(i);
       }
    }
    return list;
}
  
// Function to check whether the number
// is evenly divisible by its digits or not.
static bool isDivisible(int num)
{
  
    // Iterate each number convert number
    // into string and then to character array.
    foreach(char c in String.Join("", num).ToCharArray())
    {
        if (c == '0' || num % (c - '0') > 0)
        {
            return false;
        }
    }
    return true;
}
  
// Driver Code
public static void Main(String []args)
{
  
    // Initialise range
    int n1 = 1, m1 = 15;
    List<int> t = selfDividingNumber(n1, m1);
      
    foreach(int val in t)
        Console.Write(val + ", ");
}
}
  
// This code is contributed by sapnasingh4991

chevron_right


Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15]

Time Complexity: O(N), where N is the number of integers from left to right.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Recommended Posts:


External Technical Content Reviewer at GeeksforGeeks

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : AnkitRai01, sapnasingh4991

Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.