sequences in GEB

Goedel, Escher, Bach 에 나오는 함수들을 만들어 보았다.

G sequence

#include

#include

#include

int gfunction(int);

int main(int argc, char* argv[])

{

int i;

if (argc==1){

printf(“Error. There are no options.\n”);

exit(1);

}

i=atoi(argv[1]);

printf(“%d”,gfunction(i));

}

int gfunction(int i)

{

if (i<0) {
printf(“Error. Parameter is negative.\n”);

}

else if (i==0) {

return 0;

}

else {

return (i-gfunction(gfunction(i-1)));

}

}

H sequence

#include

#include

#include

int hfunction(int);

int main(int argc, char* argv[])

{

int i;

if (argc==1){

printf(“Error. There are no options.\n”);

exit(1);

}

i=atoi(argv[1]);

printf(“%d”,hfunction(i));

}

int hfunction(int i)

{

if (i<0) {
printf(“Error. Parameter is negative.\n”);

}

else if (i==0) {

return 0;

}

else {

return (i-hfunction(hfunction(hfunction(i-1))));

}

}

M and F sequence

#include

#include

#include

int mfunction(int);

int ffunction(int);

int main(int argc, char* argv[])

{

int i;

if (argc==1){

printf(“Error. There are no options.\n”);

exit(1);

}

i=atoi(argv[1]);

printf(“%d\n”,mfunction(i));

printf(“%d\n”,ffunction(i));

}

int mfunction(int i)

{

if (i<0) {
printf(“Error. Parameter is negative.\n”);

}

else if (i==0) {

return 0;

}

else {

return (i-ffunction(mfunction(i-1)));

}

}

int ffunction(int i)

{

if (i<0) {
printf(“Error. Parameter is negative.\n”);

}

else if (i==0) {

return 1;

}

else {

return (i-mfunction(ffunction(i-1)));

}

}

Q sequence

#include

#include

#include

int hfunction(int);

int main(void)

{

int n;

for(n=1;n<100;n++)
printf(“%d, “,qfunction(n));

}

int qfunction(int i)

{

if (i<0) {
printf(“Error. Parameter is negative.\n”);

}

else if (i==2 || i==1) {

return 1;

}

else {

int q=qfunction(i-qfunction(i-1))+qfunction(i-qfunction(i-2));

return q;

}

}

그냥 작동하기만 할 뿐인 프로그램들이다.

컴파일은 DevC++ 4.9.9.1 with MinGW 에서 해서 작동하는 것을 확인하였다.

코멘트

“sequences in GEB”에 대한 2개 응답

  1. 
                  snowall
                  아바타

    재밌는 수열이더라구요.

  2. 
                 flyingtext
                 아바타

    재귀호출 ㄷㄷㄷ;;;

    DFS생각나네요 ㅎ

댓글 남기기

이 사이트는 Akismet을 사용하여 스팸을 줄입니다. 댓글 데이터가 어떻게 처리되는지 알아보세요.