Dado un rango L y R, la tarea es encontrar la suma de todos los números naturales en el rango L a R.
Ejemplos :
Input: L = 2, R = 5 Output: 14 2 + 3 + 4 + 5 = 14 Input: L = 10, R = 20 Output: 165
Un enfoque ingenuo es atravesar de L a R y sumar todos los elementos uno por uno para obtener la suma.
Un enfoque eficiente es usar la fórmula para la suma de los primeros N números naturales . La idea del principio de inclusión-exclusión ayuda a resolver el problema anterior. Encuentre la suma de los números naturales hasta R y L-1 y luego reste sum(R)-sum(l-1) .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to print the sum // of all numbers in range L and R #include <bits/stdc++.h> using namespace std; // Function to return the sum of // all natural numbers int sumNatural(int n) { int sum = (n * (n + 1)) / 2; return sum; } // Function to return the sum // of all numbers in range L and R int suminRange(int l, int r) { return sumNatural(r) - sumNatural(l - 1); } // Driver Code int main() { int l = 2, r = 5; cout << "Sum of Natural numbers from L to R is " << suminRange(l, r); return 0; }
Java
// Java program to print the sum // of all numbers in range L and R class GFG{ // Function to return the sum of // all natural numbers static int sumNatural(int n) { int sum = (n * (n + 1)) / 2; return sum; } // Function to return the sum // of all numbers in range L and R static int suminRange(int l, int r) { return sumNatural(r) - sumNatural(l - 1); } // Driver Code public static void main(String[] args) { int l = 2, r = 5; System.out.println("Sum of Natural numbers from L to R is "+suminRange(l, r)); } } // This code is contributed by mits
Python3
# Python3 program to print the sum of # all numbers in range L and R # Function to return the sum of all natural numbers def sumNatural(n): sum = (n*(n+1))//2 return sum # Function to return the sum # of all numbers in range L and R def suminRange(l, r): return sumNatural(r) - sumNatural(l-1) # Driver Code l =2; r= 5 print("Sum of Natural numbers from L to R is ",suminRange(l, r)) # This code is contributed by Shrikant13
C#
// C# program to print the sum // of all numbers in range L and R using System; class GFG { // Function to return the sum // of all natural numbers static int sumNatural(int n) { int sum = (n * (n + 1)) / 2; return sum; } // Function to return the sum // of all numbers in range L and R static int suminRange(int l, int r) { return sumNatural(r) - sumNatural(l - 1); } // Driver Code static public void Main () { int l = 2, r = 5; Console.WriteLine("Sum of Natural numbers " + "from L to R is " + suminRange(l, r)); } } // This code is contributed by akt_mit
PHP
<?php // PHP program to print the sum // of all numbers in range L and R // Function to return the sum of // all natural numbers function sumNatural($n) { $sum = ($n * ($n + 1)) / 2; return $sum; } // Function to return the sum // of all numbers in range L and R function suminRange($l, $r) { return sumNatural($r) - sumNatural($l - 1); } // Driver Code $l = 2; $r = 5; echo "Sum of Natural numbers " . "from L to R is ", suminRange($l, $r); // This code is contributed by ajit ?>
Javascript
<script> // JavaScript program to print the sum // of all numbers in range L and R // Function to return the sum of // all natural numbers function sumNatural(n) { sum = (n * (n + 1)) / 2; return sum; } // Function to return the sum // of all numbers in range L and R function suminRange(l, r) { return sumNatural(r) - sumNatural(l - 1); } // Driver Code let l = 2; let r = 5; document.write("Sum of Natural numbers from L to R is "+ suminRange(l, r)); // This code is contributed by sravan kumar gottumukkalan </script>
Sum of Natural numbers from L to R is 14
Complejidad de tiempo: O(1)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por swetankmodi y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA