- 注册时间
- 2009-10-1
- 最后登录
- 1970-1-1
- 威望
- 星
- 金币
- 枚
- 贡献
- 分
- 经验
- 点
- 鲜花
- 朵
- 魅力
- 点
- 上传
- 次
- 下载
- 次
- 积分
- 315
- 在线时间
- 小时
|
发表于 2009-10-2 15:23:35
|
显示全部楼层
通过程序模拟也可以得到相同的结果,以下是用Java编写的程序
public class SeriesTest
{
private int n;
private int k;
private int r;
private int a[];
private int C[];
public SeriesTest(int n,int k,int r)
{
this.n=n;
this.k=k;
this.r=r;
initial();
}
public void initial()
{
int i;
a=new int[n];
C=new int[n];
for(i=0;i<n;i++)
a[i]=i;
}
public int random(int min,int max)//create a number between min and max included both
{
return (int)Math.round(Math.random()*(max-min))+min;
}
public void randomSort()
{
int i,j,temp;
for(i=0;i<n-1;i++)
{
j=random(i,n-1);
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
public void randomSort(int k)
{
int i,j,temp;
for(i=0;i<k;i++)
{
j=random(i,n-1);
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
public void countSort(int k)
{
int i,j;
for(i=0;i<n;i++)
C[i]=0;
for(i=0;i<k;i++)
C[a[i]]=1;
for(i=0,j=0;j<k;i++)
if(C[i]!=0)
a[j++]=i;
}
public boolean isSeriesOfLength(int k)
{
int i;
countSort(k);
for(i=r-1;i<k;i++)
if(a[i]-a[i-r+1]==r-1)
return true;
return false;
}
public double pr(int loop)
{
int i,j;
for(i=0,j=0;i<loop;i++)
{
randomSort(k);
if(isSeriesOfLength(k))
j++;
}
return (double)j/loop;
}
public static double lnGamma(double n)
{
return 0.5*Math.log(2*Math.PI*n)+n*(Math.log(n)-1)+1.0/12/n;
}
public static void main(String[] args)
{
SeriesTest st;
int n=1138,k=514,r=14;
long t1,t2;
int loop=1<<18;
double p,p1,p2;
st=new SeriesTest(n,k,r);
t1=System.nanoTime();
p=st.pr(loop);
t2=System.nanoTime();
System.out.printf("time=%.9f seconds\nTest Pr=%f%%\n",(t2-t1)/1e9,p1=p*100);
System.out.printf("Real Pr=%f%%\n",p2=62500*Math.exp(lnGamma(k)+lnGamma(n-r)-lnGamma(n)-lnGamma(k-r)));
System.out.printf("Error of Pr=%f%%\n",p2-p1);
}
}
运行结果:
time=29.041681275 seconds
Test Pr=0.827408%
Real Pr=0.833043%
Error of Pr=0.005636% |
|