什么叫模卡:求2的开方精确到1000位以上的算法

来源:百度文库 编辑:神马品牌网 时间:2024/04/26 23:54:41
精确到1000位以上的算法

用二分法求2的平方根

//这是用java编写的一个求2的平方根的程序,精确度可通过修改weishu参数来改变
public class app
{ //用二分法求2的平方根
public static void main(String args[])
{
int a[],b[],s[],d[],c[],ss[];
int i,j,k;
a=new int[1000];
b=new int[1000];
s=new int[1000];
d=new int[1000];
c=new int[1000];
ss=new int[1000];
boolean jingque;
jingque=true;
a[0]=b[0]=2;
a[2]=b[2]=1;
a[1]=4;
b[1]=5;

int weishu=200;//定义循环次数
for(i=0;i<1000;i++)
s[i]=0;
for(k=0;k<weishu;k++)
{
hanshucheng.cheng(b,b,s);
j=s[0];
while (s[j]>=2)
{
hanshuadd.add(a,b,c);
hanshuchu.chu(d,c);
hanshucopy.copy(ss,b);
hanshucopy.copy(b,d);
hanshucheng.cheng(b,b,s);
j=s[0];
// for(i=0;i<=s[0];i++)
//System.out.println("s["+i+"]="+s[i]);
}
hanshucopy.copy(a,b);
hanshucopy.copy(b,ss);
}
for(i=a[0];i>=1;i--)
System.out.print(a[i]);
System.out.print("左边计算到"+a[0]+"位\n");
for(i=b[0];i>=1;i--)
System.out.print(b[i]);
System.out.print("右边计算到"+b[0]+"位\n");
for(i=a[0],j=b[0];jingque==true;i--,j--)
if(a[i]==b[j])
System.out.print(a[i]);
else
jingque=false;
System.out.print("精确到"+(a[0]-i-1)+"位\n");
}
}
class hanshucheng
{
public static void cheng( int a[],int b[],int s[])//定义两数相乘的函数
{
int flag=0,flag1=0;
int number=b[0];
int c[]=new int[1000];
int i,j,k,u;
for(int i1=0;i1<1000;i1++)
s[i1]=0;
for(i=1;i<=number;i++)
{
for(int i1=0;i1<1000;i1++)
c[i1]=0;
for(j=i,k=1;j<number+i;j++,k++)
{
c[j]=(a[k]*b[i]+flag)%10;
flag=(a[k]*b[i]+flag)/10;
}
if (flag!=0)
{
c[j]=flag;
flag=0;
j=j+1;
}
c[0]=j-1;
//for(k=1;k<=c[0];k++)
//System.out.println("c="+c[k]);
for(k=1;k<=c[0];k++)
{ u=s[k];
s[k]=(u+c[k]+flag1)%10;
flag1=(u+c[k]+flag1)/10;
}
if(flag1!=0)
{
s[k]=flag1;
k=k+1;
flag1=0;
}
s[0]=k-1;
// for(k=0;k<=s[0];k++)
//System.out.println(s[k]);
}
}
}
class hanshuadd
{
public static void add(int a[],int b[],int c[])//定义两数相加的函数
{
int flag=0;int i,j,k;
int a1[]=new int[1000];
for(i=1;i<=b[0];i++)
a1[i]=0;
for(j=b[0]-a[0]+1,k=1;j<=b[0];j++,k++)
a1[j]=a[k];
//for(k=0;k<=j;k++)
//System.out.println("a1="+a1[k]);
for(i=1;i<=b[0];i++)
{
c[i]=(a1[i]+b[i]+flag)%10;
flag=(a1[i]+b[i]+flag)/10;
}
if(flag!=0)
{
c[i]=flag;
i=i+1;
flag=0;
}
c[0]=i-1;
}
}
class hanshuchu
{

public static void chu(int d[],int a[])//定义任一数除以2的函数
{
int flag=0,i;
for(i=a[0];i>=1;i--)
{
d[i+1]=(flag*10+a[i])/2;
flag=(flag*10+a[i])%2;
}
if(flag!=0)
d[1]=5;
if(d[1]==0)
for(i=1;i<=a[0]+1;i++)
d[i]=d[i+1];
d[i]=0;
d[0]=a[0]+1;
}
}
class hanshucopy
{

public static void copy(int a[],int b[])//定义
{
int i;
for(i=0;i<=b[0];i++)
a[i]=b[i];
while (i<1000)
a[i++]=0;

}
}

这是我的博客主页 http://xingruiqiang.spaces.msn.com/

#include <iostream>
#include <cryptlib/integer.h>
#include <sstream>
using namespace std;
using namespace CryptoPP;

#define PREC 1000

int main( int argc , char* argv[] )
{
if( argc != 2 )
{
cout << "usage foobar <number>" << endl;
exit( 1 );
}

istringstream iss( argv[1] );
Integer nn , mm = 1 , mm2; iss >> nn;

for( int i = 0; i < PREC; ++i ) mm *= 10; mm2 = mm * mm; nn *= mm2;
cout << nn.SquareRoot() << endl;
}

你的这个算法好像不能实现的
你知道圆周率的值精确的位数吗?

用2分法,
先用1.4*1.4=1.96<2 及 1.5, 1.5*1.5=2.25 >; 2
(1. 4+1.5)/2=1.45 ==>; 1.45*1.45=2.1025>;2
(1. 4+1.45)/2=1.425 ==>; 1.425*1.425>;2
(1. 4+1.425)/2=1.4125 ==>; 1.4125*1.4125<2
(1.4125+1.425)/2=...
...
用这个方法, 只需要多位乘法.

我用matlab编了这么一个程序,

a=1.4;b=1.5;
for i=1:100;
c=(a+b)/2;
if c*c>2
b=(a+b)/2;
end
if c*c<2
a=(a+b)/2;
end
end
c