Gapful Number es un número N de al menos 3 dígitos tal que es divisible por la concatenación de su primer y último dígito.
Algunos números vacíos son:
100, 105, 108, 110, 120, 121, 130, 132, 135, 140,…
Comprobar si N es un número vacío
Dado un número entero N , la tarea es verificar si N es un número vacío o no. Si N es un número vacío, escriba «Sí» , de lo contrario, escriba «No» .
Ejemplos:
Entrada: N = 108
Salida: Sí
Explicación:
108 es divisible por 18
Entrada: N = 112
Salida: No
Enfoque: La idea es crear un número (digamos num ) usando el primer y último dígito de los números dados y comprobar si N es divisible por num o no. Si N es divisible por num , entonces es un número vacío e imprime «Sí» , de lo contrario, imprime «No» .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Find the first digit int firstDigit(int n) { // Find total number of digits - 1 int digits = (int)log10(n); // Find first digit n = (int)(n / pow(10, digits)); // Return first digit return n; } // Find the last digit int lastDigit(int n) { // return the last digit return (n % 10); } // A function to check Gapful numbers bool isGapful(int n) { int first_dig = firstDigit(n); int last_dig = lastDigit(n); int concatenation = first_dig * 10 + last_dig; // Return true if n is gapful number return (n % concatenation == 0); } // Driver Code int main() { // Given Number int n = 108; // Function Call if (isGapful(n)) cout << "Yes"; else cout << "No"; return 0; }
Java
// Java program for the above approach class GFG{ // Find the first digit static int firstDigit(int n) { // Find total number of digits - 1 int digits = (int)(Math.log(n) / Math.log(10)); // Find first digit n = (int)(n / Math.pow(10, digits)); // Return first digit return n; } // Find the last digit static int lastDigit(int n) { // Return the last digit return (n % 10); } // A function to check Gapful numbers static boolean isGapful(int n) { int first_dig = firstDigit(n); int last_dig = lastDigit(n); int concatenation = first_dig * 10 + last_dig; // Return true if n is gapful number return (n % concatenation == 0); } // Driver code public static void main(String[] args) { // Given number int n = 108; // Function call if (isGapful(n)) System.out.print("Yes"); else System.out.print("No"); } } // This code is contributed by Pratima Pandey
Python3
# Python3 program for the above approach import math # Find the first digit def firstDigit(n): # Find total number of digits - 1 digits = math.log10(n) # Find first digit n = (n / math.pow(10, digits)) # Return first digit return n # Find the last digit def lastDigit(n): # return the last digit return (n % 10) # A function to check Gapful numbers def isGapful(n): concatenation = (firstDigit(n) * 10) +\ lastDigit(n) # Return true if n is gapful number return (n % concatenation) # Driver Code if __name__=='__main__': # Given Number n = 108 # Function Call if (isGapful(n)): print("Yes") else: print("No") # This code is contributed by Ritik Bansal
C#
// C# program for the above approach using System; class GFG{ // Find the first digit static int firstDigit(int n) { // Find total number of digits - 1 int digits = (int)(Math.Log(n) / Math.Log(10)); // Find first digit n = (int)(n / Math.Pow(10, digits)); // Return first digit return n; } // Find the last digit static int lastDigit(int n) { // Return the last digit return (n % 10); } // A function to check Gapful numbers static bool isGapful(int n) { int first_dig = firstDigit(n); int last_dig = lastDigit(n); int concatenation = first_dig * 10 + last_dig; // Return true if n is gapful number return (n % concatenation == 0); } // Driver code public static void Main() { // Given number int n = 108; // Function call if (isGapful(n)) Console.Write("Yes"); else Console.Write("No"); } } // This code is contributed by Code_Mech
Javascript
<script> // Javascript program for the above approach // Find the first digit function firstDigit( n) { // Find total number of digits - 1 let digits = parseInt( (Math.log(n) / Math.log(10))); // Find first digit n = parseInt( (n / Math.pow(10, digits))); // Return first digit return n; } // Find the last digit function lastDigit( n) { // Return the last digit return (n % 10); } // A function to check Gapful numbers function isGapful( n) { let first_dig = firstDigit(n); let last_dig = lastDigit(n); let concatenation = first_dig * 10 + last_dig; // Return true if n is gapful number return (n % concatenation == 0); } // Driver code // Given number let n = 108; // Function call if (isGapful(n)) document.write("Yes"); else document.write("No"); // This code is contributed by aashish1995 </script>
Yes
Complejidad de tiempo: O(1)
Referencia: https://oeis.org/A108343