Dados tres enteros A, B y N, la tarea es encontrar N medias aritméticas entre A y B. Básicamente, necesitamos insertar N términos en una progresión aritmética . donde A y B son los términos primero y último. Ejemplos:
Input : A = 20 B = 32 N = 5 Output : 22 24 26 28 30 The Arithmetic progression series as 20 22 24 26 28 30 32 Input : A = 5 B = 35 N = 5 Output : 10 15 20 25 30
Método: Sean A 1 , A 2 , A 3 , A 4 ……A n N Medias aritméticas entre dos números dados A y B . Entonces A, A 1 , A 2 ….. A n , B estarán en progresión aritmética. Ahora B = (N+2) el término de la progresión aritmética. Entonces: encontrar el término ( N +2) de la serie de progresión aritmética donde d es la diferencia común B = A + (N + 2 – 1)d B – A = (N + 1)d Entonces la diferencia común d es dada por. d = (B – A) / (N + 1) Así que ahora tenemos el valor de A y el valor de la diferencia común (d), ahora podemos encontrar todas las N medias aritméticas entre A y B.
C++
// C++ program to find n arithmetic // means between A and B #include <bits/stdc++.h> using namespace std; // Prints N arithmetic means between // A and B. void printAMeans(int A, int B, int N) { // calculate common difference(d) float d = (float)(B - A) / (N + 1); // for finding N the arithmetic // mean between A and B for (int i = 1; i <= N; i++) cout << (A + i * d) <<" "; } // Driver code to test above int main() { int A = 20, B = 32, N = 5; printAMeans(A, B, N); return 0; }
Java
// java program to illustrate // n arithmetic mean between // A and B import java.io.*; import java.lang.*; import java.util.*; public class GFG { // insert function for calculating the means static void printAMeans(int A, int B, int N) { // Finding the value of d Common difference float d = (float)(B - A) / (N + 1); // for finding N the Arithmetic // mean between A and B for (int i = 1; i <= N; i++) System.out.print((A + i * d) + " "); } // Driver code public static void main(String args[]) { int A = 20, B = 32, N = 5; printAMeans(A, B, N); } }
Python3
# Python3 program to find n arithmetic # means between A and B # Prints N arithmetic means # between A and B. def printAMeans(A, B, N): # Calculate common difference(d) d = (B - A) / (N + 1) # For finding N the arithmetic # mean between A and B for i in range(1, N + 1): print(int(A + i * d), end = " ") # Driver code A = 20; B = 32; N = 5 printAMeans(A, B, N) # This code is contributed by Smitha Dinesh Semwal
C#
// C# program to illustrate // n arithmetic mean between // A and B using System; public class GFG { // insert function for calculating the means static void printAMeans(int A, int B, int N) { // Finding the value of d Common difference float d = (float)(B - A) / (N + 1); // for finding N the Arithmetic // mean between A and B for (int i = 1; i <= N; i++) Console.Write((A + i * d) + " "); } // Driver code public static void Main() { int A = 20, B = 32, N = 5; printAMeans(A, B, N); } } // Contributed by vt_m
PHP
<?php // PHP program to find n arithmetic // means between A and B // Prints N arithmetic means // between A and B. function printAMeans($A, $B, $N) { // calculate common // difference(d) $d = ($B - $A) / ($N + 1); // for finding N the arithmetic // mean between A and B for ($i = 1; $i <= $N; $i++) echo ($A + $i * $d) ," "; } // Driver Code $A = 20; $B = 32; $N = 5; printAMeans($A, $B, $N); // This code is Contributed by vt_m. ?>
Javascript
<script> // JavaScript program to find n arithmetic // means between A and B // Prints N arithmetic means // between A and B. function printAMeans(A, B, N){ // Calculate common difference(d) let d = (B - A) / (N + 1) // For finding N the arithmetic // mean between A and B for(let i = 1; i < N + 1; i++) document.write(Math.floor(A + i * d)," ") } // Driver code let A = 20, B = 32, N = 5; printAMeans(A, B, N) // This code is contributed by Shinjanpatra </script>
22 24 26 28 30
Complejidad temporal: O(N), donde N es el número de términos
Complejidad espacial : O(1)
Publicación traducida automáticamente
Artículo escrito por Manish_100 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA