我有 16GB RAM 和这段代码
#include <stdio.h>
#define len 900000000
int phi[len];
int main() {
for (int i = 2; i < len; i++) {
phi[i] = i - 1;
}
phi[0] = 0;
phi[1] = 1;
for (int i = 2; i < len; i++) {
for (int j = 2 * i; j < len; j += i) {
phi[j] -= phi[i];
}
}
return 0;
}
此代码无法编译
error: unknown type size
有什么办法可以让它在不重写代码的情况下编译吗?我在 Windows 10 上工作。
我几乎可以做到了
Microsoft Windows [版本 10.0.19045.5011]
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <limits.h>
#include <assert.h>
#include <time.h>
//#define len 900000000
#define len 90000000*3
int phi[len];
int main() {
printf("%llu\n", (unsigned long long) SIZE_MAX);
printf("%d\n", INT_MAX);
printf("%llu\n", (unsigned long long) len);
assert(len < INT_MAX);
for (int i = 2; i < len; i++) {
phi[i] = i - 1;
}
phi[0] = 0;
phi[1] = 1;
for (int i = 2; i < len; i++) {
for (int j = 2 * i; j < len; j += i) {
phi[j] -= phi[i];
}
}
clock_t t = clock();
printf("Time: %g\n", (double) t / CLOCKS_PER_SEC);
return 0;
}
输出带有
90000000*3
18446744073709551615
2147483647
270000000
Time: 117.203
代码也用
90000000*5
编译,只是不想等待它完成 - 可能需要 6 分钟。
在
90000000*5
,它对于我的机器/编译器来说太大了。