Dado un número N. y la tarea es encontrar la suma de todos los múltiplos de 2 y 5 por debajo de N (N puede ser hasta 10^10).
Ejemplos :
Input : N = 10 Output : 25 Explanation : 2 + 4 + 6 + 8 + 5
Input : N = 20 Output : 110
Acercarse :
Sabemos que múltiplos de 2 forman un AP como:
2, 4, 6, 8, 10, 12, 14 ….(1)
De manera similar, los múltiplos de 5 forman un AP como:
5, 10, 15 ……(2)
Ahora, Suma(1) + Suma(2) = 2, 4, 5, 6, 8, 10, 10, 12, 14, 15….
Aquí, 10 se repite. De hecho, todos los múltiplos de 10 o 2*5 se repiten porque se cuenta dos veces, una vez en la serie de 2 y otra vez en la serie de 5. Por lo tanto, restaremos la suma de la serie de 10 de Sum( 1) + Suma(2).
La fórmula para la suma de un AP es:
n * ( a + l ) / 2
Donde es el número de términos, es el término inicial y es el último término.
Entonces, la respuesta final es:
S 2 + S 5 – S 10
A continuación se muestra la implementación del enfoque anterior:
C++
// CPP program to find the sum of all // multiples of 2 and 5 below N #include <bits/stdc++.h> using namespace std; // Function to find sum of AP series long long sumAP(long long n, long long d) { // Number of terms n /= d; return (n) * (1 + n) * d / 2; } // Function to find the sum of all // multiples of 2 and 5 below N long long sumMultiples(long long n) { // Since, we need the sum of // multiples less than N n--; return sumAP(n, 2) + sumAP(n, 5) - sumAP(n, 10); } // Driver code int main() { long long n = 20; cout << sumMultiples(n); return 0; }
C
// C program to find the sum of all // multiples of 2 and 5 below N #include <stdio.h> // Function to find sum of AP series long long sumAP(long long n, long long d) { // Number of terms n /= d; return (n) * (1 + n) * d / 2; } // Function to find the sum of all // multiples of 2 and 5 below N long long sumMultiples(long long n) { // Since, we need the sum of // multiples less than N n--; return sumAP(n, 2) + sumAP(n, 5) - sumAP(n, 10); } // Driver code int main() { long long n = 20; printf("%lld",sumMultiples(n)); return 0; } // This code is contributed by kothavvsaakash.
Java
// Java program to find the sum of all // multiples of 2 and 5 below N class GFG{ // Function to find sum of AP series static long sumAP(long n, long d) { // Number of terms n /= d; return (n) * (1 + n) * d / 2; } // Function to find the sum of all // multiples of 2 and 5 below N static long sumMultiples(long n) { // Since, we need the sum of // multiples less than N n--; return sumAP(n, 2) + sumAP(n, 5) - sumAP(n, 10); } // Driver code public static void main(String[] args) { long n = 20; System.out.println(sumMultiples(n)); } } // This code is contributed by mits
Python3
# Python3 program to find the sum of # all multiples of 2 and 5 below N # Function to find sum of AP series def sumAP(n, d): # Number of terms n = int(n / d); return (n) * (1 + n) * (d / 2); # Function to find the sum of all # multiples of 2 and 5 below N def sumMultiples(n): # Since, we need the sum of # multiples less than N n -= 1; return (int(sumAP(n, 2) + sumAP(n, 5) - sumAP(n, 10))); # Driver code n = 20; print(sumMultiples(n)); # This code is contributed by mits
C#
// C# program to find the sum of all // multiples of 2 and 5 below N using System; public class GFG{ // Function to find sum of AP series static long sumAP(long n, long d) { // Number of terms n /= d; return (n) * (1 + n) * d / 2; } // Function to find the sum of all // multiples of 2 and 5 below N static long sumMultiples(long n) { // Since, we need the sum of // multiples less than N n--; return sumAP(n, 2) + sumAP(n, 5) - sumAP(n, 10); } // Driver code static public void Main (){ long n = 20; Console.WriteLine(sumMultiples(n)); } }
PHP
<?php // PHP program to find the sum of all // multiples of 2 and 5 below N // Function to find sum of AP series function sumAP($n, $d) { // Number of terms $n = (int)($n /$d); return ($n) * ((1 + $n) * (int)$d / 2); } // Function to find the sum of all // multiples of 2 and 5 below N function sumMultiples($n) { // Since, we need the sum of // multiples less than N $n--; return sumAP($n, 2) + sumAP($n, 5) - sumAP($n, 10); } // Driver code $n = 20; echo sumMultiples($n); // This code is contributed // by Sach_Code ?>
Javascript
<script> // Java script program to find the sum of all // multiples of 2 and 5 below N // Function to find sum of AP series function sumAP(n, d) { // Number of terms n = parseInt(n / d); return (n) * ((1 + n) * parseInt(d) / 2); } // Function to find the sum of all // multiples of 2 and 5 below N function sumMultiples(n) { // Since, we need the sum of // multiples less than N n--; return sumAP(n, 2) + sumAP(n, 5) - sumAP(n, 10); } // Driver code n = 20; document.write( sumMultiples(n)); // This code is contributed by bobby </script>
110
Complejidad temporal: O(1), ya que no hay bucle ni recursividad.
Espacio Auxiliar: O(1), ya que no se ha ocupado ningún espacio extra.
Publicación traducida automáticamente
Artículo escrito por pawan_asipu y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA