Dado un número, la tarea es verificar si un número es divisible por 6 o no. El número de entrada puede ser grande y puede que no sea posible almacenarlo incluso si usamos long long int.
Ejemplos:
Input : n = 2112 Output: Yes Input : n = 1124 Output : No Input : n = 363588395960667043875487 Output : No
Python3
# Python code # To check whether the given number is divisible by 6 or not #input n=363588395960667043875487 # the above input can also be given as n=input() -> taking input from user # finding given number is divisible by 6 or not if int(n)%6==0: print("Yes") else: print("No")
Javascript
<script> // JavaScript code for the above approach // To check whether the given number is divisible by 6 or not //input var n = 363588395960667043875487 // finding given number is divisible by 6 or not if (n % 6 == 0) document.write("Yes") else document.write("No") // This code is contributed by Potta Lokesh </script>
No
Dado que el número de entrada puede ser muy grande, no podemos usar n % 6 para verificar si un número es divisible por 6 o no, especialmente en lenguajes como C/C++. La idea se basa en el siguiente hecho.
A number is divisible by 6 it's divisible by 2 and 3. a) A number is divisible by 2 if its last digit is divisible by 2. b) A number is divisible by 3 if sum of digits is divisible by 3.
A continuación se muestra la implementación basada en los pasos anteriores.
C++
// C++ program to find if a number is divisible by // 6 or not #include<bits/stdc++.h> using namespace std; // Function to find that number divisible by 6 or not bool check(string str) { int n = str.length(); // Return false if number is not divisible by 2. if ((str[n-1]-'0')%2 != 0) return false; // If we reach here, number is divisible by 2. // Now check for 3. // Compute sum of digits int digitSum = 0; for (int i=0; i<n; i++) digitSum += (str[i]-'0'); // Check if sum of digits is divisible by 3 return (digitSum % 3 == 0); } // Driver code int main() { string str = "1332"; check(str)? cout << "Yes" : cout << "No "; return 0; }
Java
// Java program to find if a number is // divisible by 6 or not class IsDivisible { // Function to find that number divisible by 6 or not static boolean check(String str) { int n = str.length(); // Return false if number is not divisible by 2. if ((str.charAt(n-1) -'0')%2 != 0) return false; // If we reach here, number is divisible by 2. // Now check for 3. // Compute sum of digits int digitSum = 0; for (int i=0; i<n; i++) digitSum += (str.charAt(i)-'0'); // Check if sum of digits is divisible by 3 return (digitSum % 3 == 0); } // main function public static void main (String[] args) { String str = "1332"; if(check(str)) System.out.println("Yes"); else System.out.println("No"); } }
Python3
# Python 3 program to find # if a number is divisible # by 6 or not # Function to find that number # is divisible by 6 or not def check(st) : n = len(st) # Return false if number # is not divisible by 2. if (((int)(st[n-1])%2) != 0) : return False # If we reach here, number # is divisible by 2. Now # check for 3. # Compute sum of digits digitSum = 0 for i in range(0, n) : digitSum = digitSum + (int)(st[i]) # Check if sum of digits # is divisible by 3 return (digitSum % 3 == 0) # Driver code st = "1332" if(check(st)) : print("Yes") else : print("No ") # This article is contributed by Nikita Tiwari.
C#
// C# program to find if a number is // divisible by 6 or not using System; class GFG { // Function to find that number // divisible by 6 or not static bool check(String str) { int n = str.Length; // Return false if number is // not divisible by 2. if ((str[n-1] -'0') % 2 != 0) return false; // If we reach here, number is // divisible by 2. // Now check for 3. // Compute sum of digits int digitSum = 0; for (int i = 0; i < n; i++) digitSum += (str[i] - '0'); // Check if sum of digits is // divisible by 3 return (digitSum % 3 == 0); } // main function public static void Main () { String str = "1332"; if(check(str)) Console.Write("Yes"); else Console.Write("No"); } } // This code is contributed by parashar.
PHP
<?php // PHP program to find if a // number is divisible by // 6 or not // Function to find that number // divisible by 6 or not function check($str) { $n = strlen($str); // Return false if number is // not divisible by 2. if (($str[$n - 1] - '0') % 2 != 0) return false; // If we reach here, number // is divisible by 2. // Now check for 3. // Compute sum of digits $digitSum = 0; for ($i = 0; $i < $n; $i++) $digitSum += ($str[$i] - '0'); // Check if sum of digits // is divisible by 3 return ($digitSum % 3 == 0); } // Driver code $str = "1332"; if(check($str)) echo "Yes" ; else echo " No "; return 0; // This code is contributed by nitin mittal. ?>
Javascript
<script> // JavaScript program for the above approach // Function to find that number // divisible by 6 or not function check(str) { let n = str.length; // Return false if number is // not divisible by 2. if ((str[n-1] -'0') % 2 != 0) return false; // If we reach here, number is // divisible by 2. // Now check for 3. // Compute sum of digits let digitSum = 0; for (let i = 0; i < n; i++) digitSum += (str[i] - '0'); // Check if sum of digits is // divisible by 3 return (digitSum % 3 == 0); } // Driver Code let str = "1332"; if(check(str)) document.write("Yes"); else document.write("No"); // This code is contributed by splevel62. </script>
Yes
Complejidad de tiempo: O(logN) donde N es el número dado
Espacio auxiliar: O(1) ya que no se utiliza espacio adicional
Este artículo es una contribución de DANISH_RAZA . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA