返回   cpper编程论坛 > 算法
注册账号 论坛帮助 会员列表 日历事件 搜索 今日新帖 标记版面已读

回复
 
LinkBack 主题工具 显示模式
  #1 (permalink)  
旧 2006-02-24
polyrandom 的头像
超级版主
 
注册日期: 2002-09-03
帖子: 3,135
文章: 20
polyrandom 正向着好的方向发展
默认 大家把能找到的计算pi的程序/算法都贴在这里

下面是我现写的一个,抛风引钱。
思路是:假设有一个正方形,边长为RAND_MAX,我往里面随机地扔石头,那么扔在以一个角为圆心,RAND_MAX为半径的区域的概率是pi/4。
C++ 代码:
  1. #include <iostream>
  2. #include <stdlib.h>
  3.  
  4. double pi( __int64 iteration )
  5. {
  6.     __int64 hits = 0;
  7.     __int64 randMaxSquare = RAND_MAX * RAND_MAX;
  8.     for( __int64 i = 0; i < iteration; ++i )
  9.     {
  10.         int x = rand();
  11.         int y = rand();
  12.  
  13.         if( x * x + y * y <= randMaxSquare )
  14.             ++hits;
  15.     }
  16.     return (double)hits / iteration * 4;
  17. }
  18.  
  19. int main()
  20. {
  21.     __int64 iteration = 100;
  22.     for( int i = 1; i < 10; ++i )
  23.     {
  24.         std::cout << "iteration number: " << iteration << '\t' << pi( iteration ) << std::endl;
  25.         iteration *= 10;
  26.     }
  27.     return 0;
  28. }
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #2 (permalink)  
旧 2006-02-24
Meta 的头像
版主
 
注册日期: 2004-12-02
帖子: 226
文章: 5
Meta 正向着好的方向发展
默认

引用:
作者: polyrandom
下面是我现写的一个,抛风引钱。
思路是:假设有一个正方形,边长为RAND_MAX,我往里面随机地扔石头,那么扔在以一个角为圆心,RAND_MAX为半径的区域的概率是pi/4。
[highlight="C++"]#include <iostream>
#inclu...
无语, 这是去年SAP Lab的一道面试题目, 别人给我做过. 当时, 这道题就是问你程序的输出是什么.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #3 (permalink)  
旧 2006-02-24
polyrandom 的头像
超级版主
 
注册日期: 2002-09-03
帖子: 3,135
文章: 20
polyrandom 正向着好的方向发展
默认

没想到SAP Lab里面也有吾爱。
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #4 (permalink)  
旧 2006-02-24
高级会员
 
注册日期: 2002-09-16
帖子: 1,087
文章: 1
SpitFire 正向着好的方向发展
默认

如果用g++素不素会出错呢

over
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #5 (permalink)  
旧 2006-02-24
Elminster 的头像
超级版主
 
注册日期: 2002-09-09
帖子: 1,763
Elminster 正向着好的方向发展
默认

这个领域,级数为王,说真的那些程序贴起来也挺无聊的 ……
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #6 (permalink)  
旧 2006-02-25
cat cat 当前离线
高级会员
 
注册日期: 2003-11-06
帖子: 1,560
文章: 6
cat 正向着好的方向发展
默认

