Techno Blender
Digitally Yours.

Count of integer whose factorial of digit sum contains every digit of original number

0 43


  

import java.util.*;

  

class GFG {

  

    

    public static void main(String[] args)

    {

        int n = 7;

        int[] arr

            = { 833, 3055, 8521, 360, 2202, 310, 2111 };

  

        

        System.out.println(funtionTOFindInt(arr, n));

    }

  

    

    public static int funtionTOFindInt(int[] arr, int n)

    {

  

        

        

        Set<Integer>[] factDigits = new HashSet[10];

        for (int i = 1; i < 10; i++) {

            int fact = factorial(i);

            factDigits[i] = new HashSet<>();

            while (fact != 0) {

                factDigits[i].add(fact % 10);

                fact /= 10;

            }

        }

  

        

        int count = 0;

        for (int i = 0; i < n; i++) {

            int x = arr[i];

            while (x >= 10) {

                x = getSum(x);

            }

            Set<Integer> digits = factDigits[x];

            boolean flag = true;

            while (arr[i] > 0) {

                int dig = arr[i] % 10;

                if (!digits.contains(dig)) {

                    flag = false;

                    break;

                }

                arr[i] /= 10;

            }

            if (flag)

                count++;

        }

  

        return count;

    }

  

    

    public static int factorial(int n)

    {

        return (n == 1 || n == 0) ? 1

                                  : n * factorial(n - 1);

    }

  

    

    public static int getSum(int n)

    {

        int sum = 0;

  

        while (n != 0) {

            sum = sum + n % 10;

            n = n / 10;

        }

  

        

        return sum;

    }

}


  

import java.util.*;

  

class GFG {

  

    

    public static void main(String[] args)

    {

        int n = 7;

        int[] arr

            = { 833, 3055, 8521, 360, 2202, 310, 2111 };

  

        

        System.out.println(funtionTOFindInt(arr, n));

    }

  

    

    public static int funtionTOFindInt(int[] arr, int n)

    {

  

        

        

        Set<Integer>[] factDigits = new HashSet[10];

        for (int i = 1; i < 10; i++) {

            int fact = factorial(i);

            factDigits[i] = new HashSet<>();

            while (fact != 0) {

                factDigits[i].add(fact % 10);

                fact /= 10;

            }

        }

  

        

        int count = 0;

        for (int i = 0; i < n; i++) {

            int x = arr[i];

            while (x >= 10) {

                x = getSum(x);

            }

            Set<Integer> digits = factDigits[x];

            boolean flag = true;

            while (arr[i] > 0) {

                int dig = arr[i] % 10;

                if (!digits.contains(dig)) {

                    flag = false;

                    break;

                }

                arr[i] /= 10;

            }

            if (flag)

                count++;

        }

  

        return count;

    }

  

    

    public static int factorial(int n)

    {

        return (n == 1 || n == 0) ? 1

                                  : n * factorial(n - 1);

    }

  

    

    public static int getSum(int n)

    {

        int sum = 0;

  

        while (n != 0) {

            sum = sum + n % 10;

            n = n / 10;

        }

  

        

        return sum;

    }

}

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.

Leave a comment