Dados dos enteros L y R . La tarea es encontrar el conteo de todos los números pares en el rango [L, R] cuya suma de dígitos es divisible por 3.
Ejemplos:
Entrada: L = 18, R = 36
Salida: 4
18, 24, 30, 36 son los únicos números en el rango [18, 36] que son pares y cuya suma de dígitos es divisible por 3.
Entrada: L = 7, R = 11
Salida: 0
No hay ningún número en el rango [7, 11] que sea par y cuya suma de dígitos sea divisible por 3.
Enfoque ingenuo: inicialice el conteo = 0 y para cada número en el rango [L, R] , verifique si el número es divisible por 2 y la suma de sus dígitos es divisible por 3. En caso afirmativo , incremente el conteo. Imprime el conteo al final.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the // sum of digits of x int sumOfDigits(int x) { int sum = 0; while (x != 0) { sum += x % 10; x = x / 10; } return sum; } // Function to return the count // of required numbers int countNumbers(int l, int r) { int count = 0; for (int i = l; i <= r; i++) { // If i is divisible by 2 and // sum of digits of i is divisible by 3 if (i % 2 == 0 && sumOfDigits(i) % 3 == 0) count++; } // Return the required count return count; } // Driver code int main() { int l = 1000, r = 6000; cout << countNumbers(l, r); return 0; }
Java
// Java implementation of the approach class GFG { // Function to return the // sum of digits of x static int sumOfDigits(int x) { int sum = 0; while (x != 0) { sum += x % 10; x = x / 10; } return sum; } // Function to return the count // of required numbers static int countNumbers(int l, int r) { int count = 0; for (int i = l; i <= r; i++) { // If i is divisible by 2 and // sum of digits of i is divisible by 3 if (i % 2 == 0 && sumOfDigits(i) % 3 == 0) count++; } // Return the required count return count; } // Driver code public static void main(String args[]) { int l = 1000, r = 6000; System.out.println(countNumbers(l, r)); } } // This code is contributed by Arnab Kundu
Python3
# python implementation of the approach # Function to return the # sum of digits of x def sumOfDigits(x): sum = 0 while x != 0: sum += x % 10 x = x//10 return sum # Function to return the count # of required numbers def countNumbers(l, r): count = 0 for i in range(l, r + 1): # If i is divisible by 2 and # sum of digits of i is divisible by 3 if i % 2 == 0 and sumOfDigits(i) % 3 == 0: count += 1 return count # Driver code l = 1000; r = 6000 print(countNumbers(l, r)) # This code is contributed by Shrikant13
C#
// C# implementation of the approach using System; class GFG { // Function to return the // sum of digits of x static int sumOfDigits(int x) { int sum = 0; while (x != 0) { sum += x % 10; x = x / 10; } return sum; } // Function to return the count // of required numbers static int countNumbers(int l, int r) { int count = 0; for (int i = l; i <= r; i++) { // If i is divisible by 2 and // sum of digits of i is divisible by 3 if (i % 2 == 0 && sumOfDigits(i) % 3 == 0) count++; } // Return the required count return count; } // Driver code public static void Main() { int l = 1000, r = 6000; Console.WriteLine(countNumbers(l, r)); } } // This code is contributed by Code_Mech.
PHP
<?php // PHP implementation of the approach // Function to return the sum of // digits of x function sumOfDigits( $x) { $sum = 0; while ($x != 0) { $sum += $x % 10; $x = $x / 10; } return $sum; } // Function to return the count // of required numbers function countNumbers($l, $r) { $count = 0; for ($i = $l; $i <= $r; $i++) { // If i is divisible by 2 and // sum of digits of i is divisible by 3 if ($i % 2 == 0 && sumOfDigits($i) % 3 == 0) $count++; } // Return the required count return $count; } // Driver code $l = 1000; $r = 6000; echo countNumbers($l, $r); // This code is contributed by princiraj1992 ?>
Javascript
<script> // JavaScript implementation of the approach // Function to return the // sum of digits of x function sumOfDigits(x) { let sum = 0; while (x != 0) { sum += x % 10; x = Math.floor(x / 10); } return sum; } // Function to return the count // of required numbers function countNumbers(l, r) { let count = 0; for (let i = l; i <= r; i++) { // If i is divisible by 2 and // sum of digits of i is divisible by 3 if (i % 2 == 0 && sumOfDigits(i) % 3 === 0) count++; } // Return the required count return count; } // Driver code let l = 1000, r = 6000; document.write(countNumbers(l, r)); // This code is contributed by Manoj. </script>
834
Complejidad de tiempo: O (r – l), ya que estamos atravesando de l a r.
Espacio auxiliar: O(1), ya que no estamos utilizando ningún espacio adicional.
Enfoque eficiente:
- Tenemos que comprobar que el número es divisible por 2.
- Tenemos que verificar que la suma de los dígitos sea divisible por 3, lo que significa que el número es divisible por 3.
Entonces, en general, tenemos que verificar si un número es divisible por 2 y 3, y dado que tanto 2 como 3 son coprimos, solo tenemos que verificar si un número es divisible por su producto, es decir, 6.
A continuación se muestra la implementación de lo anterior. Acercarse:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the count // of required numbers int countNumbers(int l, int r) { // Count of numbers in range // which are divisible by 6 return ((r / 6) - (l - 1) / 6); } // Driver code int main() { int l = 1000, r = 6000; cout << countNumbers(l, r); return 0; }
Java
// Java implementation of the approach class GFG { // Function to return the count // of required numbers static int countNumbers(int l, int r) { // Count of numbers in range // which are divisible by 6 return ((r / 6) - (l - 1) / 6); } // Driver code public static void main(String[] args) { int l = 1000, r = 6000; System.out.println(countNumbers(l, r)); } } // This code is contributed by princiraj1992
Python3
# Python3 implementation of the approach # Function to return the count # of required numbers def countNumbers(l, r) : # Count of numbers in range # which are divisible by 6 return ((r // 6) - (l - 1) // 6); # Driver code if __name__ == "__main__" : l = 1000; r = 6000; print(countNumbers(l, r)); # This code is contributed by Ryuga
C#
// C# implementation of the above approach using System; class GFG { // Function to return the count // of required numbers static int countNumbers(int l, int r) { // Count of numbers in range // which are divisible by 6 return ((r / 6) - (l - 1) / 6); } // Driver code public static void Main(String[] args) { int l = 1000, r = 6000; Console.WriteLine(countNumbers(l, r)); } } // This code contributed by Rajput-Ji
PHP
<?php // PHP implementation of the approach // Function to return the count // of required numbers function countNumbers($l, $r) { // Count of numbers in range // which are divisible by 6 return ((int)($r / 6) - (int)(($l - 1) / 6)); } // Driver code $l = 1000; $r = 6000; echo(countNumbers($l, $r)); // This code is contributed // by Code_Mech. ?>
Javascript
<script> // Javascript implementation of the approach // Function to return the count // of required numbers function countNumbers(l, r) { // Count of numbers in range // which are divisible by 6 return (parseInt(r / 6) - parseInt((l - 1) / 6)); } // Driver code var l = 1000, r = 6000; document.write(countNumbers(l, r)); </script>
834
Complejidad de tiempo: O (1), ya que no estamos atravesando ni usando ningún bucle.
Espacio auxiliar: O(1), ya que no estamos utilizando ningún espacio adicional.