C 代码:
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. long a=10000,b,c=2800,d,e,f&#91;2801],g;
  5.  
  6. main()
  7. {
  8.     for(;b-c;)
  9.         f&#91;b++]=a/5;
  10.     for(;d=0,g=c*2;c-=14,printf("%.4d",e+d/a),e=d%a)
  11.     for(b=c;d+=f&#91;b]*a,f[b]=d%--g,d/=g--,--b;d*=b);
  12. }

记得以前有人贴过的,还被我说很丑的程序,后来发现是混沌大赛的作品。
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #7 (permalink)  
旧 2006-02-25
高级会员
 
注册日期: 2002-09-19
帖子: 839
文章: 7
tomato 正向着好的方向发展
默认

水一个,SAP Lab是什么东东?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #8 (permalink)  
旧 2006-02-25
初级会员
 
注册日期: 2006-02-09
帖子: 6
涅瓦纳主席 正向着好的方向发展
发送 MSN 消息给 涅瓦纳主席
默认 提供计算圆周率的级数公式

见附件
上传的图像
文件类型: gif CalcPi.gif (2.4 KB, 31 次查看)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #9 (permalink)  
旧 2006-02-25
bankrock 的头像
高级会员
 
注册日期: 2003-12-11
帖子: 843
文章: 7
bankrock 正向着好的方向发展
默认

引用:
作者: cat
[highlight="C"]
#include <stdlib.h>
#include <stdio.h>

long a=10000,b,c=2800,d,e,f[2801],g;

main()
{
for(;b-c...
见鬼了,原来还真有这种比赛的。世界上无聊的人还真不少
http://www.ioccc.org/
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #10 (permalink)  
旧 2006-02-25
housisong 的头像
高级会员
 
注册日期: 2003-08-28
住址: 深圳`
帖子: 446
文章: 6
housisong 正向着好的方向发展
发送 MSN 消息给 housisong
默认 快速收敛的PI公式

Borwein四次迭代式:

初值:
a0=6-4*2^0.5; y0=2^0.5-1;
重复计算:
y(n+1)=[1-(1-y^4)^0.25]/[1+(1-y^4)^0.25];
y=y(n+1);
a(n+1)=a*(1+y)^4-2^(2*n+3)*y*(1+y+y*y);
最后计算:
PI=1/a;

附件中是我写的计算例子计算PI的1000位小数...
上传的附件
文件类型: rar LargeFloat.rar (10.0 KB, 14 次查看)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
  #11 (permalink)  
旧 2008-06-05
初级会员
 
注册日期: 2008-06-05
帖子: 1
zzz3265 正向着好的方向发展
默认 回复: 大家把能找到的计算pi的程序/算法都贴在这里

来自
[url=http://hi.baidu.com/digiter/blog/item/c2b6d801c2ce6cd5277fb590.html]

#include <stdio.h>
#include <math.h>
#define PI 3.141592654
#define k 1e-7
// pi/4=1-1/3+1/5-1/7...
double pi1()
{
double sum1=0, sum2, n=1, s=1;
do
{
sum2 = sum1;
sum1 += s / n;
s *= -1;
n += 2;
}while (fabs(4 * sum1 - 4 * sum2) > k);
return 4 * sum1;
}
// pi*pi/6=1/1*1 + 1/2*2 + 1/3*3 + 1/4*4...
double pi2()
{
double n=1, sum1=0, sum2;
do
{
sum2 = sum1;
sum1 += 1 / (n * n);
n += 1;
}while (fabs(sqrt(6 * sum1) - sqrt(6 * sum2)) > k);
return sqrt(6 * sum1);
}
// pi/6=1/2 + (1/2)*(1/3)*(1/2)^3 + (1/2*3/4)*(1/5)*(1/2)^5 + (1/2*3/4*5/6)*1/7*(1/2)^7...
double pi3()
{
double sum1=0.5, sum2, i=1, a=0.5, b=1/3.0, c=0.5*0.5*0.5;
do
{
sum2 = sum1;
sum1 += a*b*c;
i += 1;
a *= ((i + i - 1)/(i + i));
b = 1 /(i + i + 1);
c *= 0.5 * 0.5;
}while (fabs(6 * sum1 - 6 * sum2) > k);
return 6 * sum1;
}
// pi/2=2/1 * 2/3 * 4/3 * 4/5 * 6/5 * 6/7 * 8/7 * 8/9...
double pi4()
{
double sum1=1, sum2, t=2;
do
{
sum2 = sum1;
sum1 *= t / (t - 1);
sum1 *= t / (t + 1);
t += 2;
}while (fabs(2 * sum1 - 2 * sum2) > k);
return 2 * sum1;
}
int main()
{
printf("%.9lf\n", pi1());
printf("%.9lf\n", pi2());
printf("%.9lf\n", pi3());
printf("%.9lf\n", pi4());
printf("%.9lf\n", acos(-1));
getchar();
return 0;
}

输出:

3.141592704
3.141283750
3.141592647
3.141312517
3.141592654
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
回复时引用此帖
回复

书签

主题工具
显示模式

发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛启用 表情符号
论坛启用 [IMG] 代码
论坛禁用 HTML 代码
Trackbacks are 启用
Pingbacks are 启用
Refbacks are 启用



所有时间均为格林尼治时间 +9。现在的时间是 06:59 AM


Powered by vBulletin® 版本 3.7.0
版权所有 ©2000 - 2008,Jelsoft Enterprises Ltd.
(C) Copy Right All Right Reserved 2001 - 2007

Search Engine Friendly URLs by vBSEO 3.1.0