Dados tres números enteros positivos N , K y M , la tarea es encontrar el número que se sumará a N para obtener la suma de los primeros M múltiplos de K .
Ejemplos:
Entrada: N = 17, K = 3, M = 4
Salida: 13
Explicación:
Suma de los primeros 4 múltiplos de 3 = (3 + 6 + 9 + 12) = 30.
Por lo tanto, el valor que se suma a 17 es (30 – 17) = 13.
Por lo tanto, la salida requerida es 13.Entrada: N = 5, K = 2, M = 1
Salida: -3
Explicación:
La suma de los primeros 1 múltiplos de 2 es 2.
El valor que se debe sumar a 5 para obtener 2 es (2 – 5) = -3
Enfoque: siga los pasos a continuación para resolver el problema:
- Calcula la suma de los primeros M múltiplos de K , que será igual a K * (1 + 2 + 3 + … M) = K * M * (M + 1) / 2 .
- Inicialice una variable, digamos res , para almacenar el número que se requiere agregar a N para obtener sum .
- Por tanto, res será igual a sum – N . Imprime el valor de res .
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 print the value // to be added to N to obtain // sum of first M multiples of K static int printNumber(int N, int K, int M) { // Store the sum of the // first M multiples of K int sum = K * (M * (M + 1) / 2); // Store the value to be // added to obtain N return sum - N; } // Driver Code int main() { // Input int N = 17; int K = 3; int M = 4; cout << printNumber(N, K, M); return 0; } // This code is contributed by shubhamsingh10
Java
// Java code of above approach import java.util.*; class GFG { // Function to print the value // to be added to N to obtain // sum of first M multiples of K static int printNumber(int N, int K, int M) { // Store the sum of the // first M multiples of K int sum = K * (M * (M + 1) / 2); // Store the value to be // added to obtain N return sum - N; } // Driver code public static void main(String[] args) { // Input int N = 17; int K = 3; int M = 4; System.out.print(printNumber(N, K, M)); } } // This code is contributed by offbeat
Python3
# Python3 program for the above approach # Function to print the value # to be added to N to obtain # sum of first M multiples of K def printNumber(N, K, M): # Store the sum of the # first M multiples of K sum = K * (M * (M + 1) / 2) # Store the value to be # added to obtain N return sum - N # Driver Code # Input N = 17 K = 3 M = 4 print(int(printNumber(N, K, M)))
C#
// C# program for the above approach using System; class GFG { // Function to print the value // to be added to N to obtain // sum of first M multiples of K static int printNumber(int N, int K, int M) { // Store the sum of the // first M multiples of K int sum = K * (M * (M + 1) / 2); // Store the value to be // added to obtain N return sum - N; } // Driver code public static void Main(String[] args) { // Input int N = 17; int K = 3; int M = 4; Console.Write(printNumber(N, K, M)); } } // This code is contributed by shubhamsingh10
Javascript
<script> // JavaScript program for the above approach // Function to print the value // to be added to N to obtain // sum of first M multiples of K function printNumber(N, K, M) { // Store the sum of the // first M multiples of K var sum = K * ((M * (M + 1)) / 2); // Store the value to be // added to obtain N return sum - N; } // Driver Code // Input var N = 17; var K = 3; var M = 4; document.write(printNumber(N, K, M)); // This code is contributed by rdtank </script>
13
Tiempo Complejidad: O(1)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por dharanendralv23 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA