兩相異正數的等差中項和等比中項


#include <iostream>
#include <cmath>
#include <iomanip>
#include <vector>
using namespace std;


//求數列an的第n項
double ArithmeticalMean(double a, double b, int n)
{
	vector<double> temp1(n + 1);
	vector<double> temp2(n + 1);

	temp1[0] = a;
	temp2[0] = b;

	for (int i = 1; i <= n; ++i)
	{
		temp1[i] = (temp1[i - 1] + temp2[i - 1]) / 2;
		temp2[i] = sqrt(temp1[i - 1] * temp2[i - 1]);
	}

	return temp1[n];
}

	

int main()
{
	cout << setprecision(16) << ArithmeticalMean(200000, 1, 5) << endl;

	cout << ArithmeticalMean(200000, 1, 100) << endl;	
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章