Dado un número n, encuentre si todos los dígitos de n lo dividen o no.
Ejemplos:
Input : 128 Output : Yes 128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0. Input : 130 Output : No
Queremos probar si cada dígito es distinto de cero y divide el número. Por ejemplo, con 128, queremos probar d != 0 && 128 % d == 0 para d = 1, 2, 8. Para hacer eso, necesitamos iterar sobre cada dígito del número.
CPP
// CPP program to check the number // is divisible by all digits are not. #include <bits/stdc++.h> using namespace std; // Function to check the divisibility // of the number by its digit. bool checkDivisibility(int n, int digit) { // If the digit divides the number // then return true else return false. return (digit != 0 && n % digit == 0); } // Function to check if all digits // of n divide it or not bool allDigitsDivide(int n) { int temp = n; while (temp > 0) { // Taking the digit of the // number into digit var. int digit = temp % 10; if (!(checkDivisibility(n, digit))) return false; temp /= 10; } return true; } // Driver function int main() { int n = 128; if (allDigitsDivide(n)) cout << "Yes"; else cout << "No"; return 0; }
Java
// Java program to check whether // number is divisible by all its digits. import java.io.*; class GFG { // Function to check the divisibility // of the number by its digit. static boolean checkDivisibility(int n, int digit) { // If the digit divides the number // then return true else return false. return (digit != 0 && n % digit == 0); } // Function to check if all // digits of n divide it or not, static boolean allDigitsDivide(int n) { int temp = n; while (temp > 0) { // Taking the digit of the // number into var 'digit'. int digit = temp % 10; if ((checkDivisibility(n, digit)) == false) return false; temp /= 10; } return true; } // Driver function public static void main(String args[]) { int n = 128; // function call to check // digits divisibility if (allDigitsDivide(n)) System.out.println("Yes"); else System.out.println("No"); } } /*This code is contributed by Nikita Tiwari.*/
Python3
# Python 3 program to # check the number is # divisible by all # digits are not. # Function to check # the divisibility # of the number by # its digit. def checkDivisibility(n, digit) : # If the digit divides the # number then return true # else return false. return (digit != 0 and n % digit == 0) # Function to check if # all digits of n divide # it or not def allDigitsDivide( n) : temp = n while (temp > 0) : # Taking the digit of # the number into digit # var. digit = temp % 10 if ((checkDivisibility(n, digit)) == False) : return False temp = temp // 10 return True # Driver function n = 128 if (allDigitsDivide(n)) : print("Yes") else : print("No" ) # This code is contributed by Nikita Tiwari.
C#
// C# program to check whether // number is divisible by all its digits. using System; class GFG { // Function to check the divisibility // of the number by its digit. static bool checkDivisibility(int n, int digit) { // If the digit divides the number // then return true else return false. return (digit != 0 && n % digit == 0); } // Function to check if all // digits of n divide it or not, static bool allDigitsDivide(int n) { int temp = n; while (temp > 0) { // Taking the digit of the // number into var 'digit'. int digit = temp % 10; if ((checkDivisibility(n, digit)) == false) return false; temp /= 10; } return true; } // Driver function public static void Main() { int n = 128; // function call to check // digits divisibility if (allDigitsDivide(n)) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } /*This code is contributed by vt_m.*/
PHP
<?php //PHP program to check the number // is divisible by all digits are not. // Function to check the divisibility // of the number by its digit. function checkDivisibility($n, $digit) { // If the digit divides the number // then return true else return false. return ($digit != 0 && $n % $digit == 0); } // Function to check if all digits // of n divide it or not function allDigitsDivide($n) { $temp = $n; while ($temp > 0) { // Taking the digit of the // number into digit var. $digit = $temp % 10; if (!(checkDivisibility($n, $digit))) return false; $temp /= 10; } return true; } // Driver function $n = 128; if (allDigitsDivide($n)) echo "Yes"; else echo "No"; // This code is contributed by ajit. ?>
Javascript
<script> // Javascript program to check the number // is divisible by all digits are not. // Function to check the divisibility // of the number by its digit. function checkDivisibility(n, digit) { // If the digit divides the number // then return true else return false. return (digit != 0 && n % digit == 0); } // Function to check if all digits // of n divide it or not function allDigitsDivide(n) { let temp = n; while (temp > 0) { // Taking the digit of the // number into digit var. let digit = temp % 10; if (!(checkDivisibility(n, digit))) return false; temp = parseInt(temp / 10, 10); } return true; } let n = 128; if (allDigitsDivide(n)) document.write("Yes"); else document.write("No"); // This code is contributed by divyeshrabadiya07. </script>
Producción:
Yes
Complejidad de tiempo: O(log 10 n), donde n representa el entero dado.
Espacio auxiliar: O(1), no se requiere espacio adicional, por lo que es una constante.
Implementación alternativa en Python
C++
// C++ program to // check the number is // divisible by all // digits are not. #include <bits/stdc++.h> using namespace std; // Function to check // the divisibility // of the number by // its digit. bool checkDivisibility(int n, int digit) { // If the digit divides the // number then return true // else return false. return (digit != 0 and n % digit == 0); } // Function to check if // all digits of n divide // it or not bool allDigitsDivide(int n) { // creating a set of integers // representing the digits of n set<int> nlist; // building the set for (char c : to_string(n)) nlist.insert(c - '0'); // checking if all the digits divide // n evenly for (int digit : nlist) { if (!checkDivisibility(n, digit)) return false; } return true; } // Driver function int main() { int n = 128; cout << (allDigitsDivide(n) ? "Yes" : "No"); } // This code is contributed by phasing17
Python3
# Python 3 program to # check the number is # divisible by all # digits are not. # Function to check # the divisibility # of the number by # its digit. def checkDivisibility(n, digit) : # If the digit divides the # number then return true # else return false. return (digit != 0 and n % digit == 0) # Function to check if # all digits of n divide # it or not def allDigitsDivide( n) : nlist = map(int, set(str(n))) for digit in nlist : if not (checkDivisibility(n, digit)) : return False return True # Driver function n = 128 print("Yes" if (allDigitsDivide(n)) else "No")
C#
// C# program to // check the number is // divisible by all // digits are not. using System; using System.Linq; using System.Collections.Generic; class GFG { // Function to check // the divisibility // of the number by // its digit. static bool checkDivisibility(int n, int digit) { // If the digit divides the // number then return true // else return false. return (digit != 0 && n % digit == 0); } // Function to check if // all digits of n divide // it or not static bool allDigitsDivide(int n) { HashSet<char> nlist = new HashSet<char>( Convert.ToString(n).ToCharArray()); foreach(var digit in nlist) { if (checkDivisibility(n, Convert.ToInt32(digit))) return false; } return true; } // Driver function public static void Main(string[] args) { int n = 128; if (allDigitsDivide(n)) Console.Write("Yes"); else Console.Write("No"); } } // The code is contributed by phasing17
Javascript
// JavaScript program to // check the number is // divisible by all // digits are not. // Function to check // the divisibility // of the number by // its digit. function checkDivisibility(n, digit){ // If the digit divides the // number then return true // else return false. return (digit != 0 && n % digit == 0); } // Function to check if // all digits of n divide // it or not function allDigitsDivide(n){ let nlist = new Set(n.toString()); nlist.forEach(digit => { if(checkDivisibility(n, digit)){ return false; } }); return true; } // Driver function let n = 128; console.log((allDigitsDivide(n)) ? "Yes" : "No"); // The code is contributed by Nidhi goel
Yes
Complejidad de tiempo: O(n), donde n representa el entero dado.
Espacio auxiliar: O(n), donde n representa el entero dado.
Publicación traducida automáticamente
Artículo escrito por Sagar Shukla y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA