📜  检查给定的号码是否是丑陋的号码

📅  最后修改于: 2021-04-29 07:54:03             🧑  作者: Mango

给定整数N ,任务是找出给定的数字是否为Ugly数字。

例子:

方法:想法是使用递归来解决此问题,并检查数字是否可被2、3或5整除。如果是,则将其除以该数字,然后递归检查数字是否为丑数。如果在任何时候都没有这样的除数,则返回false,否则返回true。

下面是上述方法的实现:

C++
// C++ implementation to check
// if a number is an ugly
// number or not
 
#include 
#include 
 
// Function to check if a number
// is an ugly number or not
int isUgly(int n)
{
    // Base Cases
    if (n == 1)
        return 1;
    if (n <= 0)
        return 0;
 
    // Condition to check if the
    // number is divided by 2, 3, or 5
    if (n % 2 == 0) {
        return (isUgly(n / 2));
    }
    if (n % 3 == 0) {
        return (isUgly(n / 3));
    }
    if (n % 5 == 0) {
        return (isUgly(n / 5));
    }
 
    // Otherwise return false
    return 0;
}
// Driver Code
int main()
{
    int no = isUgly(14);
    if (no == 1)
        printf("Yes");
    else
        printf("No");
    return 0;
}


Java
// Java implementation to
// check if a number is ugly number
class GFG {
 
    // Function to check the ugly
    // number
    static int isUgly(int n)
    {
        // Base Cases
        if (n == 1)
            return 1;
        if (n <= 0)
            return 0;
 
        // Condition to check if
        // a number is divide by
        // 2, 3, or 5
        if (n % 2 == 0) {
            return (isUgly(n / 2));
        }
        if (n % 3 == 0) {
            return (isUgly(n / 3));
        }
        if (n % 5 == 0) {
            return (isUgly(n / 5));
        }
        return 0;
    }
 
    // Driver Code
    public static void main(String args[])
    {
        int no = isUgly(14);
        if (no == 1)
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}


Python3
# Python3 implementation to check
# if a number is an ugly number
# or not
 
# Function to check if a number
# is an ugly number or not
def isUgly(n):
 
    # Base Cases
    if (n == 1):
        return 1
    if (n <= 0):
        return 0
 
    # Condition to check if the
    # number is divided by 2, 3, or 5
    if (n % 2 == 0):
        return (isUgly(n // 2))
         
    if (n % 3 == 0):
        return (isUgly(n // 3))
     
    if (n % 5 == 0):
        return (isUgly(n // 5))
 
    # Otherwise return false
    return 0
 
# Driver Code
if __name__ == "__main__":
 
    no = isUgly(14)
     
    if (no == 1):
        print("Yes")
    else:
        print("No")
 
# This code is contributed by chitranayal


C#
// C# implementation to check
// if a number is ugly number
using System;
 
class GFG{
 
// Function to check the ugly
// number
static int isUgly(int n)
{
     
    // Base Cases
    if (n == 1)
        return 1;
    if (n <= 0)
        return 0;
 
    // Condition to check if
    // a number is divide by
    // 2, 3, or 5
    if (n % 2 == 0)
    {
        return (isUgly(n / 2));
    }
    if (n % 3 == 0)
    {
        return (isUgly(n / 3));
    }
    if (n % 5 == 0)
    {
        return (isUgly(n / 5));
    }
    return 0;
}
 
// Driver Code
public static void Main(String []args)
{
    int no = isUgly(14);
     
    if (no == 1)
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
}
 
// This code is contributed by amal kumar choubey


Javascript


输出:
No