自然対数eのプログラムです。
1000桁を求めています。
言語はVC++ 2008のコマンドプロンプトで動作しています。
※見やすくするため、インデント部分に全角を入れてあります。
※そのため、そのままコピーするとエラーになります。
eをテイラー展開すると次のようになります。
e = ∑n!-1 = 1 + 2!-1 + 3!-1 + ..... +n!-1 + .....
テイラー定理の解説と証明
※正確には、上記の式は「マクロリーン展開」または「x = 0 の時のテイラー展開」が正しいです。
<サンプルプログラム>
#include <iostream>
using namespace std;
void ladd(short *, short *, short *); //足し算
void ldiv(short *, short , short *); //割り算
void result(short *); //結果
#define L 1000 //求める桁数
#define L1 ((L / 4) + 1) //配列サイズ
#define L2 (L1 + 1) //一つ余分に取る
#define N 451 //計算する項数
//***** メイン処理 *****
void main(void)
{
static short s[L2 + 2], w[L2 + 2];
short idx;
//配列の初期化
for (idx = 0; idx <= L2; idx++)
{
s[idx] = w[idx] = 0;
}
//計算
s[0] = w[0] = 1;
for (idx = 1; idx <= N; idx++)
{
ldiv(w, idx, w);
ladd(s, w, s);
}
//結果を表示
result(s);
}
//***** 計算関数 *****
//足し算
void ladd(short a[], short b[], short c[])
{
short i, cy = 0;
for (i = L2; i >= 0; i--)
{
c[i] = a[i] + b[i] + cy;
if (c[i] <10000)
else
{
c[i] -= 10000;
cy = 1;
}
}
}
//割り算
void ldiv(short a[], short b, short c[])
{
short i; long d, rem = 0;
for (i = 0; i <= L2; i++)
{
d = a[i];
c[i] = (short)((d + rem) / b);
rem = ((d + rem) % b) * 10000;
}
}
//***** 結果の表示 *****
void result(short c[])
{
short i;
cout << c[0] << "." << endl;
for (i = 1; i <L1; i++)
cout.width(4); //4桁を表示
cout.fill('0'); //4桁を0を表示 74 ⇒ 0074
cout << c[i] << " ";
}
cout << endl;
}
//結果
e = 2.
7182 8182 8459 0452 3536 0287 4713 5266 2497 7572 4709 3699 9595 7496 6967 6277
2407 6630 3535 4759 4571 3821 7852 5166 4274 2746 6391 9320 0305 9921 8174 1359
6629 0435 7290 0334 2952 6059 5630 7381 3232 8627 9434 9076 3233 8298 8075 3195
2510 1901 1573 8341 8793 0702 1540 8914 9934 8841 6750 9244 7614 6066 8082 2648
0016 8477 4118 5374 2345 4424 3710 7539 0777 4499 2069 5517 0276 1838 6062 6133
1384 5830 0075 2044 9338 2656 0297 6067 3711 3200 7093 2870 9127 4437 4704 7230
6969 7720 9310 1416 9283 6819 0255 1510 8657 4637 7211 1252 3897 8442 5056 9536
9677 0785 4499 6996 7946 8644 5490 5987 9316 3688 9230 0987 9312 7736 1782 1542
4999 2295 7635 1482 2082 6989 5193 6680 3318 2528 8693 9849 6465 1058 2093 9239
8294 8879 3320 3625 0944 3117 3012 3819 7068 4161 4039 7019 8376 7932 0683 2823
7646 4804 2953 1180 2328 7825 0981 9455 8153 0175 6717 3613 3206 9811 2509 9618
1881 5930 4169 0351 5988 8851 9345 8072 7386 6738 5894 2287 9228 4998 9208 6805
8257 4927 9610 4841 9844 4363 4632 4496 8487 5602 3362 4827 0419 7862 3209 0021
6099 0235 3043 6994 1849 1463 1409 3431 7381 4364 0546 2531 5209 6183 6908 8870
7016 7683 9642 4378 1405 9271 4563 5490 6130 3107 2085 1038 3750 5101 1574 7704
1718 9861 0687 3969 6552 1267 1546 8895 7035 0354
1000桁を求めています。
言語はVC++ 2008のコマンドプロンプトで動作しています。
※見やすくするため、インデント部分に全角を入れてあります。
※そのため、そのままコピーするとエラーになります。
eをテイラー展開すると次のようになります。
e = ∑n!-1 = 1 + 2!-1 + 3!-1 + ..... +n!-1 + .....
テイラー定理の解説と証明
※正確には、上記の式は「マクロリーン展開」または「x = 0 の時のテイラー展開」が正しいです。
<サンプルプログラム>
#include <iostream>
using namespace std;
void ladd(short *, short *, short *); //足し算
void ldiv(short *, short , short *); //割り算
void result(short *); //結果
#define L 1000 //求める桁数
#define L1 ((L / 4) + 1) //配列サイズ
#define L2 (L1 + 1) //一つ余分に取る
#define N 451 //計算する項数
//***** メイン処理 *****
void main(void)
{
static short s[L2 + 2], w[L2 + 2];
short idx;
//配列の初期化
for (idx = 0; idx <= L2; idx++)
{
s[idx] = w[idx] = 0;
}
//計算
s[0] = w[0] = 1;
for (idx = 1; idx <= N; idx++)
{
ldiv(w, idx, w);
ladd(s, w, s);
}
//結果を表示
result(s);
}
//***** 計算関数 *****
//足し算
void ladd(short a[], short b[], short c[])
{
short i, cy = 0;
for (i = L2; i >= 0; i--)
{
c[i] = a[i] + b[i] + cy;
if (c[i] <10000)
else
{
c[i] -= 10000;
cy = 1;
}
}
}
//割り算
void ldiv(short a[], short b, short c[])
{
short i; long d, rem = 0;
for (i = 0; i <= L2; i++)
{
d = a[i];
c[i] = (short)((d + rem) / b);
rem = ((d + rem) % b) * 10000;
}
}
//***** 結果の表示 *****
void result(short c[])
{
short i;
cout << c[0] << "." << endl;
for (i = 1; i <L1; i++)
cout.width(4); //4桁を表示
cout.fill('0'); //4桁を0を表示 74 ⇒ 0074
cout << c[i] << " ";
}
cout << endl;
}
//結果
e = 2.
7182 8182 8459 0452 3536 0287 4713 5266 2497 7572 4709 3699 9595 7496 6967 6277
2407 6630 3535 4759 4571 3821 7852 5166 4274 2746 6391 9320 0305 9921 8174 1359
6629 0435 7290 0334 2952 6059 5630 7381 3232 8627 9434 9076 3233 8298 8075 3195
2510 1901 1573 8341 8793 0702 1540 8914 9934 8841 6750 9244 7614 6066 8082 2648
0016 8477 4118 5374 2345 4424 3710 7539 0777 4499 2069 5517 0276 1838 6062 6133
1384 5830 0075 2044 9338 2656 0297 6067 3711 3200 7093 2870 9127 4437 4704 7230
6969 7720 9310 1416 9283 6819 0255 1510 8657 4637 7211 1252 3897 8442 5056 9536
9677 0785 4499 6996 7946 8644 5490 5987 9316 3688 9230 0987 9312 7736 1782 1542
4999 2295 7635 1482 2082 6989 5193 6680 3318 2528 8693 9849 6465 1058 2093 9239
8294 8879 3320 3625 0944 3117 3012 3819 7068 4161 4039 7019 8376 7932 0683 2823
7646 4804 2953 1180 2328 7825 0981 9455 8153 0175 6717 3613 3206 9811 2509 9618
1881 5930 4169 0351 5988 8851 9345 8072 7386 6738 5894 2287 9228 4998 9208 6805
8257 4927 9610 4841 9844 4363 4632 4496 8487 5602 3362 4827 0419 7862 3209 0021
6099 0235 3043 6994 1849 1463 1409 3431 7381 4364 0546 2531 5209 6183 6908 8870
7016 7683 9642 4378 1405 9271 4563 5490 6130 3107 2085 1038 3750 5101 1574 7704
1718 9861 0687 3969 6552 1267 1546 8895 7035 0354
※コメント投稿者のブログIDはブログ作成者のみに通知されます