Dado que la relación entre la suma de los primeros m y n términos de un AP con el primer término ‘a’ y la diferencia común ‘d’ es m^2:n^2 . La tarea es encontrar la relación entre el m-ésimo y el n-ésimo término de este AP
. Ejemplos:
Input: m = 3, n = 2 Output: 1.6667 Input: m = 5, n = 3 Output: 1.8
Enfoque:
Sea la Suma de los primeros m y n términos denotada por Sm y Sn respectivamente.
Además, denotemos el término m-ésimo y n-ésimo por tm y tn respectivamente.
Sm = (m * [ 2*a + (m-1)*d ])/2
Sn = (n * [ 2*a + (n-1)*d ])/2
Dado: Sm / Sn = m^ 2 / n^2
Por lo tanto, ((m * [ 2*a + (m-1)*d ])/2) / ((n * [ 2*a + (n-1)*d ])/2) = m^2 / n^2
=> (2*a + (m-1)*d) / (2*a + (n-1)*d) = m / n
al multiplicar en cruz y resolver, obtenemos
d = 2 * a
Por lo tanto, los términos m-ésimo y n-ésimo se pueden escribir como:
término m-ésimo = tm = a +(m-1)*d = a + (m-1)*(2*a)
término n-ésimo = tn = a +(n-1)*d = a + (n-1)*(2*a)
Por tanto la relación será:
tm / tn = (a + (m-1)*(2*a)) / (a + (n-1)*(2*a))
tm / tn = (2*m – 1) / (2*n – 1)
A continuación se muestra la implementación requerida:
C++
// C++ code to calculate ratio #include <bits/stdc++.h> using namespace std; // function to calculate ratio of mth and nth term float CalculateRatio(float m, float n) { // ratio will be tm/tn = (2*m - 1)/(2*n - 1) return (2 * m - 1) / (2 * n - 1); } // Driver code int main() { float m = 6, n = 2; cout << CalculateRatio(m, n); return 0; }
Java
// Java code to calculate ratio import java.io.*; class Nth { // function to calculate ratio of mth and nth term static float CalculateRatio(float m, float n) { // ratio will be tm/tn = (2*m - 1)/(2*n - 1) return (2 * m - 1) / (2 * n - 1); } } // Driver code class GFG { public static void main (String[] args) { float m = 6, n = 2; Nth a=new Nth(); System.out.println(a.CalculateRatio(m, n)); } } // this code is contributed by inder_verma..
Python3
# Python3 program to calculate ratio # function to calculate ratio # of mth and nth term def CalculateRatio(m, n): # ratio will be tm/tn = (2*m - 1)/(2*n - 1) return (2 * m - 1) / (2 * n - 1); # Driver code if __name__=='__main__': m = 6; n = 2; print (float(CalculateRatio(m, n))); # This code is contributed by # Shivi_Aggarwal
C#
// C# code to calculate ratio using System; class Nth { // function to calculate ratio of mth and nth term float CalculateRatio(float m, float n) { // ratio will be tm/tn = (2*m - 1)/(2*n - 1) return (2 * m - 1) / (2 * n - 1); } // Driver code public static void Main () { float m = 6, n = 2; Nth a=new Nth(); Console.WriteLine(a.CalculateRatio(m, n)); } } // this code is contributed by anuj_67.
PHP
<?php // PHP code to calculate ratio // function to calculate ratio // of mth and nth term function CalculateRatio( $m, $n) { // ratio will be tm/tn = (2*m - 1)/(2*n - 1) return (2 * $m - 1) / (2 * $n - 1); } // Driver code $m = 6; $n = 2; echo CalculateRatio($m, $n); // This code is contributed // by inder_verma ?>
Javascript
<script> // JavaScript code to calculate ratio // function to calculate ratio of mth and nth term function CalculateRatio(m, n) { // ratio will be tm/tn = (2*m - 1)/(2*n - 1) return (2 * m - 1) / (2 * n - 1); } // Driver code let m = 6, n = 2; document.write(CalculateRatio(m, n)); // This code is contributed by Surbhi Tyagi. </script>
3.66667
Tiempo Complejidad: O(1)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por Shashank_Sharma y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA