给定整数N ,任务是检查N是否为Zuckerman数。
Zuckerman Number is a number which is divisible by the product of its digits
例子:
Input: N = 115
Output: Yes
Explanation:
1*1*5 = 5 and 115 % 5 = 0
Input: N = 28
Output: No
方法:想法是找到N的数字乘积,并检查N是否可被其数字的乘积整除。如果是,则数字N是祖克曼数字。
下面是上述方法的实现:
C++
// C++ implementation to check if N
// is a Zuckerman number
#include
using namespace std;
// Function to get product of digits
int getProduct(int n)
{
int product = 1;
while (n != 0) {
product = product * (n % 10);
n = n / 10;
}
return product;
}
// Function to check if N is an
// Zuckerman number
bool isZuckerman(int n)
{
return n % getProduct(n) == 0;
}
// Driver code
int main()
{
int n = 115;
if (isZuckerman(n))
cout << "Yes";
else
cout << "No";
return 0;
}
Java
// Java implementation to check if N
// is a Zuckerman number
class GFG{
// Function to get product of digits
static int getProduct(int n)
{
int product = 1;
while (n != 0)
{
product = product * (n % 10);
n = n / 10;
}
return product;
}
// Function to check if N is an
// Zuckerman number
static boolean isZuckerman(int n)
{
return n % getProduct(n) == 0;
}
// Driver code
public static void main(String[] args)
{
int n = 115;
if (isZuckerman(n))
{
System.out.println("Yes");
}
else
{
System.out.println("No");
}
}
}
// This code is contributed by shubham
Python 3
# Python3 implementation to check if N
# is a Zuckerman number
# Function to get product of digits
def getProduct(n):
product = 1
while (n > 0):
product = product * (n % 10)
n = n // 10
return product
# Function to check if N is an
# Zuckerman number
def isZuckerman(n):
return n % getProduct(n) == 0
# Driver code
N = 115
if (isZuckerman(N)):
print("Yes")
else:
print("No")
# This code is contributed by Vishal Maurya
C#
// C# implementation to check if N
// is a Zuckerman number
using System;
class GFG{
// Function to get product of digits
static int getProduct(int n)
{
int product = 1;
while (n != 0)
{
product = product * (n % 10);
n = n / 10;
}
return product;
}
// Function to check if N is an
// Zuckerman number
static bool isZuckerman(int n)
{
return n % getProduct(n) == 0;
}
// Driver code
public static void Main(String[] args)
{
int n = 115;
if (isZuckerman(n))
{
Console.WriteLine("Yes");
}
else
{
Console.WriteLine("No");
}
}
}
// This code is contributed by Rohit_ranjan
Javascript
输出
Yes
参考文献: OEIS