Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > IT форум > Помощь студентам
Регистрация

Восстановить пароль
Повторная активизация e-mail

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 06.12.2011, 19:54   #1
Sobaka_ru
Пользователь
 
Регистрация: 09.12.2010
Сообщений: 44
По умолчанию Вычисление числа П

Нужна программа на C, вычисляющая число "ПИ" ну или хотя бы карказ ( с объяснением построчно, если можно )
Sobaka_ru вне форума Ответить с цитированием
Старый 06.12.2011, 19:56   #2
IHOR1989
 
Регистрация: 22.03.2010
Сообщений: 3
По умолчанию

Могу сбросить программу написанную для MPI если нужно говори
IHOR1989 вне форума Ответить с цитированием
Старый 06.12.2011, 20:48   #3
rUs_LAN
Форумчанин
 
Регистрация: 15.11.2008
Сообщений: 577
По умолчанию

PHP код:
#include <time.h>
#include <stdio.h>
#include <malloc.h>
#include <math.h>
#include <conio.h>
long B=10000/* Working base */
long LB=4;    /* Log10(base)  */
long MaxDiv=450;  /* about sqrt(2^31/B) */
/*
** Set the big real x to the small integer Integer 
*/
void SetToInteger (long nlong *xlong Integer) {
  
long i;
  for (
i=1i<ni++) x[i] = 0;
  
x[0] = Integer;
}
/*
** Is the big real x equal to zero ?
*/
long IsZero (long nlong *x) {
  
long i;
  for (
i=0i<ni++)  
    if (
x[i])   return 0;
        return 
1;
}
/*
** Addition of big reals : x += y
**  Like school addition with carry management
*/
void Add (long nlong *xlong *y) {
  
long carry=0i;
  for (
i=n-1i>=0i--) {
    
x[i] += y[i]+carry;
    if (
x[i]<Bcarry 0;
    else {
      
carry 1;
      
x[i] -= B;
    }
  }  
}
/*
** Substraction of big reals : x -= y
**  Like school substraction with carry management
**  x must be greater than y
*/
void Sub (long nlong *xlong *y) {
  
long i;
  for (
i=n-1i>=0i--) {
    
x[i] -= y[i];
                if (
x[i]<0) {
                  if (
i) {      
        
x[i] += B;
        
x[i-1]--;
      }
                }
  }  
}
/*
** Multiplication of the big real x by the integer q 
** x = x*q.
**  Like school multiplication with carry management
*/
void Mul (long nlong *xlong q) {
  
long carry=0xii;
  for (
i=n-1i>=0i--) {
    
xi  x[i]*q;               
    
xi += carry;                
    if (
xi>=B) {
      
carry xi/B;
      
xi -= (carry*B);
    }
    else 
      
carry 0;
    
x[i] = xi;
        }  
}
/*
** Division of the big real x by the integer d 
** The result is y=x/d.
**  Like school division with carry management
**  d is limited to MaxDiv*MaxDiv.
*/
void Div (long nlong *xlong dlong *y) {
  
long carry=0xiqi;
  for (
i=0i<ni++) {
    
xi    x[i]+carry*B;
    
q     xi/d;
    
carry xi-q*d;   
    
y[i]  = q;        
  }  
}
/*
** Find the arc cotangent of the integer p (that is arctan (1/p))
**  Result in the big real x (size n)
**  buf1 and buf2 are two buffers of size n
*/
void arccot (long plong nlong *xlong *buf1long *buf2) {
  
long p2=p*pk=3sign=0;
  
long *uk=buf1, *vk=buf2;
  
SetToInteger (nx0);
  
SetToInteger (nuk1);      /* uk = 1/p */
  
Div (nukpuk);
  
Add (nxuk);               /* x  = uk */

  
while (!IsZero(nuk)) {
    if (
p<MaxDiv)
      
Div (nukp2uk);  /* One step for small p */
    
else {
      
Div (nukpuk);   /* Two steps for large p (see division) */
      
Div (nukpuk);  
    }
    
/* uk = u(k-1)/(p^2) */
    
Div (nukkvk);       /* vk = uk/k  */
    
if (signAdd (nxvk); /* x = x+vk   */
    
else Sub (nxvk);      /* x = x-vk   */
    
k+=2;
    
sign 1-sign;
  }
}
/*
** Print the big real x
*/
void Print (long nlong *x) {
  
long i
  
printf ("%d."x[0]);
  for (
i=1i<ni++) {
    
printf ("%.4d"x[i]);
    if (
i%25==0printf ("%8d\n"i*4);
  }
  
printf ("\n");
}
/*
** Computation of the constant Pi with arctan relations
*/
void main () {  
  
clock_t endclockstartclock
  
long NbDigits=10000NbArctan;
  
long p[10], m[10];
  
long size=1+NbDigits/LBi;
  
long *Pi      = (long *)malloc(size*sizeof(long));
  
long *arctan  = (long *)malloc(size*sizeof(long));
  
long *buffer1 = (long *)malloc(size*sizeof(long));
  
long *buffer2 = (long *)malloc(size*sizeof(long)); 
  
startclock clock();    
  
/*
  ** Formula used: 
  **   
  **   Pi/4 = 12*arctan(1/18)+8*arctan(1/57)-5*arctan(1/239) (Gauss)
  */
  
NbArctan 3;
  
m[0] = 12m[1] = 8;  m[2] = -5;
  
p[0] = 18p[1] = 57p[2] = 239
  
SetToInteger (sizePi0);
  
/*
  ** Computation of Pi/4 = Sum(i) [m[i]*arctan(1/p[i])] 
  */
  
for (i=0i<NbArctani++) {
    
arccot (p[i], sizearctanbuffer1buffer2);
    
Mul (sizearctanabs(m[i]));
    if (
m[i]>0Add (sizePiarctan);  
    else        
Sub (sizePiarctan);  
  }
  
Mul (sizePi4);
  
endclock clock ();
  Print (
sizePi);  /* Print out of Pi */
  
printf ("Computation time is : %9.2f seconds\n",  
         (float)(
endclock-startclock)/(float)CLOCKS_PER_SEC ); 
  
free (Pi);
  
free (arctan);
        
free (buffer1);
        
free (buffer2);
        
getch();
        

google....
rUs_LAN вне форума Ответить с цитированием
Старый 09.12.2011, 13:13   #4
Sobaka_ru
Пользователь
 
Регистрация: 09.12.2010
Сообщений: 44
По умолчанию

Цитата:
Сообщение от rUs_LAN Посмотреть сообщение
PHP код:
#include <time.h>
#include <stdio.h>
#include <malloc.h>
#include <math.h>
#include <conio.h>
long B=10000/* Working base */
long LB=4;    /* Log10(base)  */
long MaxDiv=450;  /* about sqrt(2^31/B) */
/*
** Set the big real x to the small integer Integer 
*/
void SetToInteger (long nlong *xlong Integer) {
  
long i;
  for (
i=1i<ni++) x[i] = 0;
  
x[0] = Integer;
}
/*
** Is the big real x equal to zero ?
*/
long IsZero (long nlong *x) {
  
long i;
  for (
i=0i<ni++)  
    if (
x[i])   return 0;
        return 
1;
}
/*
** Addition of big reals : x += y
**  Like school addition with carry management
*/
void Add (long nlong *xlong *y) {
  
long carry=0i;
  for (
i=n-1i>=0i--) {
    
x[i] += y[i]+carry;
    if (
x[i]<Bcarry 0;
    else {
      
carry 1;
      
x[i] -= B;
    }
  }  
}
/*
** Substraction of big reals : x -= y
**  Like school substraction with carry management
**  x must be greater than y
*/
void Sub (long nlong *xlong *y) {
  
long i;
  for (
i=n-1i>=0i--) {
    
x[i] -= y[i];
                if (
x[i]<0) {
                  if (
i) {      
        
x[i] += B;
        
x[i-1]--;
      }
                }
  }  
}
/*
** Multiplication of the big real x by the integer q 
** x = x*q.
**  Like school multiplication with carry management
*/
void Mul (long nlong *xlong q) {
  
long carry=0xii;
  for (
i=n-1i>=0i--) {
    
xi  x[i]*q;               
    
xi += carry;                
    if (
xi>=B) {
      
carry xi/B;
      
xi -= (carry*B);
    }
    else 
      
carry 0;
    
x[i] = xi;
        }  
}
/*
** Division of the big real x by the integer d 
** The result is y=x/d.
**  Like school division with carry management
**  d is limited to MaxDiv*MaxDiv.
*/
void Div (long nlong *xlong dlong *y) {
  
long carry=0xiqi;
  for (
i=0i<ni++) {
    
xi    x[i]+carry*B;
    
q     xi/d;
    
carry xi-q*d;   
    
y[i]  = q;        
  }  
}
/*
** Find the arc cotangent of the integer p (that is arctan (1/p))
**  Result in the big real x (size n)
**  buf1 and buf2 are two buffers of size n
*/
void arccot (long plong nlong *xlong *buf1long *buf2) {
  
long p2=p*pk=3sign=0;
  
long *uk=buf1, *vk=buf2;
  
SetToInteger (nx0);
  
SetToInteger (nuk1);      /* uk = 1/p */
  
Div (nukpuk);
  
Add (nxuk);               /* x  = uk */

  
while (!IsZero(nuk)) {
    if (
p<MaxDiv)
      
Div (nukp2uk);  /* One step for small p */
    
else {
      
Div (nukpuk);   /* Two steps for large p (see division) */
      
Div (nukpuk);  
    }
    
/* uk = u(k-1)/(p^2) */
    
Div (nukkvk);       /* vk = uk/k  */
    
if (signAdd (nxvk); /* x = x+vk   */
    
else Sub (nxvk);      /* x = x-vk   */
    
k+=2;
    
sign 1-sign;
  }
}
/*
** Print the big real x
*/
void Print (long nlong *x) {
  
long i
  
printf ("%d."x[0]);
  for (
i=1i<ni++) {
    
printf ("%.4d"x[i]);
    if (
i%25==0printf ("%8d\n"i*4);
  }
  
printf ("\n");
}
/*
** Computation of the constant Pi with arctan relations
*/
void main () {  
  
clock_t endclockstartclock
  
long NbDigits=10000NbArctan;
  
long p[10], m[10];
  
long size=1+NbDigits/LBi;
  
long *Pi      = (long *)malloc(size*sizeof(long));
  
long *arctan  = (long *)malloc(size*sizeof(long));
  
long *buffer1 = (long *)malloc(size*sizeof(long));
  
long *buffer2 = (long *)malloc(size*sizeof(long)); 
  
startclock clock();    
  
/*
  ** Formula used: 
  **   
  **   Pi/4 = 12*arctan(1/18)+8*arctan(1/57)-5*arctan(1/239) (Gauss)
  */
  
NbArctan 3;
  
m[0] = 12m[1] = 8;  m[2] = -5;
  
p[0] = 18p[1] = 57p[2] = 239
  
SetToInteger (sizePi0);
  
/*
  ** Computation of Pi/4 = Sum(i) [m[i]*arctan(1/p[i])] 
  */
  
for (i=0i<NbArctani++) {
    
arccot (p[i], sizearctanbuffer1buffer2);
    
Mul (sizearctanabs(m[i]));
    if (
m[i]>0Add (sizePiarctan);  
    else        
Sub (sizePiarctan);  
  }
  
Mul (sizePi4);
  
endclock clock ();
  Print (
sizePi);  /* Print out of Pi */
  
printf ("Computation time is : %9.2f seconds\n",  
         (float)(
endclock-startclock)/(float)CLOCKS_PER_SEC ); 
  
free (Pi);
  
free (arctan);
        
free (buffer1);
        
free (buffer2);
        
getch();
        

google....
А можно с объяснением???
Sobaka_ru вне форума Ответить с цитированием
Старый 09.12.2011, 13:14   #5
Sobaka_ru
Пользователь
 
Регистрация: 09.12.2010
Сообщений: 44
По умолчанию

Цитата:
Сообщение от IHOR1989 Посмотреть сообщение
Могу сбросить программу написанную для MPI если нужно говори
Сбрось на всякий случай, посмотрю
Sobaka_ru вне форума Ответить с цитированием
Старый 09.12.2011, 18:20   #6
rUs_LAN
Форумчанин
 
Регистрация: 15.11.2008
Сообщений: 577
По умолчанию

зачем вычислять то что давно уже вычислено.
http://habrahabr.ru/blogs/algorithm/128454/ вот здесь понятно написано.
rUs_LAN вне форума Ответить с цитированием
Старый 10.12.2011, 13:00   #7
Sobaka_ru
Пользователь
 
Регистрация: 09.12.2010
Сообщений: 44
По умолчанию

Цитата:
Сообщение от rUs_LAN Посмотреть сообщение
зачем вычислять то что давно уже вычислено.
http://habrahabr.ru/blogs/algorithm/128454/ вот здесь понятно написано.
Воо.. спасибо
Sobaka_ru вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
задача вычисление числа 7olia7 Паскаль, Turbo Pascal, PascalABC.NET 3 13.03.2011 20:45
Вычисление числа e, Pi с заданной точностью MrakSPb Общие вопросы C/C++ 3 12.05.2010 12:51
Многопоточное вычисление числа Пи Novasty Помощь студентам 0 07.12.2009 00:49
Вычисление числа пи Maniac.Den Помощь студентам 2 30.11.2009 14:42
Вычисление факториала числа PAVEL315 Общие вопросы Delphi 17 21.03.2007 07:32