Supernatural

You are given a number n.

A supernatural number is a number whose product of digits is equal to n, and in this number, there is no digit 1.

Count the number of supernatural numbers for a given n.

Example:

Input:  n = 4
Output: 2

Approach

C++

#include <bits/stdc++.h>
using namespace std;

int superNatural(int n)
{
    int cnt = 0;
    for (int i = 2i <= 1000000i++)
    {
        int j = i;
        int res = 1;
        int flag = 0;
        while (j)
        {
            int x = j % 10;
            if (x == 1 || x == 0)
            {
                flag = 1;
                break;
            }
            res *= x;
            j = j / 10;
        }
        if (flag == 0 && res == n)
            cnt++;
    }
    return cnt;
}
int main()
{
    int n = 4;

    cout << superNatural(n);

    return 0;
}


No comments:

Post a Comment