Dado un rango l – r (inclusive), cuente los números que son divisibles por todos sus dígitos distintos de cero.
Ejemplos:
Input : 1 9 Output : 9 Explanation: all the numbers are divisible by their digits in the range 1-9. Input : 10 20 Output : 5 Explanation: 10, 11, 12, 15, 20
Enfoque:
1. Ejecute un ciclo para generar cada número de l y r.
2. Compruebe si cada dígito distinto de cero de ese número divide el número o no.
3. Lleva la cuenta de todos los números que son completamente divisibles por sus dígitos.
4. Imprime el conteo de números.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to // Count numbers in // range L-R that are // divisible by // all of its non-zero // digits #include <bits/stdc++.h> using namespace std; // check if the number is // divisible by the digits. bool check(int n) { int m = n; while (n) { int r = n % 10; if (r > 0) if ((m % r) != 0) return false; n /= 10; } return true; } // function to calculate the // number of numbers int count(int l, int r) { int ans = 0; for (int i = l; i <= r; i++) if (check(i)) ans += 1; return ans; } // Driver function int main() { int l = 10, r = 20; cout << count(l, r); return 0; }
Java
// Java program to Count // numbers in range L-R // that are divisible by // all of its non-zero // digits import java.io.*; class GFG { // check if the number // is divisible by the // digits. static boolean check(int n) { int m = n; while (n != 0) { int r = n % 10; if (r > 0) if ((m % r) != 0) return false; n /= 10; } return true; } // function to calculate // the number of numbers static int count(int l, int r) { int ans = 0; for (int i = l; i <= r; i++) if (check(i)) ans += 1; return ans; } // Driver function public static void main(String args[]) { int l = 10, r = 20; System.out.println(count(10, 20)); } } // This code is contributed by Nikita Tiwari.
Python3
# Python 3 program # to Count numbers in # range L-R that are # divisible by all of # its non-zero digits # check if the number is # divisible by the digits. def check(n) : m = n while (n != 0) : r = n % 10 if (r > 0) : if ((m % r) != 0) : return False n = n // 10 return True # function to calculate the # number of numbers def count(l, r) : ans = 0 for i in range(l, r+1) : if (check(i)) : ans = ans + 1 return ans # Driver function l = 10 r = 20 print(count(l, r)) # This code is contributed by Nikita Tiwari.
C#
// Java program to Count // numbers in range L-R // that are divisible by // all of its non-zero // digits using System; class GFG { // check if the number // is divisible by the // digits. static bool check(int n) { int m = n; while (n != 0) { int r = n % 10; if (r > 0) if ((m % r) != 0) return false; n /= 10; } return true; } // function to calculate // the number of numbers static int count(int l, int r) { int ans = 0; for (int i = l; i <= r; i++) if (check(i)) ans += 1; return ans; } // Driver function public static void Main() { int l = 10, r = 20; Console.WriteLine(count(l, r)); } } // This code is contributed by Vt_m.
PHP
<?php // PHP program to Count numbers // in range L-R that are // divisible by all of its // non-zero digits // check if the number is // divisible by the digits. function check($n) { $m = $n; while ($n) { $r = $n % 10; if ($r > 0) if (($m % $r) != 0) return false; $n /= 10; } return true; } // function to calculate the // number of numbers function countIn($l, $r) { $ans = 0; for ($i = $l; $i <= $r; $i++) if (check($i)) $ans += 1; return $ans; } // Driver function $l = 10; $r = 20; echo countIn($l, $r); // This code is contributed ajit ?>
Javascript
<script> // Javascript program to Count numbers // in range L-R that are // divisible by all of its // non-zero digits // check if the number is // divisible by the digits. function check(n) { let m = n; while (n) { let r = n % 10; if (r > 0) if ((n % r) != 0) return false; n /= 10; } return true; } // function to calculate the // number of numbers function countIn(l, r) { let ans = 0; for (let i = l; i <= r; i++) if (check(i)) ans += 1; return ans; } // Driver function let l = 10; let r = 20; document.write(countIn(l, r)); // This code is contributed by sravan kumar </script>
Producción:
5
Complejidad de tiempo: O((rl) * log 10 r), donde r representa el límite superior del rango y l denota el límite inferior del rango dado
Espacio auxiliar: O(1), no se requiere espacio adicional, por lo que es una constante.