2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

アッカーマン関数の実装。

Posted at

アッカーマン関数のC言語による実装を示します。

実装例

call_numにackermann関数がどれだけコールされたか記録しています。

ackermann.c
# include<stdio.h>
int call_num = 0;

int main(void) {
	int a;
	int n, m;
	for (n = 0; n <= 3; n++) {
		for (m = 0; m <= 3; m++) {
			call_num = 0;
			a = ackermann(n, m);
			printf("ackermann(%d, %d) = %d\n", n, m, a);
			printf("call_num = %d\n", call_num);
		}
	}
	return 0;
}

int ackermann(int n, int m) {
	call_num++;
	if (n == 0)
		return m + 1;
	if (m == 0)
		return ackermann(n - 1, 1);
	return ackermann(n - 1, ackermann(n, m - 1));
}

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?