원주율 구하기 프로그램

cppig1995의 이미지

#include <stdio.h>
#include <conio.h>

#define MAXDIGIT 2503

void divide_longnum_by_int( int x[], int y, int z[] )
{
long dividend;
int quotient;
long remainder;
int i;
remainder = 0;
for (i = 0; i < MAXDIGIT; i++) {
dividend = remainder * 10000L + x[i];
quotient = dividend / y;
remainder = dividend % y;
z[i] = quotient;
}
}

void multiply_longnum_by_int( int x[], int y, int z[] )
{
long sum;
long carry;
int i;

carry = 0;
for (i = MAXDIGIT - 1; i >= 0; i--) {
sum = (long)x[i] * y + carry;
z[i] = sum % 10000;
carry = sum / 10000;
}
}

void add_longnum( int x[], int y[], int z[] )
{
int carry, sum;
int i;
carry = 0;
for (i = MAXDIGIT - 1; i >= 0; i--) {
sum = x[i] + y[i] + carry;
z[i] = sum % 10000;
carry = sum / 10000;
}
}

int longnum_is_zero( int x[] )
{
int i;
for (i = MAXDIGIT - 1; i >= 0; i--) {
if (x[i] != 0)
return 0;
}
return 1;
}

void print_longnum( int x[] )
{
int i, count;

printf("%d.", x[0]);

count = 1;
for (i = 1; i < MAXDIGIT - 2; i++) {
printf("%04d", x[i]);
}
}

void set_longnum_to_int( int x[], int n )
{
int i;

x[0] = n;
for (i = 1; i < MAXDIGIT; i++) {
x[i] = 0;
}
}


int main()
{
int a[MAXDIGIT];
int pi[MAXDIGIT];
int i, j;

clrscr();

set_longnum_to_int( pi, 2 );
set_longnum_to_int( a, 2 );

for (i = 1, j = 3; ! longnum_is_zero( a ); i++, j += 2) {
multiply_longnum_by_int( a, i, a );
divide_longnum_by_int( a, j, a );
add_longnum( pi, a, pi );
}

gotoxy(1, 5);
printf("Value of PI ( %4d digit ) =", MAXDIGIT);
gotoxy(1, 7);
print_longnum( pi );
printf("\n");

getch();

return 0;
}
File attachments: 
첨부파일 크기
Image icon f8615f3816d774fb80a84914b92638c0.png1.69 KB
Plain text icon pi500.txt1.95 KB
Forums: 
puzzlet의 이미지

이게 더 간단한 것 같은데요? :twisted:

 p 410p 120p 130p  v
v01+1*2\*g00*::::+1<
>g:00p*+10p::*20g*v
vp03+*p02:g03+1*2\<$
>00g20g/ :10g30g/ -|
^^v**25%g02g00,+"0"<
^ >00p10g30g%52**10p

P.S. 일부 인터프리터에서는 자리수 제한이 있으며, PyFunge에서 돌리면 제대로 돌아갑니다.
댓글 첨부 파일: 
첨부파일 크기
Image icon 0바이트

발발다빠따반반나다발딸발발다빠따따맣발발다뿌
멓터벅더떠벋떠벌더벌벌떠벌떠더법벍떠더벌벌떠

codebank의 이미지

자유 게시판보다는 강좌나 팁에 가까운것 같아서 게시물을 옮겼습니다.
:)

------------------------------
좋은 하루 되세요.

hey의 이미지

명예의 전당으로 갈 조짐이 보입니다. ..


----------------------------
May the F/OSS be with you..


cppig1995의 이미지

Python 에다 재귀라면 인터프리터는 재귀의 깊이를 제한할 겁니다.

그리고 Redirection 과 함께 하면,

나오는 내용은 첨부로...

[수정] 죄송합니다.

그냥 올렸더니 한 줄로 나타난 소수점 약 2000자리 수치, 초소형 스크롤바, 망가진 빠른 답장 양식이 보이는군요.

댓글 첨부 파일: 
첨부파일 크기
Plain text icon 0바이트

Real programmers /* don't */ comment their code.
If it was hard to write, it should be /* hard to */ read.