원주율 구하기 프로그램
글쓴이: cppig1995 / 작성시간: 목, 2005/01/13 - 4:34오후
#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:
첨부 | 파일 크기 |
---|---|
![]() | 1.69 KB |
![]() | 1.95 KB |
Forums:
이게 더 간단한 것 같은데요? :twisted: [code:1] p
이게 더 간단한 것 같은데요? :twisted:
P.S. 일부 인터프리터에서는 자리수 제한이 있으며, PyFunge에서 돌리면 제대로 돌아갑니다.
아발발다빠따반반나다발딸발발다빠따따맣발발다뿌
희멓터벅더떠벋떠벌더벌벌떠벌떠더법벍떠더벌벌떠
자유 게시판보다는 강좌나 팁에 가까운것 같아서 게시물을 옮겼습니다.:
자유 게시판보다는 강좌나 팁에 가까운것 같아서 게시물을 옮겼습니다.
:)
------------------------------
좋은 하루 되세요.
명예의 전당으로 갈 조짐이 보입니다. ..
명예의 전당으로 갈 조짐이 보입니다. ..
----------------------------
May the F/OSS be with you..
Redirection과 함께 하는 즐거운 세상!
Python 에다 재귀라면 인터프리터는 재귀의 깊이를 제한할 겁니다.
그리고 Redirection 과 함께 하면,
나오는 내용은 첨부로...
[수정] 죄송합니다.
그냥 올렸더니 한 줄로 나타난 소수점 약 2000자리 수치, 초소형 스크롤바, 망가진 빠른 답장 양식이 보이는군요.
Real programmers /* don't */ comment their code.
If it was hard to write, it should be /* hard to */ read.
댓글 달기