Dado un número entero N , la tarea es averiguar si el número dado es un número feo o no.
Los números feos son números cuyos únicos factores primos son 2, 3 o 5.
Ejemplos:
Entrada: N = 14
Salida: No
Explicación:
14 no es feo ya que incluye otro factor primo 7.Entrada: N = 6
Salida: Sí
Explicación:
6 es feo ya que incluye 2 y 3.
Enfoque: La idea es usar la recursividad para resolver este problema y verificar si un número es divisible por 2, 3 o 5. Si es así, entonces divida el número por eso y verifique recursivamente si un número es un número feo o no. Si en algún momento no existe dicho divisor, devuelve falso, de lo contrario, verdadero.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation to check if a number is an ugly number // or not #include <bits/stdc++.h> using namespace std; // 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) cout << "Yes" << endl; else cout << "No" << endl; return 0; } // This code is contributed by Sania Kumari Gupta (kriSania804)
C
// C implementation to check if a number is an ugly number // or not #include <stdio.h> // 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; } // This code is contributed by Sania Kumari Gupta (kriSania804)
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
<script> // Javascript implementation to check // if a number is an ugly // number or not // Function to check if a number // is an ugly number or not function 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 let no = isUgly(14); if (no == 1) document.write("Yes"); else document.write("No"); // This code is contributed by Mayank Tyagi </script>
No
Publicación traducida automáticamente
Artículo escrito por ShivaTeja2 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA