Dados tres números enteros N , K y R . La tarea es calcular la suma de todos los números de 1 a N que produce el resto R al dividir por K.
Ejemplos:
Entrada: N = 20, K = 4, R = 3
Salida: 55
3, 7, 11, 15 y 19 son los únicos números que dan 3 como resto de la división con 4.
3 + 7 + 11 + 15 + 19 = 55
Entrada: N = 15, K = 13, R = 2
Salida: 17
Acercarse:
- Inicialice sum = 0 y tome el módulo de cada elemento de 1 a N con K .
- Si el resto es igual a R , actualice sum = sum + i donde i es el número actual que dio a R como el resto al dividir por K .
- Imprime el valor de sum 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 long long int count(int N, int K, int R) { long long int sum = 0; for (int i = 1; i <= N; i++) { // If current number gives R as the // remainder on dividing by K if (i % K == R) // Update the sum sum += i; } // Return the sum return sum; } // Driver code int main() { int N = 20, K = 4, R = 3; cout << count(N, K, R); return 0; }
Java
// Java implementation of the approach class GfG { // Function to return the sum static long count(int N, int K, int R) { long sum = 0; for (int i = 1; i <= N; i++) { // If current number gives R as the // remainder on dividing by K if (i % K == R) // Update the sum sum += i; } // Return the sum return sum; } // Driver code public static void main(String[] args) { int N = 20, K = 4, R = 3; System.out.println(count(N, K, R)); } } // This code is contributed by // prerna saini.
Python3
# Python 3 implementation of the approach # Function to return the sum def count(N, K, R): sum = 0 for i in range(1, N + 1): # If current number gives R as the # remainder on dividing by K if (i % K == R): # Update the sum sum += i # Return the sum return sum # Driver code if __name__ == '__main__': N = 20 K = 4 R = 3 print(count(N, K, R)) # This code is contributed by # Surendra_Gangwar
C#
// C# implementation of the approach using System; class GFG { // Function to return the sum static long count(int N, int K, int R) { long sum = 0; for (int i = 1; i <= N; i++) { // If current number gives R as the // remainder on dividing by K if (i % K == R) // Update the sum sum += i; } // Return the sum return sum; } // Driver code public static void Main() { int N = 20, K = 4, R = 3; Console.Write(count(N, K, R)); } } // This code is contributed by // Akanksha Rai
PHP
<?php // PHP implementation of the approach // Function to return the sum function count1($N, $K, $R) { $sum = 0; for ($i = 1; $i <= $N; $i++) { // If current number gives R as the // remainder on dividing by K if ($i % $K == $R) // Update the sum $sum += $i; } // Return the sum return $sum; } // Driver code $N = 20; $K = 4; $R = 3; echo count1($N, $K, $R); // This code is contributed // by Akanksha Rai ?>
Javascript
<script> // Javascript implementation of the approach // Function to return the sum function count(N , K , R) { var sum = 0; for (i = 1; i <= N; i++) { // If current number gives R as the // remainder on dividing by K if (i % K == R) // Update the sum sum += i; } // Return the sum return sum; } // Driver code var N = 20, K = 4, R = 3; document.write(count(N, K, R)); // This code contributed by aashish1995 </script>
Producción:
55
Complejidad de tiempo: O(N)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por Shashank_Sharma y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA