Dado un número, verifique si es divisible por 7. No está permitido usar el operador de módulo, la aritmética de punto flotante tampoco está permitida.
Un método simple es la resta repetida. El siguiente es otro método interesante.
La divisibilidad por 7 se puede comprobar mediante un método recursivo. Un número de la forma 10a + b es divisible por 7 si y solo si a – 2b es divisible por 7. En otras palabras, resta dos veces el último dígito del número formado por los dígitos restantes. Continúe haciendo esto hasta que un número pequeño.
Ejemplo: el número 371: 37 – (2×1) = 37 – 2 = 35; 3 – (2 × 5) = 3 – 10 = -7; por lo tanto, dado que -7 es divisible por 7, 371 es divisible por 7.
A continuación se muestra la implementación del método anterior
C++
// A Program to check whether a number is divisible by 7 #include <bits/stdc++.h> using namespace std; int isDivisibleBy7( int num ) { // If number is negative, make it positive if( num < 0 ) return isDivisibleBy7( -num ); // Base cases if( num == 0 || num == 7 ) return 1; if( num < 10 ) return 0; // Recur for ( num / 10 - 2 * num % 10 ) return isDivisibleBy7( num / 10 - 2 * ( num - num / 10 * 10 ) ); } // Driver code int main() { int num = 616; if( isDivisibleBy7(num ) ) cout << "Divisible" ; else cout << "Not Divisible" ; return 0; } // This code is contributed by rathbhupendra
C
// A Program to check whether a number is divisible by 7 #include <stdio.h> int isDivisibleBy7( int num ) { // If number is negative, make it positive if( num < 0 ) return isDivisibleBy7( -num ); // Base cases if( num == 0 || num == 7 ) return 1; if( num < 10 ) return 0; // Recur for ( num / 10 - 2 * num % 10 ) return isDivisibleBy7( num / 10 - 2 * ( num - num / 10 * 10 ) ); } // Driver program to test above function int main() { int num = 616; if( isDivisibleBy7(num ) ) printf( "Divisible" ); else printf( "Not Divisible" ); return 0; }
Java
// Java program to check whether a number is divisible by 7 import java.io.*; class GFG { // Function to check whether a number is divisible by 7 static boolean isDivisibleBy7(int num) { // If number is negative, make it positive if( num < 0 ) return isDivisibleBy7( -num ); // Base cases if( num == 0 || num == 7 ) return true; if( num < 10 ) return false; // Recur for ( num / 10 - 2 * num % 10 ) return isDivisibleBy7( num / 10 - 2 * ( num - num / 10 * 10 ) ); } // Driver program public static void main (String[] args) { int num = 616; if(isDivisibleBy7(num)) System.out.println("Divisible"); else System.out.println("Not Divisible"); } } // Contributed by Pramod Kumar
Python3
# Python program to check whether a number is divisible by 7 # Function to check whether a number is divisible by 7 def isDivisibleBy7(num) : # If number is negative, make it positive if num < 0 : return isDivisibleBy7( -num ) # Base cases if( num == 0 or num == 7 ) : return True if( num < 10 ) : return False # Recur for ( num / 10 - 2 * num % 10 ) return isDivisibleBy7( num // 10 - 2 * ( num - num // 10 * 10 ) ) # Driver program num = 616 if(isDivisibleBy7(num)) : print ("Divisible") else : print ("Not Divisible") # This code is contributed by Nikita Tiwari
C#
// C# program to check whether a // number is divisible by 7 using System; class GFG { // Function to check whether a // number is divisible by 7 static bool isDivisibleBy7(int num) { // If number is negative, // make it positive if( num < 0 ) return isDivisibleBy7(-num); // Base cases if( num == 0 || num == 7 ) return true; if( num < 10 ) return false; // Recur for ( num / 10 - 2 * num % 10 ) return isDivisibleBy7(num / 10 - 2 * ( num - num / 10 * 10 )); } // Driver Code public static void Main () { int num = 616; if(isDivisibleBy7(num)) Console.Write("Divisible"); else Console.Write("Not Divisible"); } } // This code is contributed by Nitin Mittal.
PHP
<?php // PHP Program to check whether // a number is divisible by 7 // Function to check whether a // number is divisible by 7 function isDivisibleBy7( $num ) { // If number is negative, // make it positive if( $num < 0 ) return isDivisibleBy7( -$num ); // Base cases if( $num == 0 || $num == 7 ) return 1; if( $num < 10 ) return 0; // Recur for ( num / 10 - 2 * num % 10 ) return isDivisibleBy7($num / 10 - 2 * ($num - $num / 10 * 10 ) ); } // Driver Code $num = 616; if( isDivisibleBy7($num )>=0 ) echo("Divisible"); else echo("Not Divisible"); // This code is contributed by vt_m. ?>
Javascript
<script> // js Program to check whether // a number is divisible by 7 // Function to check whether a // number is divisible by 7 function isDivisibleBy7( num ) { // If number is negative, // make it positive if( num < 0 ) return isDivisibleBy7( -num ); // Base cases if( num == 0 || num == 7 ) return 1; if( num < 10 ) return 0; // Recur for ( num / 10 - 2 * num % 10 ) return isDivisibleBy7(num / 10 - 2 * (num - num / 10 * 10 ) ); } // Driver Code let num = 616; if( isDivisibleBy7(num )>=0 ) document.write("Divisible"); else document.write("Not Divisible"); // This code is contributed by sravan kumar </script>
Divisible
Espacio Auxiliar: O(1)
¿Como funciona esto? Sea ‘b’ el último dígito de un número ‘n’ y sea ‘a’ el número que obtenemos cuando separamos ‘b’.
La representación del número también se puede multiplicar por cualquier número primo relativo al divisor sin cambiar su divisibilidad. Luego de observar que 7 divide a 21, podemos realizar lo siguiente:
10.a + b
después de multiplicar por 2, esto se convierte en
20.a + 2.b
y entonces
21.a - a + 2.b
Eliminando el múltiplo de 21 da
-a + 2b
y multiplicando por -1 da
a - 2b
Método: Para comprobar que el número dado es divisible por 7 o no, se utiliza el operador de división de módulo «%».
Python3
# Python code # To check whether the given number is divisible by 7 or not #input n=371 # the above input can also be given as n=input() -> taking input from user # finding given number is divisible by 7 or not if int(n)%7==0: print("divisible") else: print("Not divisible") # this code is contributed by gangarajula laxmi
Javascript
<script> // JavaScript code for the above approach // To check whether the given number is divisible by 7 or not //input let n = 371 // finding given number is divisible by 7 or not if (n % 7 == 0) document.write("divisible") else document.write("Not divisible") // This code is contributed by Potta Lokesh </script>
PHP
<?php //input $n=371; // finding given number is divisible by 7 or not // checking the given number is divisible by 7 or not if ($n % 7 == 0) { echo "Divisible"; } else { echo "Not divisible"; } // this code is contributed by gangarajula laxmi ?>
divisible
Método: verificar que el número dado es divisible por 7 o no usar la división de módulo.
C++
// C++ program to check if given number is divisible by 7 or // not using modulo division #include <iostream> using namespace std; int main() { // input number int num = 371; // checking if the given number is divisible by 7 or not // using modulo division operator if the output of num%7 // is equal to 0 then given number is divisible by 7 // otherwise not divisible by 7 if (num % 7 == 0) { cout << " divisible"; } else { cout << " not divisible"; } return 0; } // this code is contributed by gangarajula laxmi
Java
// java program to check if given number is divisible by 7 or // not using modulo division import java.io.*; class GFG { public static void main (String[] args) { // input number int num=371; // checking if the given number is divisible by 7 or not // using modulo division operator if the output of num%7 // is equal to 0 then given number is divisible by 7 // otherwise not divisible by 7 if (num % 7 == 0) { System.out.println(" divisible"); } else { System.out.println(" not divisible"); } } } // this code is contributed by gangarajula laxmi
C#
// C# program to check if given number is divisible by 7 or // not using modulo division using System; class GFG { public static void Main(string[] args) { // input number int num = 371; // checking if the given number is divisible by 7 or // not // using modulo division operator if the output of // num%7 is equal to 0 then given number is // divisible by 7 otherwise not divisible by 7 if (num % 7 == 0) { Console.WriteLine(" divisible"); } else { Console.WriteLine(" not divisible"); } } } // This code is contributed by phasing17
Hay otros métodos interesantes para comprobar la divisibilidad por 7 y otros números. Vea la siguiente página Wiki para más detalles.
Referencias:
http://en.wikipedia.org/wiki/Divisibility_rule
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