Dado un número N , la tarea es encontrar la suma de todos los múltiplos de 3 y 7 debajo de N.
Nota: Un número no debe repetirse en la suma.
Ejemplos:
Entrada: N = 10
Salida: 25
3 + 6 + 7 + 9 = 25Entrada: N = 24
Salida: 105
3 + 6 + 7 + 9 + 12 + 14 + 15 + 18 + 21 = 105
Acercarse:
- Sabemos que los múltiplos de 3 forman un AP como S 3 = 3 + 6 + 9 + 12 + 15 + 18 + 21 + …
- Y los múltiplos de 7 forman un AP como S 7 = 7 + 14 + 21 + 28 + …
- Ahora, Suma = S 3 + S 7 es decir , 3 + 6 + 7 + 9 + 12 + 14 + 15 + 18 + 21 + 21 + …
- Desde el paso anterior, 21 se repite dos veces. De hecho, todos los múltiplos de 21 (o 3*7) se repetirán ya que se cuentan dos veces, una vez en la serie S 3 y otra vez en la serie S 7 . Entonces, los múltiplos de 21 deben descartarse del resultado.
- Entonces, el resultado final será S 3 + S 7 – S 21
La fórmula para la suma de una serie AP es:
n * ( a + l ) / 2
Donde n es el número de términos, a es el término inicial y l es el último término.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to find the sum of all // multiples of 3 and 7 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 3 and 7 below N long long sumMultiples(long long n) { // Since, we need the sum of // multiples less than N n--; return sumAP(n, 3) + sumAP(n, 7) - sumAP(n, 21); } // Driver code int main() { long long n = 24; cout << sumMultiples(n); return 0; }
Java
// Java program to find the sum of all // multiples of 3 and 7 below N import java.util.*; class solution { // 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 3 and 7 below N static long sumMultiples(long n) { // Since, we need the sum of // multiples less than N n--; return sumAP(n, 3) + sumAP(n, 7) - sumAP(n, 21); } // Driver code public static void main(String args[]) { long n = 24; System.out.println(sumMultiples(n)); } } //This code is contributed by Surendra_Gangwar
Python3
# Python3 program to find the sum of # all multiples of 3 and 7 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 3 and 7 below N def sumMultiples(n): # Since, we need the sum of # multiples less than N n -= 1; return int(sumAP(n, 3) + sumAP(n, 7) - sumAP(n, 21)); # Driver code n = 24; print(sumMultiples(n)); # This code is contributed # by mits
C#
// C# program to find the sum of all // multiples of 3 and 7 below N using System; 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 3 and 7 below N static long sumMultiples(long n) { // Since, we need the sum of // multiples less than N n--; return sumAP(n, 3) + sumAP(n, 7) - sumAP(n, 21); } // Driver code static public void Main(String []args) { long n = 24; Console.WriteLine(sumMultiples(n)); } } // This code is contributed // by Arnab Kundu
PHP
<?php // PHP program to find the sum of all // multiples of 3 and 7 below N // Function to find sum of AP series function 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 3 and 7 below N function sumMultiples($n) { // Since, we need the sum of // multiples less than N $n--; return sumAP($n, 3) + sumAP($n, 7) - sumAP($n, 21); } // Driver code $n = 24; echo sumMultiples($n); // This code is contributed // by Akanksha Rai ?>
Javascript
<script> // JavaScript program to find the sum of all // multiples of 3 and 7 below N // Function to find sum of AP series function sumAP(n, d) { // Number of terms n = parseInt(n / d); return (n) * (1 + n) * (d / 2); } // Function to find the sum of all // multiples of 3 and 7 below N function sumMultiples(n) { // Since, we need the sum of // multiples less than N n--; return sumAP(n, 3) + sumAP(n, 7) - sumAP(n, 21); } // Driver code let n = 24; document.write(sumMultiples(n)); // This code is contributed by mohan1240760 </script>
105
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