Dado un entero positivo, N . Encuentre la suma del primer N término de la serie-
1/1*3, 1/3*5, 1/5*7, ….
Ejemplos :
Entrada : N = 3
Salida : 0.428571
Entrada : N = 1
Salida : 0.333333
Enfoque : La secuencia se forma usando el siguiente patrón. Para cualquier valor N-
S norte = norte / (2 * norte + 1)
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to return sum of // N term of the series double findSum(int N) { return (double)N / (2 * N + 1); } // Driver Code int main() { int N = 3; cout << findSum(N); }
Java
// JAVA program to implement // the above approach import java.util.*; class GFG { // Function to return sum of // N term of the series public static double findSum(int N) { return (double)N / (2 * N + 1); } // Driver Code public static void main(String[] args) { int N = 3; System.out.print(findSum(N)); } } // This code is contributed by Taranpreet
Python3
# Python 3 program for the above approach # Function to return sum of # N term of the series def findSum(N): return N / (2 * N + 1) # Driver Code if __name__ == "__main__": # Value of N N = 3 print(findSum(N)) # This code is contributed by Abhishek Thakur.
C#
// C# program to implement // the above approach using System; class GFG { // Function to return sum of // N term of the series public static double findSum(int N) { return (double)N / (2 * N + 1); } // Driver Code public static void Main() { int N = 3; Console.Write(findSum(N)); } } // This code is contributed by gfgking
Javascript
<script> // Javascript program to implement // the above approach // Function to return sum of // N term of the series function findSum(N) { return N / (2 * N + 1); } // Driver Code let N = 3; document.write(findSum(N)); // This code is contributed by Palak Gupta </script>
Producción
0.428571
Complejidad de Tiempo : O(1)
Espacio Auxiliar : O(1)
Publicación traducida automáticamente
Artículo escrito por akashjha2671 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA