圓周率的計算 (轉)
圓周率的計算 (轉)[@more@]/* Part 1: 概念
圓周率是在一個圓上作『內接正N邊形』和『外切正N邊形』
當N越大時,所作出來的這兩個正N邊形的『周長』也就會越接近
當然啦!這個『周長』也就會越接近這個圓的圓周了
然後再以基本定義求出圓周率
定義:
圓的周長
π(圓周率)= ─────
直徑
資料來源:牛頓雜誌 */
/* Part 2: 執行結果
下列是算到小數點以下一千位的圓周率(要算更多也可以):
3.1415926535 8979323846 2643383279 5028841971 6939937510 5820974944
5923078164 0628620899 8628034825 3421170679 8214808651 3282306647
0938446095 5058223172 5359408128 4811174502 8410270193 8521105559
6446229489 5493038196 4428810975 6659334461 2847564823 3786783165
2712019091 4564856692 3460348610 4543266482 1339360726 0249141273
7245870066 0631558817 4881520920 9628292540 9171536436 7892590360
0113305305 4882046652 1384146951 9415116094 3305727036 5759591953
0921861173 8193261179 3105118548 0744623799 6274956735 1885752724
8912279381 8301194912 9833673362 4406566430 8602139494 6395224737
1907021798 6094370277 0539217176 2931767523 8467481846 7669405132
0005681271 4526356082 7785771342 7577896091 7363717872 1468440901
2249534301 4654958537 1050792279 6892589235 4201995611 2129021960
8640344181 5981362977 4771309960 5187072113 4999999837 2978049951
0597317328 1609631859 5024459455 3469083026 4252230825 3344685035
2619311881 7101000313 7838752886 5875332083 8142061717 7669147303
5982534904 2875546873 1159562863 8823537875 9375195778 1857780532
1712268066 1300192787 6611195909 2164201989
*/
/* 源如下:??
** PI.C - Computes Pi to an arbitrary number of digits
**
** Uses far arrays so may be compiled in any memory model
*/
#include
#include
#if defined(__ZTC__)
#include
#define FAR _far
#define Fcalloc farcalloc
#define Ffree farfree
#define Size_T unsigned long
#elif defined(__TURBOC__)
#include
#define FAR far
#define Fcalloc farcalloc
#define Ffree farfree
#define Size_T unsigned long
#else /* assume MSC/QC */
#include
#define FAR _far
#define Fcalloc _fcalloc
#define Ffree _ffree
#define Size_T size_t
#endif
long kf, ks;
long FAR *mf, FAR *ms;
long cnt, n, temp, nd;
long i;
long col, col1;
long loc, stor[21];
void shift(long FAR *l1, long FAR *l2, long lp, long lmod)
{
long k;
k = ((*l2) > 0 ? (*l2) / lmod: -(-(*l2) / lmod) - 1);
*l2 -= k * lmod;
*l1 += k * lp;
}
void yprint(long m)
{
if (cnt {
if (++col == 11)
{
col = 1;
if (++col1 == 6)
{
col1 = 0;
printf("n");
printf("%4ld",m%10);
}
else printf("%3ld",m%10);
}
else printf("%ld",m);
cnt++;
}
}
void xprint(long m)
{
long ii, wk, wk1;
if (m < 8)
{
for (ii = 1; ii <= loc; )
yprint(stor[(int)(ii++)]);
loc = 0;
}
else
{
if (m > 9)
{
wk = m / 10;
m %= 10;
for (wk1 = loc; wk1 >= 1; wk1--)
{
wk += stor[(int)wk1];
stor[(int)wk1] = wk % 10;
wk /= 10;
}
}
}
stor[(int)(++loc)] = m;
}
void memerr(int errno)
{
printf("anOut of memory error #%dn", errno);
if (2 == errno)
Ffree(mf);
_exit(2);
}
int main(int argc, char *argv[])
{
int i=0;
char *endp;
stor[i++] = 0;
if (argc < 2)
{
puts("aUsage: PI");
return(1);
}
n = strtol(argv[1], &endp, 10);
if (NULL == (mf = Fcalloc((Size_T)(n + 3L), (Size_T)sizeof(long))))
memerr(1);
if (NULL == (ms = Fcalloc((Size_T)(n + 3L), (Size_T)sizeof(long))))
memerr(2);
printf("nApproximation of PI to %ld digitsn", (long)n);
cnt = 0;
kf = 25;
ks = 57121L;
mf[1] = 1L;
for (i = 2; i <= (int)n; i += 2)
{
mf[i] = -16L;
mf[i+1] = 16L;
}
for (i = 1; i <= (int)n; i += 2)
{
ms[i] = -4L;
ms[i+1] = 4L;
}
printf("n 3.");
while (cnt < n)
{
for (i = 0; ++i <= (int)n - (int)cnt; )
{
mf[i] *= 10L;
ms[i] *= 10L;
}
for (i =(int)(n - cnt + 1); --i >= 2; )
{
temp = 2 * i - 1;
shift(&mf[i - 1], &mf[i], temp - 2, temp * kf);
shift(&ms[i - 1], &ms[i], temp - 2, temp * ks);
}
nd = 0;
shift((long FAR *)&nd, &mf[1], 1L, 5L);
shift((long FAR *)&nd, &ms[1], 1L, 239L);
xprint(nd);
}
printf("nnCalculations Completed!n");
Ffree(ms);
Ffree(mf);
return(0);
}
圓周率是在一個圓上作『內接正N邊形』和『外切正N邊形』
當N越大時,所作出來的這兩個正N邊形的『周長』也就會越接近
當然啦!這個『周長』也就會越接近這個圓的圓周了
然後再以基本定義求出圓周率
定義:
圓的周長
π(圓周率)= ─────
直徑
資料來源:牛頓雜誌 */
/* Part 2: 執行結果
下列是算到小數點以下一千位的圓周率(要算更多也可以):
3.1415926535 8979323846 2643383279 5028841971 6939937510 5820974944
5923078164 0628620899 8628034825 3421170679 8214808651 3282306647
0938446095 5058223172 5359408128 4811174502 8410270193 8521105559
6446229489 5493038196 4428810975 6659334461 2847564823 3786783165
2712019091 4564856692 3460348610 4543266482 1339360726 0249141273
7245870066 0631558817 4881520920 9628292540 9171536436 7892590360
0113305305 4882046652 1384146951 9415116094 3305727036 5759591953
0921861173 8193261179 3105118548 0744623799 6274956735 1885752724
8912279381 8301194912 9833673362 4406566430 8602139494 6395224737
1907021798 6094370277 0539217176 2931767523 8467481846 7669405132
0005681271 4526356082 7785771342 7577896091 7363717872 1468440901
2249534301 4654958537 1050792279 6892589235 4201995611 2129021960
8640344181 5981362977 4771309960 5187072113 4999999837 2978049951
0597317328 1609631859 5024459455 3469083026 4252230825 3344685035
2619311881 7101000313 7838752886 5875332083 8142061717 7669147303
5982534904 2875546873 1159562863 8823537875 9375195778 1857780532
1712268066 1300192787 6611195909 2164201989
*/
/* 源如下:??
** PI.C - Computes Pi to an arbitrary number of digits
**
** Uses far arrays so may be compiled in any memory model
*/
#include
#include
#if defined(__ZTC__)
#include
#define FAR _far
#define Fcalloc farcalloc
#define Ffree farfree
#define Size_T unsigned long
#elif defined(__TURBOC__)
#include
#define FAR far
#define Fcalloc farcalloc
#define Ffree farfree
#define Size_T unsigned long
#else /* assume MSC/QC */
#include
#define FAR _far
#define Fcalloc _fcalloc
#define Ffree _ffree
#define Size_T size_t
#endif
long kf, ks;
long FAR *mf, FAR *ms;
long cnt, n, temp, nd;
long i;
long col, col1;
long loc, stor[21];
void shift(long FAR *l1, long FAR *l2, long lp, long lmod)
{
long k;
k = ((*l2) > 0 ? (*l2) / lmod: -(-(*l2) / lmod) - 1);
*l2 -= k * lmod;
*l1 += k * lp;
}
void yprint(long m)
{
if (cnt
if (++col == 11)
{
col = 1;
if (++col1 == 6)
{
col1 = 0;
printf("n");
printf("%4ld",m%10);
}
else printf("%3ld",m%10);
}
else printf("%ld",m);
cnt++;
}
}
void xprint(long m)
{
long ii, wk, wk1;
if (m < 8)
{
for (ii = 1; ii <= loc; )
yprint(stor[(int)(ii++)]);
loc = 0;
}
else
{
if (m > 9)
{
wk = m / 10;
m %= 10;
for (wk1 = loc; wk1 >= 1; wk1--)
{
wk += stor[(int)wk1];
stor[(int)wk1] = wk % 10;
wk /= 10;
}
}
}
stor[(int)(++loc)] = m;
}
void memerr(int errno)
{
printf("anOut of memory error #%dn", errno);
if (2 == errno)
Ffree(mf);
_exit(2);
}
int main(int argc, char *argv[])
{
int i=0;
char *endp;
stor[i++] = 0;
if (argc < 2)
{
puts("aUsage: PI
return(1);
}
n = strtol(argv[1], &endp, 10);
if (NULL == (mf = Fcalloc((Size_T)(n + 3L), (Size_T)sizeof(long))))
memerr(1);
if (NULL == (ms = Fcalloc((Size_T)(n + 3L), (Size_T)sizeof(long))))
memerr(2);
printf("nApproximation of PI to %ld digitsn", (long)n);
cnt = 0;
kf = 25;
ks = 57121L;
mf[1] = 1L;
for (i = 2; i <= (int)n; i += 2)
{
mf[i] = -16L;
mf[i+1] = 16L;
}
for (i = 1; i <= (int)n; i += 2)
{
ms[i] = -4L;
ms[i+1] = 4L;
}
printf("n 3.");
while (cnt < n)
{
for (i = 0; ++i <= (int)n - (int)cnt; )
{
mf[i] *= 10L;
ms[i] *= 10L;
}
for (i =(int)(n - cnt + 1); --i >= 2; )
{
temp = 2 * i - 1;
shift(&mf[i - 1], &mf[i], temp - 2, temp * kf);
shift(&ms[i - 1], &ms[i], temp - 2, temp * ks);
}
nd = 0;
shift((long FAR *)&nd, &mf[1], 1L, 5L);
shift((long FAR *)&nd, &ms[1], 1L, 239L);
xprint(nd);
}
printf("nnCalculations Completed!n");
Ffree(ms);
Ffree(mf);
return(0);
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-997975/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- VB計算圓周率的方法
- Oracle中實現圓周率計算(三)Oracle
- Oracle中實現圓周率計算(二)Oracle
- Oracle中實現圓周率計算(一)Oracle
- 幾種計算圓周率的軟體比較
- 圓周率π的計算曆程及各種腦洞大開的估計方法
- 使用Kubernetes裡的job計算圓周率後2000位
- Google:Google Cloud再次打破了百萬億位的圓周率計算紀錄GoCloud
- 如何用python求圓周率?Python
- 蒙特卡羅法求圓周率
- 蒙特卡羅演算法求圓周率Pi的值演算法
- 新紀錄!谷歌 Cloud 計算出圓周率“ π” 第 100 萬億位數谷歌Cloud
- 【倉頡】入門文件程式碼圓周率估算程式碼更正
- 計算圓的面積程式碼
- PLSQL實現計算圓的面積SQL
- 橢圓曲線加法原理計算
- 轉矩的計算?
- java 根據經緯度計算圓周Java
- Heartbeat Score的計算(轉)
- Java演算法 概率演算法(蒙特卡洛概率演算法求圓周率)Java演算法
- 申通 圓通 快遞發貨的運費計算方式
- 最簡單的Qt程式:根據使用者所輸入圓半徑計算圓面積QT
- 從雲端計算轉向邊緣計算
- 計算機儲存器的容量計算和地址轉換計算機
- 碎片化學習Java(五)-- Java計算圓柱體積Java
- SQL 轉置計算SQL
- 計算Java日期 (轉)Java
- 【轉】分散式計算的謬論分散式
- 計算cpu速度的小程式 (轉)
- (轉)MRP的計算步驟
- [計算幾何]圓與三角形是否相交
- 未來計算世界 (轉)
- IP地址分段計算 (轉)
- SQL Server日期計算 (轉)SQLServer
- 關於日期計算的問題 (轉)
- 萬能的計算日期函式(轉)函式
- 計算兩豎直直線與橢圓圍成部分面積
- 不止於流,615杭州流計算峰會圓滿舉行