有限幾何級数2
y[n] = x0 + x1 + x2 + x3 + ...... + xn-1は、|x|<1のときy[∞] = 1/(1 - x)である。
x=0.5のときnが最低いくつであればn=∞のときの真値2.0との誤差が0.1%未満になるか。
finiteGeom_2.vi - Google ドライブ
.tns
ceiling(right(solve(abs(1-((∑(x^(n-1),n,1,n_min))/(((−1)/(x-1)))))*100<percent,n_min)))|x=((1)/(2)) and percent=((1)/(10))
.py
x = 0.5 trueVal = 1/(1 - x) maxPercent = 0.1 total = 0 for n in range(1, 999): total += x**(n-1) deviationPercent = abs(1 - (total/trueVal)) * 100 print("n: %2d, total: %0.6f, trueVal: %.1f, deviationPercent: %6.3f%%" % (n, total, trueVal, deviationPercent)) if deviationPercent < maxPercent: break
.cpp
#include <iostream> #include <cmath> using namespace std; int main(){ double x = 0.5; double trueVal = 1.0/(1.0 - x); double maxPercent = 0.1; double total = 0.0; for(int n=1; n<999; n++){ total += pow(x, (n-1)); double deviationPercent = abs(1.0 - (total/trueVal)) * 100.0; printf("n: %2d, total: %.6f, trueVal: %.2f, deviationPercent: %6.3f%%\n", n, total, trueVal, deviationPercent); if(deviationPercent < maxPercent){ break; } } return 0; }