Dado un número N, la tarea es verificar si para cada valor de i ( 0 <= i <= len ) , los primeros i dígitos de un número son divisibles por (len – i + 1) o no, donde len es el número de dígitos en N . Si se encuentra que es cierto, escriba «Sí» . De lo contrario, escriba “No”.
Ejemplos:
Entrada: N = 52248.
Salida: Sí
Explicación:
- 5 es divisible por 5
- 52 es divisible por 4
- 522 es divisible por 3
- 5224 es divisible por 2
- 52248 es divisible por 1
Entrada: N = 59268
Salida: No
Enfoque: La idea es recorrer todos los prefijos del número dado y para cada prefijo, comprobar si cumple la condición o no.
Siga los pasos a continuación para resolver el problema:
- Inicialice una variable, digamos i como 1, para mantener el valor de (len – i + 1 ).
- Iterar mientras n es mayor que 0.
- Si N no es divisible por i, devuelve falso.
- De lo contrario, divida N entre 10 y aumente el valor de i en 1 .
- Finalmente, devuelve verdadero.
A continuación se muestra la implementación del enfoque anterior.
C++
// C++ program for the above approach #include <iostream> using namespace std; // Function to check if all prefixes // of a number is divisible by // remaining count of digits or not bool prefixDivisble(int n) { int i = 1; while (n > 0) { // Traverse and check divisibility // for each updated number if (n % i != 0) return false; // Update the original number n = n / 10; i++; } return true; } // Driver Code int main() { // Given Input int n = 52248; // Function Call if (prefixDivisble(n)) cout << "Yes" << endl; else cout << "No" << endl; return 0; }
Java
// Java program for the above approach import java.io.*; class GFG{ // Function to check if all prefixes // of a number is divisible by // remaining count of digits or not public static boolean prefixDivisble(int n) { int i = 1; while (n > 0) { // Traverse and check divisibility // for each updated number if (n % i != 0) return false; // Update the original number n = n / 10; i++; } return true; } // Driver Code public static void main (String[] args) { // Given Input int n = 52248; // Function Call if (prefixDivisble(n)) System.out.println("Yes"); else System.out.println("No"); } } // This code is contributed by lokeshpotta20.
Python3
# Python3 program for the above approach # Function to check if all prefixes of # a number is divisible by remaining count # of digits or not def prefixDivisble(n): i = 1 while n > 0: # Traverse and check divisibility # for each updated number if n % i != 0: return False # Update the original number n = n // 10 i += 1 return True # Driver Code # Given Input n = 52248 # Function Call if (prefixDivisble(n)): print("Yes") else: print("No") # This code is contributed by abhivick07
C#
// C# program for the above approach using System; using System.Collections.Generic; class GFG{ // Function to check if all prefixes // of a number is divisible by // remaining count of digits or not static bool prefixDivisble(int n) { int i = 1; while (n > 0) { // Traverse and check divisibility // for each updated number if (n % i != 0) return false; // Update the original number n = n / 10; i++; } return true; } // Driver Code public static void Main() { // Given Input int n = 52248; // Function Call if (prefixDivisble(n)) Console.Write("Yes"); else Console.Write("No"); } } // This code is contributed by ipg2016107
Javascript
<script> // Javascript program for the above approach // Function to check if all prefixes // of a number is divisible by // remaining count of digits or not function prefixDivisble(n) { let i = 1; while (n > 0) { // Traverse and check divisibility // for each updated number if (n % i != 0) return false; // Update the original number n = parseInt(n / 10); i++; } return true; } // Driver Code // Given Input let n = 52248; // Function Call if (prefixDivisble(n) == true) document.write("Yes"); else document.write("No"); // This code is contributed by lokeshpotta20. </script>
Yes
Complejidad de tiempo: O(len) donde len es el número de dígitos en N.
Espacio auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por ShubhamSingh53 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA