Dado un número grande, la tarea es verificar si el número es divisible por 13 o no.
Ejemplos:
Input : 637 Output : 637 is divisible by 13. Input : 920 Output : 920 is not divisible by 13. Input : 83959092724 Output : 83959092724 is divisible by 13.
Si el número num dado es pequeño, podemos encontrar fácilmente si es divisible por 13 o no haciendo num % 13 y verificando si el resultado es 0 o no. Pero ¿qué pasa con los números muy grandes. Hablemos de grandes números.
A continuación se presentan algunos datos interesantes sobre la divisibilidad de 13.
- Un número es divisible por 13 si y si la suma alterna (alternativamente sumando y restando) de bloques de tres de derecha a izquierda es divisible por 13. Por ejemplo, 2911285 es divisible por 13 porque la suma alterna de bloques de tamaño 3 es 2 – 911 + 285 = -650 que es divisible por 13.
- Un número es divisible por 13 si y solo si el número obtenido al sumar el último dígito multiplicado por 4 al resto también es divisible por 13.
Por ejemplo, considere 2353. Aplicando la regla anterior, obtenemos 235 + 3*4 = 247. Nuevamente aplicamos regla y obtenga, 24 + 7*4 = 52. Dado que 52 es divisible por 13, el número dado es divisible por 13.
A continuación, se basa en la implementación un primer hecho anterior (Encontrar una suma alterna de bloques de tamaño 3)
C++
// CPP program to check // whether a number is // divisible by 13 or not. #include <iostream> using namespace std; // Returns true if number // is divisible by 13 else // returns false bool checkDivisibility(string num) { int length = num.size(); if (length == 1 && num[0] == '0') return true; // Append required 0s . // at the beginning. if (length % 3 == 1) { // Same as strcat(num, "00"); // in c. num +="00"; length += 2; } else if (length % 3 == 2) { // Same as strcat(num, "0"); // in c. num += "0"; length += 1; } // Alternatively add/subtract // digits in group of three // to result. int sum = 0, p = 1; for (int i = length - 1; i >= 0; i--) { // Store group of three // numbers in group variable. int group = 0; group += num[i--] - '0'; group += (num[i--] - '0') * 10; group += (num[i] - '0') * 100; sum = sum + group * p; // Generate alternate series // of plus and minus p *= (-1); } sum = abs(sum); return (sum % 13 == 0); } // Driver code int main() { string number = "83959092724"; if (checkDivisibility(number)) cout << number << " is divisible by 13."; else cout << number << " is not divisible by 13."; return 0; }
Java
// Java program to check // whether a number is // divisible by 13 or not. class GFG { // Returns true if number // is divisible by 13 else // returns false static boolean checkDivisibility(String num) { int length = num.length(); if (length == 1 && num.charAt(0) == '0') return true; // Append required 0s . // at the beginning. if (length % 3 == 1) { // Same as strcat(num, "00"); // in c. num +="00"; length += 2; } else if (length % 3 == 2) { // Same as strcat(num, "0"); // in c. num += "0"; length += 1; } // Alternatively add/subtract // digits in group of three // to result. int sum = 0, p = 1; for (int i = length - 1; i >= 0; i--) { // Store group of three // numbers in group variable. int group = 0; group += num.charAt(i--) - '0'; group += (num.charAt(i--) - '0') * 10; group += (num.charAt(i) - '0') * 100; sum = sum + group * p; // Generate alternate series // of plus and minus p *= (-1); } sum = Math.abs(sum); return (sum % 13 == 0); } // Driver code public static void main(String[] args) { String number = "83959092724"; if (checkDivisibility(number)) System.out.println(number + " is divisible by 13."); else System.out.println(number + " is not divisible by 13."); } } // This code is contributed by mits
Python3
C#
// C# program to check // whether a number is // divisible by 13 or not. using System; class GFG { // Returns true if number // is divisible by 13 else // returns false static bool checkDivisibility(string num) { int length = num.Length; if (length == 1 && num[0] == '0') return true; // Append required 0s . // at the beginning. if (length % 3 == 1) { // Same as strcat(num, "00"); // in c. num +="00"; length += 2; } else if (length % 3 == 2) { // Same as strcat(num, "0"); // in c. num += "0"; length += 1; } // Alternatively add/subtract // digits in group of three // to result. int sum = 0, p = 1; for (int i = length - 1; i >= 0; i--) { // Store group of three // numbers in group variable. int group = 0; group += num[i--] - '0'; group += (num[i--] - '0') * 10; group += (num[i] - '0') * 100; sum = sum + group * p; // Generate alternate series // of plus and minus p *= (-1); } sum = Math.Abs(sum); return (sum % 13 == 0); } // Driver code static void Main() { string number = "83959092724"; if (checkDivisibility(number)) Console.Write( number + " is divisible by 13."); else Console.Write( number + " is not divisible by 13."); } } // This code is contributed by Sam007
PHP
<?php // PHP program to check // whether a number is // divisible by 13 or not. // Returns true if number // is divisible by 13 else // returns false function checkDivisibility($num) { $length = strlen($num); if ($length == 1 && $num[0] == '0') return true; // Append required 0s // at the beginning. if ($length % 3 == 1) { // Same as strcat(num, "00"); // in c. $num += "00"; $length += 2; } else if ($length % 3 == 2) { // Same as strcat(num, "0"); // in c. $num += "0"; $length += 1; } // Alternatively add/subtract // digits in group of three // to result. $sum = 0; $p = 1; for ($i = $length - 1; $i >= 0; $i--) { // Store group of three // numbers in group variable. $group = 0; $group += $num[$i--] - '0'; $group += ($num[$i--] - '0') * 10; $group += ($num[$i] - '0') * 100; $sum = $sum + $group * $p; // Generate alternate series // of plus and minus $p *= (-1); } $sum = abs($sum); return ($sum % 13 == 0); } // Driver code $number = "83959092724"; if (checkDivisibility($number)) echo($number . " is divisible by 13."); else echo($number . " is not divisible by 13."); // This code is contributed by Ajit. ?>
Javascript
<script> // Javascript program to check // whether a number is // divisible by 13 or not. // Returns true if number // is divisible by 13 else // returns false function checkDivisibility(num) { let length = num.length; if (length == 1 && num[0] == '0') return true; // Append required 0s // at the beginning. if (length % 3 == 1) { // Same as strcat(num, "00"); // in c. num += "00"; length += 2; } else if (length % 3 == 2) { // Same as strcat(num, "0"); // in c. num += "0"; length += 1; } // Alternatively add/subtract // digits in group of three // to result. let sum = 0; p = 1; for (let i = length - 1; i >= 0; i--) { // Store group of three // numbers in group variable. group = 0; group += num[i--] - '0'; group += (num[i--] - '0') * 10; group += (num[i] - '0') * 100; sum = sum + group * p; // Generate alternate series // of plus and minus p *= (-1); } sum = Math.abs(sum); return (sum % 13 == 0); } // Driver code let number = "83959092724"; if (checkDivisibility(number)) document.write(number + " is divisible by 13."); else document.write(number + " is not divisible by 13."); // This code is contributed by _saurabh_jaiswal. </script>
83959092724 is divisible by 13.
Método: Comprobación de que el número dado es divisible por 13 o no utilizando el operador de división de módulo «%».
Python3
# Python code # To check whether the given number is divisible by 13 or not #input n=83959092724 # the above input can also be given as n=input() -> taking input from user # finding given number is divisible by 13 or not if int(n)%13==0: print("Yes") else: print("No") # this code is contributed by gangarajula laxmi
Yes
Publicación traducida automáticamente
Artículo escrito por pawan_asipu y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA